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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Including HTML on selective pages

Started by Lukas Severinghaus 8 years ago · 5 replies · 847 views
8 years ago

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?
Thanks,

8 years ago

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:

8 years ago

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 %}
<section id="body" class="{{ class }}">
{% block content %}{% endblock %}

TWIG
    </section>
    {% 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 northernazaerialphotography.com Any idea why this is occuring?

8 years ago

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.

8 years ago

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?

8 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 44 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago