Including HTML on selective pages

I have a contact button on my website that I want to put on the top and bottom of every page, except for the contact page. I have the HTML for the button, and I think the correct way to implement this is to create a twig template that I include in the top and bottom of each page, but I’m not sure where to put the twig file and the correct way to include this through the markup. How do I best implement this?

You are correct that a Twig file is the best way to implement this. You want to create a “partial” template. Look at the way partial templates like navigation, footer, and header are stored and called from the base template in this documentation example for examples.

As for excluding that partial template from being included in the contact page, one way is to wrap the Twig include statement in a Twig condition. Use that to check if the current page is the contact page before including the partial template.

You’ll probably benefit more from me pointing you in the right direction and finding the solution yourself than me giving it to you. So look at these to derive your answer:

Thanks for the information. I’ve implemented it as you suggested in partials/base.html.twig
{% if not page.title() == “Contact Us” %}
{% include ‘partials/button.html.twig’ %}
{% endif %}
{% block body %}

{% block content %}{% endblock %}

    {% endblock %}
{% if not page.title() == "Contact Us" %}
        {% include 'partials/button.html.twig' %}
    {% endif %}

This shows up on the home screen of my page, which is based on the default template, but all the other pages, all of which use different templates that extend the base template don’t have it. The site is Any idea why this is occuring?

No, I’m afraid if they extend the same base template, I am not sure. I rarely have this problem in Grav, but have you checked cache and Twig cache settings? I usually have them turned off in development environments.

Try using != instead of if not too, though I can’t see how that would cause this to show on the homepage only.

I found the problem, it seemed to have something to do with the if statement not working properly, but changing the if statement line to: {% if page.url != find("/contact") %} has it working again, except for one thing. The buttons no longer seem to be affected by the page layout and are hidden at the bottom and top of the page rather than just above and just below the body areas. Any ideas as to why this is happening?

Solved my own problem, by putting the twig snippet inside the tags in the body block.