Twig Check Current Page

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

{% 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!

Use page.home which returns a boolean value https://learn.getgrav.org/api/Grav/Common/Page/Page.html#method_home

@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:

{% 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.

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:

{% 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

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:

{% 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

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?