Macros with Twig. post

Today I wrote my first macro for Twig. It's pretty basic:


{% macro linklist(list, url, delimiter) %}
  {% for name,posts in list %}
    {% spaceless %}
        <a href="{{ url }}{{ name|url_encode(true) }}">{{ name }}</a>
          {% if not loop.last %}{{ delimiter|default(", ") }}{% endif %}
    {% endspaceless %}
  {% endfor %}
{% endmacro %}

The way to use it is like this:

  {% import "linklist.html" as linklist %}
  {% set url = "/blog/tags/" %}
  {% set list = data.posts_tags %}
  {{ linklist.linklist(list, url, " ↂ ") }}

To see what it outputs, take a look at my tags and/or categories lists below.

Categories

twigtemplates

Tags

macrosexciting