Skip to content

Instantly share code, notes, and snippets.

@narration-sd
Created August 12, 2013 06:17
Show Gist options
  • Save narration-sd/6208607 to your computer and use it in GitHub Desktop.
Save narration-sd/6208607 to your computer and use it in GitHub Desktop.
A cleaner, more knowledge-applying example of building article author attribution lists in Twig/Craft CMS.
<body>
{% spaceless %}
{% macro affiliate (author) %}
{%- if author.affiliation -%}
{{- " - " ~ author.affiliation -}}
{%- endif -%}
{% endmacro %}
{% import _self as macros %}
<h2>Blog Entries with Authors</h2>
{% for article in craft.entries.section('blog').order('postDate asc').find() %}
<h3>{{ article.title -}}</h3>
<p>{{- "by " -}}
{% for author in article.authors %}
{%- if loop.length == 1 -%}
{{- author.anAuthor -}}
{{- macros.affiliate(author) -}}{{- "</p>" -}}
{%- elseif loop.length > 1 and loop.last -%}
{{- "and " ~ author.anAuthor -}}
{{- macros.affiliate(author) -}}{{- "</p>\n" -}}
{%- else -%} {# within list #}
{{- author.anAuthor -}}
{{- macros.affiliate(author) -}}{{- ", " -}}
{%- endif -%}
{% endfor %}
{% endfor %}
{% endspaceless %}
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment