added more to menu chapter

This commit is contained in:
Patrick 2015-02-08 18:00:41 +01:00
parent 53d5731ffa
commit d9738776b2
2 changed files with 22 additions and 1 deletions

View file

@ -8,7 +8,7 @@ A good quick read on this is [ircmaxell on templating](http://blog.ircmaxell.com
For this tutorial we will use a PHP implementation of [Mustache](https://github.com/bobthecow/mustache.php). So install that package before you continue.
Other well known alternatives would be [Twig](http://twig.sensiolabs.org/) or [Smarty](http://www.smarty.net/), but they are both pretty bloated and offer too much functionality for just a template engine.
Another well known alternative would be [Twig](http://twig.sensiolabs.org/).
Now please go and have a look at the source code of the [engine class](https://github.com/bobthecow/mustache.php/blob/master/src/Mustache/Engine.php). As you can see, the class does not implement an interface.

View file

@ -6,5 +6,26 @@ Now we have some sweet dynamic pages. But nobody can find them.
Let's fix that now. In this chapter we will create a menu with links to all our pages.
For a start we will just send a hardcoded array to the template. Go to you `Homepage` controller and change your `$data` array to this:
```php
$data = [
'name' => $this->request->getParameter('name', 'stranger'),
'menuItems' => ['href' => '/', 'text' => 'Homepage'],
];
```
Now add the following at the top of your `Homepage.mustache` file:
```
{{#menuItems}}
<a href="{{ href }}">{{ text }}</a><br>
{{/menuItems}}
```
Now if you navigate to your homepage, you should see a link at the top.
So far so good. But now we realize that we want to reuse this code snippet on every page. We could create a separate file and include it every time, but there is a better solution.
[<< previous](10-dynamic-pages.md)