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 Contact Form & Sending Email Named Routes

Did you end up explaining how to highlight 'contact' when that page is selected?

I watched all the videos and I don't think you explained it, would love to know since it's my first time using a framework.

Carmine Maraglio
Carmine Maraglio
29,762 Points

I was able to get this working with the following code in my main.twig file:

{% set url = currentUrl() %}
<a href="{{ baseUrl() }}" {% if url == "http://localhost/" %} class='selected' {% endif %}>About</a>
<a href="{{ siteUrl("/contact") }}" {% if url == "http://localhost/contact" %} class='selected' {% endif %}>Contact</a>

3 Answers

Jon Mullins
Jon Mullins
15,724 Points

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).

Michal Broniewicz
Michal Broniewicz
6,681 Points

It is not that tricky actually. All you have to do is overwrite data in contact page to make it highlighted in the nav menu. For sure there are few ways of doing that but i will explain you method I used for title - same will work for you. We start with title inside main.twig looking like this

<title>{%  block title %}Your title{%  endblock title %}</title>

And when creating contact.twig you can add this line to change title to something else

{%  block title %}Title saying something else{%  endblock title %}

Make highlighted item in its .twig file not the global one for template (main.twig used in video) and change it when needed.

Elliot Alderton
Elliot Alderton
11,955 Points

I used {% block nav %} in main.twig around the nav. Then used the below to override it in contact.twig

 {% block nav %}
      <a href="http://localhost/composer_example">About</a>
      <a href="contact" class="selected">Contact</a>
 {% endblock nav %}