Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses 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;
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Currently our display recipe uses only
the title and source properties and
0:00
returns those to the function call
as a single formatted string.
0:04
The first thing we need to do is start an
output variable instead of returning this
0:09
line directly we're gonna add
that to the output variable.
0:13
We can then return our output variable.
0:25
We'll keep the title and
source as our first line.
0:32
Then we'll add a new line.
0:35
We'll use our concatenation
and; we'll add a new line character.
0:40
Our next line will be the tags.
0:44
We're gonna want to use an implode.
0:57
We'll separate these with a comma.
1:05
Let's add another line break.
1:08
Now we can add our ingredients.
1:13
Formatting for our ingredients
is a little more complicated.
1:17
So let's loop through the ingredients and
format each line.
1:20
First we'll add the amount.
1:37
And then we'll add our measure.
1:47
And finally, our item.
1:55
We also want to add a new
line after each item.
2:01
Let's add our instructions
using implode again.
2:10
This time we'll separate
with a line break.
2:20
Let's add another line break.
2:30
And now our yield.
2:38
Let's take a look at
this by running our code.
2:47
This looks pretty good let's
add a couple more line breaks.
2:51
I want an extra one before our ingredients
and an extra line before our instructions.
2:57
Pull this up and try again.
3:12
Great.
That looks much better.
3:14
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up