Displaying a Post Differently in Movable Type Depending on Asset Count

| No Comments | No TrackBacks |
User Xtencil on the Movable Type Forum asks how he can show an entry in a different way depending on the number of assets present in the entry.  For example, if there are no images vs. if there are one or more.  He already attempted to use the EntryAssetsCount tag, which failed because this tag does not exist.  But never fear...
... with some template trickery we can go a long way, as usual:

<mt:var name="thecount" value="0">
   <mt:setvarblock name="thecount"><mt:var name="__counter__"></mt:setvarblock>
<mt:if name="thecount" gt="1">
<li>Entry <mt:entryidhas more than one asset (<mt:var name="thecount">)</li>
<li>Entry <mt:entryidhas one or less assets (<mt:var name="thecount">)</li>

This bit of code will produce following output:

  • Entry 76 has one or less assets (1)
  • Entry 71 has more than one asset (6)
  • Entry 75 has one or less assets (1)
  • Entry 74 has one or less assets (1)
  • Entry 73 has one or less assets (1)
  • Entry 70 has more than one asset (6)
  • Entry 69 has one or less assets (1)
  • Entry 68 has one or less assets (1)
  • Entry 65 has more than one asset (4)
  • Entry 64 has more than one asset (2)
As you can see, you can play with the number in the 'gt' argument, so you can test for any arbitrary number of assets.

If your goal is simply to display the first asset for each post, if one is available, then use this code:

<mt:if name="__counter__" lt="2">
<li>First asset of entry <mt:entryidis <mt:asseturl></li>

Both snippets use the '__counter__' variable which is available inside almost any loop in MT and which indicates the number of times we have entered he loop so far.  If it is less than two, it means we are going through the loop for the first time. The result would look like this:

  • First asset of entry 76 is : http://www.movabletips.com/index.png
  • First asset of entry 71 is : http://www.movabletips.com/off.png
  • First asset of entry 75 is : http://www.movabletips.com/cup.gif
  • First asset of entry 74 is : http://www.movabletips.com/france.jpg
  • First asset of entry 73 is : http://www.movabletips.com/euroflag.png
  • First asset of entry 70 is : http://www.movabletips.com/ignore.jpg
  • First asset of entry 69 is : http://www.movabletips.com/cashregister.jpg
  • First asset of entry 68 is : http://www.movabletips.com/bug-pbmt-white.png
  • First asset of entry 65 is : http://www.movabletips.com/Cloud.png
  • First asset of entry 64 is : http://www.movabletips.com/moneybag.jpg
Note: if there is no asset available for a particular entry, the EntryAssets loop will not display anything.

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/67

Leave a comment