Question

I want to make a index page (like you see one in beginning of dictionaries) with MTTags from a blog using MT5.1. There might be some jQuery solutions but I would love to accomplish this with Movable type tags. Here is what I have so far.

<ul>
    <mt:Tags sort_by="Name">
      <li><mt:TagName></li>
    </mt:Tags>
</ul>

I would like the result be like below:

A

- Apple
- apricot

B

- bee

C

- Cake
- Cinnamon

D

- Dog
- Dragon
Was it helpful?

Solution

First we need to isolate the first character:

<$mt:TagName regex_replace="/(?<=.).*$/","" $>

(that is a zero-width positive look-behind assertion) but we want it as capital letter, and to save it to a variable:

<$mt:TagName regex_replace="/(?<=.).*$/","" upper_case="1" setvar="current_index" $>

Now we only need to compare it to the last index, to see if we need to output the index header:

<mt:Tags sort_by="Name">

  <$mt:TagName regex_replace="/(?<=.).*$/","" upper_case="1" setvar="current_index" $>
  <mt:unless name="last_index">
     # this is the first time
  <mt:else name="current_index" ne="last_index">
     # need to output the new index
  </mt:unless>
  <mt:var name="current_index" setvar="last_index">

  <li><mt:TagName></li>

</mt:Tags>

<mt:if name="last_index">
    # close the list
</mt:if>

The html tags are left to the reader. :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top