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.

Archive

Twig variable as link

Started by Muut Archive 9 years ago · 8 replies · 3024 views
9 years ago

I stuck at a serious simple problem. I try to loop over Grav pages and dynamically output there links.

TWIG
{% for news in page.find('/aktuelles').children.order('header.news_date', 'asc').slice(0, 3) %}
<div class="recent-news">
    <a href="{{ news.url }}">
    <span>{{ news.header.news_date|date("d.m.Y") }}</span>
    {{ news.title }}
    </a>
</div>
{% endfor %}

Unfortunatly the Twig code

TWIG
{{ news.url }}

doesnt get replaced by the value behind the variable. Instead the generated link looks like this http://127.0.0.1/%7B%7B%20news.url%20%7D%7D. I tested the variable capsuled in the a-Tag. That worked! Does anybody know why twig can´t be rendered in a´s href-Attribute and have a workaround for me? Thanks!

9 years ago

Is there someone who knows what to do?

9 years ago

It's strange, it seems that Twig is not parsed. But since you're in the loop, and the title is shown, Twig is processing.

Moving it outside the <a> tag works?

9 years ago

yes, also moving outside the href works. My only actual workaround right now is to write the url in data-href and replace href on DOM load with the value with JS.

9 years ago

Hello,
I don't know why it's not working, but you could try this workaround :
Define a variable :

TWIG
{% set newsLink = news.url %}

Then use the variable instead of news.url :

TWIG

<a href="{{ newsLink }}"></a>
---
9 years ago

Thanks for your nice idea. Also tried it, but also not working. Twig got not parsed in the a´s href.

9 years ago

Oh wow! That is actually working but not a charm for my HTML markup :D

9 years ago

Is there another aproach to fix it on this way?

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1318 9 years ago
Archive · by Muut Archive, 9 years ago
2 915 9 years ago
Archive · by Muut Archive, 9 years ago
2 4044 9 years ago
Archive · by Muut Archive, 9 years ago
1 2920 9 years ago
Archive · by Muut Archive, 9 years ago
3 1104 9 years ago