Here's my code.
<th>Attachment</th>
<td>
<ul>
{% for attachment in lineup.attachments %}
<li><a href='http://files.example.com/{{ attachment.file_url }}'>{{ attachment.name }}</a>
{% endfor %}
</ul>
</td>
This is posting EVERY attachment which is great but I just want it to post the last attachment it finds while iterating through the attachments table. For instance if it finds 10 attachments, I don't want all of them, just the 10th one. Is there anyway to do this?
As of version 1.12.2 Twig contains a "last" filter The syntax is like this:
{{ array|last }}
In your situation it would be:
{{ lineup.attachments|last }}
You can use it like:
{% set attachement = lineup.attachments|last %}
<li>
<a href='http://files.example.com/{{ attachment.file_url }}'>
{{ attachment.name }}
</a>
</li>
You can read all about it here: Twig documentation
To add on to the accepted answer, there is no need to set it as a variable. This works too:
<a href='http://files.example.com/{{ lineup.attachments|last.file_url }}'>
{{ lineup.attachments|last.name }}
</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With