Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Dynamic metatag

Started by JOduMonT 8 years ago · 2 replies · 841 views
8 years ago

Hello everyone;

might be simple; but I'm more comfortable with file configuration than twig and php ;)
anyway...

in the file templates/partials/metatag.html.twig

the behaviour I expect as a result is :

if page/post contain a description show this description as a metatag
else show the meta description of the site.

I try to make something like this :

TWIG
...  
      {% if meta.description %}description="{% if header.description %}{{ header.description }}{% else %}{{ meta.description }}{% endif %}" {% endif %}  
...  

I also tried something like this

which make me a little bit closer because the page/post part works but the meta.description don't make it.

TWIG
...
<meta name="description" content="{% if header.description %}{{ header.description|e('html') }}{% else %}{{ meta.description|e('html') }}{% endif %}">
...

Any finger pointing I'm a good direction will be appreciate ;)

Have a nice day

J.

8 years ago

Please provide more contextual template code, specifically I am wondering from your examples:

  • what is the the value of meta - is there somewhere you've set this to page.header.metadata?

There are a couple of shorter Twig ways to output values if they exist (though I wouldn't mess with this in the first place unless you understand it completely):

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by MarkP, 2 weeks ago
3 193 2 weeks ago
Themes & Styling · by TomW, 2 weeks ago
0 111 2 weeks ago
Themes & Styling · by themetman, 3 weeks ago
1 206 3 weeks ago
Themes & Styling · by AH, 1 month ago
1 324 3 weeks ago
Themes & Styling · by Bruno, 4 years ago
6 1523 3 weeks ago