Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Twig Check Current Page

Started by Muut Archive 9 years ago · 3 replies · 6962 views
9 years ago

Is there a way to check the current page using a twig if statement? For example, I want to show a certain header on the home page (home.md w/ home.twig.html) and then use a different header on all other pages. In the base.html.twig file, I wish to put something like this

TWIG
{% block header %}
   {% if page == home %}
      {% include "partials/header_home.html.twig %}
   {% elseif %}
      {% include "partials/header_alt.html.twig %}
   {% endif %}
{% endblock %}

I'm having trouble with the {% if page == home %} section. How do you check the current page and how to you specify certain pages (in this case, the home page)? If there is an alternative method, I would be willing to try it. But either way, I'd still like to know how to check for current page and how to specify certain pages in twig. I've looked up some examples, but couldn't get it to work!

9 years ago

@flavioscopes answer is correct, but only works because you are specifically targeting the home page. I would like to offer another answer that is a little broader.

You can use your page's frontmatter for this sort of thing. Come up with a variable like header_template. If a page does not have that value set, the page will use the default header template. If you want to use partials/header_alt.html.twig then you set that value like so `header_template: alt'.

Then change your twig if statement to:

TWIG
{% block header %}
   {% if page.header.header_template == "alt" %}
      {% include "partials/header_alt.html.twig %}
   {% elseif page.header.header_template == "other" %}
      {% include "partials/header_other.html.twig %}
   {% else %}
      {% include "partials/header_home.html.twig %}
   {% endif %}
{% endblock %}

Now your template is more flexible and you can have several different headers.

Be practical, not clever. You will confuse your users if every page looks different and if you are doing this many times in your template, your server will have to w ork harder to build pages and could slow you down. Even more important is your templates become more confusing and harder to maintain. The alternative is to have multiple base templates, it depends on your situation which is better.

9 years ago

Thank you both for your suggestions. I tried both and the first method (@flaviocopes) did not work. The second (@Ryan Spencer) did work.

Try 1:

TWIG
{% block header %}
        {% if page.home == True %}
                    {% include "partials/header.html.twig" %}
        {% else %}
                    {% include "partials/header_alt.html.twig" %}  
        {% endif %} 
{% endblock %}

For reference, inside user/config/system.yaml I have

YAML
home:
   alias: '/home'

And a folder called 01.home with a home.md inside (the page that loads when visiting the website). This try did not work.

Try 2:

TWIG
{% block header %}
        {% if page.header.header_var == 'home' %}
                    {% include "partials/header.html.twig" %}
        {% else %}
                    {% include "partials/header_alt.html.twig" %}
        {% endif %} 
{% endblock %} 
---  
Then, inside /user/pages/01.home/home.md I have 

header_var: home

TXT

in the front matter. This method worked! Thank you! I'm curious though, why didn't @flaviocopes method work? Is my 01.home/home.md not the technical homepage for some reason?

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1329 9 years ago
Archive · by Muut Archive, 9 years ago
2 922 9 years ago
Archive · by Muut Archive, 9 years ago
2 4051 9 years ago
Archive · by Muut Archive, 9 years ago
1 2930 9 years ago
Archive · by Muut Archive, 9 years ago
3 1108 9 years ago