One of the things that bothered me about the switch to dynamic archives is that I had to stop using the Compare plugin, since it doesn’t support dynamic publishing (or didn’t, at any rate). With the use of Smarty, you can very easily reproduce this functionality, and though I haven’t done any benchmarks or anything, I suspect that the performance gains are dramatic.
For instance, I used to use something like this in my template to conditionally show text around comments:
<MTIfEqual a="[MTEntryCommentCount]" b="0" numeric="1"> No comments yet! </MTIfEqual> <MTIfEqual a="[MTEntryCommentCount]" b="1" numeric="1"> 1 comment! </MTIfEqual> <MTIfGreater a="[MTEntryCommentCount]" b="1" numeric="1"> <$MTEntryCommentCount$> comments! </MTIfGreater>
The same thing can be done with Smarty:
{{capture assign="commentcount"}}<$MTEntryCommentCount$>{{/capture}} {{if $commentcount > 0}} {{if $commentcount > 1}} {{assign var="s" value="s"}} {{else}} {{assign var="s" value=""}} {{/if}} {{$commentcount}} Comment{{$s}}! {{else}} No comments yet! {{/if}}
This first takes the value of MTEntryCommentCount and assigns it to Smarty variable $commentcount (remember the $ in front of the variable when you’re using it to get a value). Then we check the variable, and if it’s 0 or less (this is the else condition), we display the text No Comments yet!
If there are some comments, we then check to see if the number is greater than 1. If it is, we create an “s” to turn Comment into Comments. Then we display the comment count, followed by the appropriate form of the word.
All of this just goes right into your template, no plugins or anything. I’m liking Smarty more and more all the time!
Comments
3 responses to “Even More fun with Smarty”
What a cute site you have here.
I can tell that you have put a lot of time and work into it.
Great job!
france gay grenoble
Thank you for this–this is exactly what I was trying to do on my soon-to-be-relaunched site, and I couldn’t figure out how without the Compare plugin.
What would be even more useful is if Sixapart added ‘assign = ‘ functionality into all of their standard tags (which are, after all, just Smarty tags using a different delimiter set).
That way, you could avoid doing the comparitively expensive buffer-and-capture method used by capture and have something along the lines of:
<$MTEntryCommentCount assign=commentcount$>
{{if $commentcount > 0}}
…etc…