Question

I'm trying to get the Entry Summary of my movable type blog to display an image with the title formatted a certain way over the image (The image is tagged if it will be used for this purpose), and display the title another way if there is no image. However, I cannot get the else statement to work with MTEntryAsset as it should. If there is no EntryAssets tagged "homepage" with the type "image" it does nothing.

<mt:entryassets tag="homepage" type="image" limit="1"> 
     <div class="image_entrie_header"><img src="<mt:assetthumbnailurl>"></div>
     <h3 class="blog_entry_headingimg"><a href="<mtentrypermalink>"><mt:entryTitle></a></h3>
<mt:else>
     <h3 class="blog_entry_heading"><a href="<mtentrypermalink>"><mt:entryTitle></a></h3>
</mt:entryassets>
Was it helpful?

Solution

I do not believe the EntryAssets tag supports <mt:Else>, but you can achieve this with a little Movable Type logic.

With some simple tags, you would be able to do something like this:

<mt:If tag="EntryCategory">
  <p>Filed under <$mt:EntryCategory$></p>
<mt:Else>
  <p>Uncategorized</p>
</mt:If>

But since your block tag, EntryAssets, is a complicated tag taking several template tag modifiers, one of which is also called tag, this won't work. We can fake it using Movable Type variables like this:

<$mt:Var name="asset_found" value="0"$>
<mt:EntryAssets tag="homepage" type="image" limit="1">
  <$mt:Var name="asset_found" value="1"$>
  Your code here
</mt:EntryAssets>
<mt:Unless name="asset_found">
  Show this if none found
</mt:Unless>

Note the first line where we set the variable asset_found to 0 is not strictly required by Movable Type, but it's good to reset the variable in case you use this block in multiple places in the same template.

OTHER TIPS

Charlie's solution is a fine one; but let me present an alternate: the Extra Tags plugin has a tag called EntryHasAssets. The benefit of this tag over Charlie's solution is that the templating is a little leaner and perhaps easier to read.

<mt:EntryHasAssets>
    <mt:entryassets tag="homepage" type="image" limit="1"> 
        <div class="image_entrie_header"><img src="<mt:assetthumbnailurl>"></div>
       <h3 class="blog_entry_headingimg"><a href="<mtentrypermalink>"><mt:entryTitle></a></h3>
    </mt:entryassets>
<mt:Else>
    <h3 class="blog_entry_heading"><a href="<mtentrypermalink>"><mt:entryTitle></a></h3>
</mt:EntryHasAssets>

I haven't done any performance testing: I don't know if Charlie's native solution is faster or slower than this plugin's tag. Adding a plugin does add overhead, though for a well-written and small plugin like ExtraTags it's minimal. I would probably just use the native solution unless I also had use of another tag in this plugin, just to keep things clean.

mt:Else is used with mt:If and mt:Unless. It isn't defined as the condition when enclosing tags such as mt:Entries, mt:Comments, mt:Categories have no results.

The solution that Charlie suggested is how we code the absence of entry assets in templates that support the SuperAssets series of plugins.

The reason why your solution didn't work is because the mt:Else tag that you've used there doesn't work as a negation of the 'mt:entryassets' tag.

The solution that Charlie outlined above works very well.

Dan's plugin works excellent with the template sample that he gave you. What you may like to know is that Dan's plugin is a lot smarter and could allow you to do much more evolved jobs.

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