Generating A-Z lists of links to child pages (of the current page) - Antimatter theme - worked as expected, AT FIRST. Now, suddenly, the p.title property works fine but p.url is garbled in a way that GRAV is unable to follow its own-generated route.
As in:
In a page (typically a page with many children), looping through the child page list in a specified order.
Interestingly I can assign (write) p.url to any other property of the <a> tag, for example a data-url property, and the twig reference to p.url is displayed correctly.
This is my first brush with GRAV. Possibly this stopped working as usual after switching themes briefly from antimatter to quark and back to antimatter again.
Given that my {{ p.url }} links are still intact on my remote/testing site, is it possible that one or the other of these plugin updates could produce this bug?
You are 100% correct. When I ask twig to loop over some page collection and generate <a> links to each child/descendent/post in HTML, twig will commit the {{ p.url }} value to ANY OTHER ATTRIBUTE of the <a> element, such as …data-url=“{{ p.url }}…” HOWEVER NOT THE HREF ATTRIBUTE:
Using a filter like |raw or |urlencode is ignored. I tried twig string interpolation to solve the problem but that too produces the output in HTML shown above.
Is there perhaps some other twig filter I could/should be using to commit a {{ … }} value to the <a href=“…”> attribute?
Again, I came up with the following site-wide workaround for this using js/jQuery, saved to a file I named: repair_urls.js
$(document).ready(function(){
$("a[data-url]").each(function(){
/*
provided all twig loops over any page collection
assign the p.url value to BOTH the href attribute of <a>
as well as an attribute deliberately named: data-url
*/
$(this).click(function(){
let goChild = $(this).data("url");
window.location.assign(goChild);
return false;
})
});
});
To re-enable bastardized links generated by twig that SHOULD point to the URL or route of each page in a collection (BUT DON’T) we simply overwrite/correct the href attribute using the intact value in data-url (once the page is fully loaded).
@boatful, You haven’t provided much context, so I’ve been trying to figure out what’s happening. I can finally reproduce the issue when using the following:
Installed plugin page-toc
Inside page typography.md, I have the following
---
title: Typography
content:
items: '@root.pages'
process:
twig: true
---
[toc]
{% for page in page.collection %}
<a href="{{ page.url }}">{{ page.title }}</a>
{% endfor %}
etc.
The HTML generated by the Twig snippet is as follows:
It’s clearly an issue introduced by plugin page_toc and the issue has been reported before in June 2022 but hasn’t received much attention (yet).
You might consider a separation of concerns and generate the Toc and the url list outside Markdown in their own reusable partial templates. In my view, Markdown in default.md is content, and a list of pages and toc are meta data.
Perhaps using twig_first: true is the remedy. Twig WAS outputting <a href=“%7B%7B%20page.url%20%7D%7D”>…</a> on a page of mine that loops over a collection of pages using:
collection:
items:
taxonomy.type: event
Then, when I added to the frontmatter:
----------
title: Events
twig_first: true
the values ascribed to the href properties in the output reverted to usable/expected/root-relative links:
@boatful, In that case, your use-case (which is still unclear to me) is different from the one I described above, because in my case twig_first: true resulted in anchors wrapped in a code block as follows: