Muut
May 24, 2017, 12:41pm
1
I stuck at a serious simple problem. I try to loop over Grav pages and dynamically output there links.
{% 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
{{ news.url }}
doesnt get replaced by the value behind the variable. Instead the generated link looks like this http://127.0.0.1/{{%20news.url%20}} . 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!
Muut
May 25, 2017, 10:52am
2
Is there someone who knows what to do?
Muut
May 25, 2017, 2:36pm
3
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?
Muut
May 25, 2017, 3:21pm
4
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.
Muut
May 25, 2017, 6:17pm
5
Hello,
I don’t know why it’s not working, but you could try this workaround :
Define a variable :
{% set newsLink = news.url %}
Then use the variable instead of news.url
:
<a href="{{ newsLink }}"></a>
---
Muut
May 25, 2017, 6:40pm
6
Thanks for your nice idea. Also tried it, but also not working. Twig got not parsed in the a´s href.
Muut
May 25, 2017, 6:48pm
7
Could you try to take the <a>
out of the <div>
?
I’m asking that because I found a post talking about an issue quite similar :
https://github.com/erusev/parsedown/issues/266
Muut
May 26, 2017, 9:48am
8
Oh wow! That is actually working but not a charm for my HTML markup
Muut
May 26, 2017, 9:49am
9
Is there another aproach to fix it on this way?