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

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?

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

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

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

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

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

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

So, you could try in ‘blog.html.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 %}

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

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