Skip to content

Commit 1ef2209

Browse files
committed
add mealplans
US4
1 parent 00be89d commit 1ef2209

File tree

9 files changed

+147
-6
lines changed

9 files changed

+147
-6
lines changed

public/contact.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
require_once '../vendor/autoload.php';
33

44
use Twig\Environment;
5+
use Twig\Error\LoaderError;
6+
use Twig\Error\RuntimeError;
7+
use Twig\Error\SyntaxError;
58
use Twig\Loader\FilesystemLoader;
69

710
$loader = new FilesystemLoader('../templates');
811
$twig = new Environment($loader);
912

10-
$template = $twig->load('contact.html.twig');
11-
12-
echo $template->render(['the' => 'variables', 'go' => 'here']);
13+
try {
14+
echo $twig->render('contact.html.twig');
15+
} catch (LoaderError|SyntaxError|RuntimeError $e) {
16+
echo $e->getMessage();
17+
}

public/index.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
<?php
2+
23
require_once '../vendor/autoload.php';
34

5+
use Radlinger\Mealplan\Seeder\MealSeeder;
46
use Twig\Environment;
7+
use Twig\Error\LoaderError;
8+
use Twig\Error\RuntimeError;
9+
use Twig\Error\SyntaxError;
510
use Twig\Loader\FilesystemLoader;
611

712
$loader = new FilesystemLoader('../templates');
813
$twig = new Environment($loader);
914

10-
$template = $twig->load('home.html.twig');
15+
$meal_plans = MealSeeder::generate();
1116

12-
echo $template->render(['the' => 'variables', 'go' => 'here']);
17+
try {
18+
echo $twig->render('home.html.twig', ['meal_plans' => $meal_plans]);
19+
} catch (LoaderError|SyntaxError|RuntimeError $e) {
20+
echo $e->getMessage();
21+
}

src/Classes/Meal.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Radlinger\Mealplan\Classes;
4+
5+
use JsonSerializable;
6+
7+
class Meal implements JsonSerializable
8+
{
9+
public int $id;
10+
public string $name;
11+
public string $allergens;
12+
public string $nutritionalInfo;
13+
public float $price;
14+
15+
function __construct(
16+
int $id,
17+
string $name,
18+
string $allergens,
19+
string $nutritionalInfo,
20+
float $price,
21+
)
22+
{
23+
$this->id = $id;
24+
$this->name = $name;
25+
$this->allergens = $allergens;
26+
$this->nutritionalInfo = $nutritionalInfo;
27+
$this->price = $price;
28+
}
29+
30+
public function jsonSerialize(): array {
31+
return get_object_vars($this);
32+
}
33+
}

src/Classes/MealPlan.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Radlinger\Mealplan\Classes;
4+
5+
use JsonSerializable;
6+
7+
class MealPlan implements JsonSerializable
8+
{
9+
public int $id;
10+
public string $name;
11+
public string $schoolName;
12+
public string $weekOfDelivery;
13+
public array $meals;
14+
15+
function __construct(
16+
int $id,
17+
string $name,
18+
string $schoolName,
19+
string $weekOfDelivery,
20+
array $meals,
21+
)
22+
{
23+
$this->id = $id;
24+
$this->name = $name;
25+
$this->schoolName = $schoolName;
26+
$this->weekOfDelivery = $weekOfDelivery;
27+
$this->meals = $meals;
28+
}
29+
30+
public function jsonSerialize(): array
31+
{
32+
return get_object_vars($this);
33+
}
34+
}

src/Seeder/MealSeeder.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Radlinger\Mealplan\Seeder;
4+
use Radlinger\Mealplan\Classes\Meal;
5+
use Radlinger\Mealplan\Classes\MealPlan;
6+
7+
class MealSeeder
8+
{
9+
public static function generate(): array
10+
{
11+
$plans = [];
12+
$anz_plans = 3;
13+
$meal_id = 0;
14+
for ($plans_index = 0; $plans_index < $anz_plans; $plans_index++) {
15+
$meals = [];
16+
$anz_meals = 4;
17+
for ($meal_index = 0; $meal_index < $anz_meals; $meal_index++) {
18+
$meals[] = new Meal(
19+
$meal_id,
20+
"Meal $meal_index",
21+
"Gluten, Lactose",
22+
"Calories: " . rand(400, 700),
23+
rand(5, 10)
24+
);
25+
$meal_id++;
26+
}
27+
$plans[] = new MealPlan(
28+
$plans_index,
29+
"Meal Plan $plans_index",
30+
"HTL Rennweg",
31+
"Week $plans_index",
32+
$meals
33+
);
34+
}
35+
return $plans;
36+
}
37+
}

templates/base.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>{% block title %}ADV4{% endblock %}</title>
5+
<title>{% block title %}ADV4{% endblock %}</title> {# text is taken if none provided #}
66
<link rel="stylesheet" href="/styles/style.css">
77
</head>
88
<body>

templates/home.html.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@
55
{% block content %}
66
<h1>Welcome to the Home Page</h1>
77
<p>This is the homepage content.</p>
8+
9+
<h2>Our Meal Plans</h2>
10+
<div class="homepage-meal-plans">
11+
{% for plan in meal_plans %}
12+
{% include 'meal_plan.html.twig' with { 'plan': plan } %}
13+
{% endfor %}
14+
</div>
815
{% endblock %}

templates/meal.html.twig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="meal-card">
2+
<h3 class="meal-name">{{ meal.name }}</h3>
3+
<p class="meal-allergens"><strong>Allergens:</strong> {{ meal.allergens }}</p>
4+
<p class="meal-calories"><strong>Nutritional Info:</strong> {{ meal.nutritionalInfo }}</p>
5+
<p class="meal-rating"><strong>Price:</strong> {{ meal.price }}</p>
6+
</div>

templates/meal_plan.html.twig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="meal-plan-card">
2+
<h2 class="plan-name">{{ plan.name }}</h2>
3+
<p class="plan-location">{{ plan.schoolName }} | {{ plan.weekOfDelivery }}</p>
4+
5+
<div class="meals-container">
6+
{% for meal in plan.meals %}
7+
{% include 'meal.html.twig' with { 'meal': meal } %}
8+
{% endfor %}
9+
</div>
10+
</div>

0 commit comments

Comments
 (0)