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

Where to put the snippet for anchor menu in the twig file?

first-time plugins

Solved by pamtbaau View solution

Started by Hannah Liesen 5 years ago · 5 replies · 1280 views
5 years ago

Hi there,

I'm new to wonderful grav CMS and would like to add a menu from anchors to the top of a page with lots of contents divided by h3 headlines.

I know that there's a plugin called grav-plugin-anchors which I already have installed in my site. But I'm not sure where to put the code for the menu. In the readme of the plugin it says:

If you want to use the generated links to also generate a menu from these anchors, just put the function below in the template file Twig.
source: https://github.com/getgrav/grav-plugin-anchors

As a grav newbie I'm not sure where exactly to place the snippet in the blog.html.twig. I have tried several places but I always got an error. Maybe somebody has a hint for that?

5 years ago

@hanlie, Hard to tell what hint you need...

  • Which theme are you using?
  • Which places did you try in blog.html.twig?
  • Which errors did you get?
  • Where do you want a list of anchors to appear?
last edited 01/05/21 by pamtbaau
5 years ago

Hi pamtbaau,

thanks for the quick reply. I use the default theme quark with a child-theme for custom adaptations. I tried to put the snippet

TWIG
{{ anchors(content, tag, terms) }}

in blog.html.twig. I would like it put the toc on top of the page. The syntax error is:

A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?

My question is more in general. It's the first time that I want to extend the code (apart from small css adjustments) and I'm getting the feeling that I'm missing some basic steps here. As I said I'm new to grav and definitely on newbie level.

5 years ago Solution

@hanlie, I believe the answer is in the error message from Twig:

A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?

When a template extends another template like 'blog.html.twig' extends '/partials/base.html.twig', you can only add snippets to existing blocks defined in the extended template. Blocks in 'base.html.twig' are for example:

TWIG
{% block javascripts %}
  ...
{% endblock %}

{% block body %}
  ...
{% endblock %}

So, you could try in 'blog.html.twig':

TWIG
{% block body %}
    <section id="body-wrapper" class="section">
        <section class="container {{ grid_size }}">
            {% block messages %}
                {% include 'partials/messages.html.twig' ignore missing %}
            {% endblock %}

Your snippet --->  {{ anchors(content, tag, terms) }}

            {{ block('content_surround') }}
        </section>
    </section>
{% endblock %}
5 years ago

@pamtbaau You're right. I did not put the snippet inside a {% block %} tag. But I also had to change the snippet I think. Now the error message is gone. But the plugin is still a little bit tricky. So I used „Page Toc“ (https://github.com/trilbymedia/grav-plugin-page-toc) instead which was easier to implement thanks to your explanation.

👍 1
5 years ago

@hanlie, Nice...

If your issue has been solved, please mark it as such by ticking the 'solution' icon in the lower right corner of any of the post leading to the solution.

👍 1

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 83 1 week ago
Plugins · by Xavier, 4 weeks ago
2 85 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1211 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 78 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 105 2 months ago