Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Building Websites with PHP Slim Basics & Twig Templates Layouts with Twig

Aaron Munoz
Aaron Munoz
11,177 Points

A couple of gaps left open...

I can't figure out why I keep loading the about page when I'm previewing the contact.twig file. I stripped all the code out except for the content and put the appropriate code to extend the main.twig template but it still takes me to the about page. I can manually go to the contact page and works perfectly but when I preview it, it takes me to the about page. Is that a workspace bug?

I also would like to know how to fix the "selected" class to make it update on each page using the DRY principles.

And last, what do you change your href to in the main.twig file for your menu since the html links don't exist anymore? I changed the about page to a "/" href and the contact to a "/contact" href and seemed to work but wasn't sure if that was the best way.

3 Answers

Michael Soileau
Michael Soileau
9,431 Points

In main.twig:

 <nav>
  <a href="about" class="selected">About</a>
  <a href="contact">Contact</a>
</nav>

In index.php

$app->get("/", $ref = function() use ($app) { $app->render('about.twig'); });

$app->get("/index", $ref);

$app->get("/about", $ref);

$app->get("/contact", function() use ($app) { $app->render('contact.twig'); });

Your href has to match the routing parameters, which right now, aren't using wildcards or any other matching patterns except a string literal. E.g. it only matches the literal route http://{your-url-here}/{whatever-the-get-is}

Wildcards and optional routes are more advanced. Creating a variable reference to an anonymous function allows it to be reused, so while still using a literal pattern match, the homepage, going to index, or going to about will all take you to the about.twig file.

Aaron Munoz
Aaron Munoz
11,177 Points

That's awesome. Thanks!

Aaron Munoz
Aaron Munoz
11,177 Points

Actually, the named routes video answered most of this . ^_^

Jon Mullins
Jon Mullins
15,724 Points

I'm a little late to the party, but here's how I did mine:

In main.twig:

<a href={{ siteUrl('/contact') }} {% block contact_act %}{% endblock contact_act %}>Contact</a>

In contact.twig:

{% block contact_act %}class="selected"{% endblock contact_act %}

Do this for each link (named accordingly).