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

Dynamic metatag

Started by JOduMonT 8 years ago · 2 replies · 577 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 Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 91 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 451 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 45 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 125 3 months ago