Answers.Body: <p>This can also be done with a Twig macro, rather than creating a plugin:</p>
  
  <pre><code>{% macro ordinal(number) %}
      {%- spaceless %}
      {% if ((number % 100) &gt;= 11) and ((number % 100) &lt;= 13) %}
          {{ number }}th
      {% else %}
          {% set ends = ['th','st','nd','rd','th','th','th','th','th','th'] %}
          {{ number ~ ends[number % 10] }}
      {% endif %}
      {% endspaceless -%}
  {% endmacro %}
  
  {% from _self import ordinal %}
  
  {% for entry in craft.entries %}
      &lt;li&gt;{{ ordinal(loop.index) }}&lt;/li&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-23T23:14:34.470Z
Answers.Text: Thanks Brandon. Is the an advantage/disadvantage to using a macro over a plugin?
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T20:03:07.733Z
Answers.LastActivityDate: 2015-07-17T20:03:07.733Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p>You'd want to write a plugin that added an <code>|ordinal</code> Twig <a href="http://buildwithcraft.com/docs/templating/filters" rel="noreferrer">filter</a>.</p>
  
  <p>Then from your template, you would do: <code>loop.index|ordinal</code>.</p>
  
  <p>The logic in the plugin would look something like:</p>
  
  <pre><code>public function ordinal($number)
  {
      $ends = array('th','st','nd','rd','th','th','th','th','th','th');
  
      if ((($number % 100) &gt;= 11) &amp;&amp; (($number % 100) &lt;= 13))
      {
          return $number.'th';
      }
      else
      {
          return $number.$ends[$number % 10];
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T18:15:04.287Z
Answers.Text: For the record, I had to Google what an ordinal suffix was.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T18:35:00.243Z
Answers.Text: So did I Brad...
Answers.UserDisplayName: Peter Tell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T19:24:01.293Z
Answers.Text: Sorry about that. What should I have called it?
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T19:24:54.690Z
Answers.Text: I'm sure that's the right word... just got to learn something new is all. :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T19:25:18.520Z
Answers.Text: And thanks Brad, I'll try find some time figuring out how to add a plugin :)
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T19:26:36.357Z
Answers.Text: Search for filter here: https://straightupcraft.com/craft-plugins and you'll find a bunch of plugins to get you started.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T12:52:56.487Z
Answers.Text: I've finally gotten around to creating [a plugin](https://github.com/katrinkerber/craft-ordinalize). Thank you for your help.
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-17T18:14:39.113Z
Answers.LastActivityDate: 2015-07-17T19:25:08.920Z
Answers.LastEditDate: 2015-07-17T19:25:08.920Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'd like to add an ordinal suffix to the <code>loop.index</code> variable within a <code>for</code> loop.</p>
  
  <p>For example:</p>
  
  <pre><code>1st
  2nd
  3rd
  </code></pre>
  
Tags: <templating><forloop>
Title: Add ordinal suffix to loop.index
ThreadText: # Add ordinal suffix to loop.index
  
  (Asked by: username_0 on 2015-07-17)
  
  I'd like to add an ordinal suffix to the `loop.index` variable within a `for` loop.
  
  For example:
  
  ```
  1st
  2nd
  3rd
  
  ```
  
  **Tags:** templating, forloop
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-07-17. Score: 5 | Accepted answer
  
  You'd want to write a plugin that added an `|ordinal` Twig [filter](http://buildwithcraft.com/docs/templating/filters).
  
  Then from your template, you would do: `loop.index|ordinal`.
  
  The logic in the plugin would look something like:
  
  ```
  public function ordinal($number)
  {
      $ends = array('th','st','nd','rd','th','th','th','th','th','th');
  
      if ((($number % 100) >= 11) && (($number % 100) <= 13))
      {
          return $number.'th';
      }
      else
      {
          return $number.$ends[$number % 10];
      }
  }
  
  ```
  
  > **Comments:**
  > **username_2**: For the record, I had to Google what an ordinal suffix was.  
  > **username_3**: So did I username_2...  
  > **username_0**: Sorry about that. What should I have called it?  
  > **username_2**: I'm sure that's the right word... just got to learn something new is all. :)  
  > **username_0**: And thanks username_2, I'll try find some time figuring out how to add a plugin :)  
  > **username_2**: Search for filter here: https://straightupcraft.com/craft-plugins and you'll find a bunch of plugins to get you started.  
  > **username_0**: I've finally gotten around to creating [a plugin](https://github.com/katrinkerber/craft-ordinalize). Thank you for your help.  
  
  ---
  
  ### Answer by username_1 on 2015-07-17. Score: 7
  
  This can also be done with a Twig macro, rather than creating a plugin:
  
  ```
  {% macro ordinal(number) %}
      {%- spaceless %}
      {% if ((number % 100) >= 11) and ((number % 100) <= 13) %}
          {{ number }}th
      {% else %}
          {% set ends = ['th','st','nd','rd','th','th','th','th','th','th'] %}
          {{ number ~ ends[number % 10] }}
      {% endif %}
      {% endspaceless -%}
  {% endmacro %}
  
  {% from _self import ordinal %}
  
  {% for entry in craft.entries %}
      <li>{{ ordinal(loop.index) }}</li>
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_1. Is the an advantage/disadvantage to using a macro over a plugin?  
  
  ---
  
  