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 trialMircea Dan Dumitru
6,737 Points[Navigation] class="selected" with slim?
Could someone help me make the nav more dynamic with slim regarding the class="selected" ?
Thanks
<header> {% block header %} <h1>Ralph Waldo Emerson</h1> <nav> <a href="{{ baseUrl() }}" class="selected">About</a> <a href="{{ siteUrl('/contact') }}">Contact</a> </nav> {% endblock header %} </header>
2 Answers
thomascawthorn
22,986 PointsHey There!
Are you using Slim 2?
If so, Check out the helpers on the Slim request object. Scrolling down, you'll see a couple of useful methods - I think you're after getUrl().
So you can do something like:
<?php
$currentUrl = $app->request->getUrl();
Then check in the a tag if you should apply the 'selected' class
class="{% if currentUrl == 'some-url' %}selected{% endif %}"
Tom
Nurrokhman Hidayat
12,705 PointsTom, Can tell me detail about the code , I tried but failed
so now i use this method
in about.twig and contact.twig I added this code :
{% set active_page = 'about' %}
{% set active_page = 'contact' %}
and the main template I added this code :
<a href="{{ baseUrl() }}" class="{% if active_page== 'about' %} selected{% endif %}">About</a>
<a href="{{siteUrl('/contact')}}" class="{% if active_page== 'contact' %} selected{% endif %}">Contact</a>