Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
There's a lot more to a recipe then a title and source. In this video we'll be adding the rest of our property to the displayRecipe method, using a simple easy to read format.
Code Sample
public static function displayRecipe($recipe)
{
$output = "";
$output .= $recipe->getTitle() . " by " . $recipe->getSource();
$output .= "\n";
$output .= implode(", ",$recipe->getTags());
$output .= "\n\n";
foreach ($recipe->getIngredients() as $ing) {
$output .= $ing["amount"] . " " . $ing["measure"] . " " . $ing["item"];
$output .= "\n";
}
$output .= "\n";
$output .= implode("\n", $recipe->getInstructions());
$output .= "\n";
$output .= $recipe->getYield();
return $output;
}
-
0:00
Currently our display recipe uses only the title and source properties and
-
0:04
returns those to the function call as a single formatted string.
-
0:09
The first thing we need to do is start an output variable instead of returning this
-
0:13
line directly we're gonna add that to the output variable.
-
0:25
We can then return our output variable.
-
0:32
Will keep the title and source as our first line.
-
0:35
Then we'll add a new line.
-
0:40
We'll use our concatenation and,we'll add a new line character.
-
0:44
Our next line will be the tags.
-
0:57
We're gonna want to use an implode.
-
1:05
We'll separate these with a comma.
-
1:08
Let's add another line break.
-
1:13
Now we can add our ingredients.
-
1:17
Formatting for our ingredients is a little more complicated.
-
1:20
So let's loop through the ingredients and format each line.
-
1:37
First we'll add the amount.
-
1:47
And then we'll add our measure.
-
1:55
And finally, our item.
-
2:01
We also ,want to add a new line after each item.
-
2:10
Let's add our instructions using implode again.
-
2:20
This time we'll separate with a line break.
-
2:30
Let's add another line break.
-
2:38
And now our yield.
-
2:47
Let's take a look at this by running our code.
-
2:51
This looks pretty good let's add a couple more line breaks.
-
2:57
I want an extra one before our ingredients and an extra line before our instructions.
-
3:12
Pull this up and try again.
-
3:14
Great. That looks much better.
You need to sign up for Treehouse in order to download course files.
Sign up