Skip to content

Instantly share code, notes, and snippets.

@narration-sd
Created August 11, 2013 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narration-sd/6203750 to your computer and use it in GitHub Desktop.
Save narration-sd/6203750 to your computer and use it in GitHub Desktop.
Achieving a list of authors in CraftCMS/Twig
Output:
Blog Entries with Authors
First Article
by F. Scott Fitzgerald - Ancestral, and James Michener - Dell Pocket Books
Second Article
by John Fowles - Oxford University, 이오령 - 이회여자대학교, and 김덕종 - KAIST Materials Science
Third Article and Counting
by Walter Fontana - Harvard Medical School
Code:
{% 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() -%}
{{- "\n" -}}{{ article.title -}}{{- "\n<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>" -}}
{%- else -%} {# within list #}
{{- author.anAuthor -}}
{{- macros.affiliate(author) -}}{{- ", " -}}
{%- endif -%}
{% endfor %}
{%- if not loop.last -%}
{{- "<br />" -}}
{%- endif -%}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment