Recently in Templates Category
This article was first published at Movable Tweak.
This little snip of code will dump out a list of all the users and their email addresses on your install in a comma-delimited format that you can easily import into your email client. It’s particularly useful on larger installs:
<mt:Authors include_blogs="all">
<mt:IfNonEmpty tag="AuthorEmail">
<mt:IfNonEmpty tag="AuthorDisplayName">"<mt:AuthorDisplayName />" </mt:IfNonEmpty>
<<mt:AuthorEmail />>,
</mt:IfNonEmpty>
</mt:Authors>
And the Authors tag allows for all sorts of cool filtering attributes so you can get at any group of authors in the system, ordered however you like:
- display_name: Specifies a particular author to select.
- lastn: Limits the selection of authors to the specified number.
- sortby: Supported values: displayname, name, created_on.
- sort_order: Supported values: ascend, descend.
- roles: comma separated list of values. eg “Author, Commenter”
- need_entry: 1 | 0 (default is 1)
- status: Supported values: enabled, disabled. Default is enabled.
Note: Some people have asked about creating an email list of all the commenters on the system, and it’s very simple. The code stays the same, but you just need to specify roles=”Commenter” and need_entry=”0” (since most commenters won’t have written an entry). Here’s the code to do it:
<mt:Authors include_blogs="all" roles="Commenter" need_entry="0" sort_by="display_name"><mt:IfNonEmpty tag="AuthorEmail"><mt:IfNonEmpty tag="AuthorDisplayName">"<mt:AuthorDisplayName />" </mt:IfNonEmpty><<mt:AuthorEmail />>, </mt:IfNonEmpty>
</mt:Authors>
Notice the code is much more compressed than the code given previously. If you tried the previous code, you probably noticed that the spacing is crazy because of all the hard returns and spaces we have in there. This second set of code I posted will give you a highly compact list of email addresses than can literally be copied and pasted into an email client.
Just make sure you use this for good, not evil.
Recently when working on updating the Photo Gallery plugin to work with Movable Type 4.1 I found myself neck deep in some of the most complicated Movable Type templates I have ever seen. Of course, their complexity is exactly why the Photo Gallery plugin outputs such beautiful results.
Within these templates I have the need to take an arbitrary image asset of any size, and scale it proportionally to fit inside a well defined area. This could be a thumbnail, or a larger version of the photo at hand. What I found myself doing is replicating the same template code over and over again in order to produce the output I desired throughout the template set. Here is the template code I was using:
<MTSetVarBlock name="width" trim="1"><MTEntryAssets><MTAssetProperty
property="image_width"></MTEntryAssets></MTSetVarBlock>
<MTSetVarBlock name="height" trim="1"><MTEntryAssets><MTAssetProperty
property="image_height"></MTEntryAssets></MTSetVarBlock>
<mt:if name="width" gt="$height">
<MTSetVarBlock name="img"><MTEntryAssets><mt:AssetThumbnailLink width="90"
regex_replace="/^<a[^>]*>(<img[^>]*>)<\/a>$/","$1" /></MTEntryAssets></MTSetVarBlock>
<mt:else>
<MTSetVarBlock name="img"><MTEntryAssets><mt:AssetThumbnailLink height="90"
regex_replace="/^<a[^>]*>(<img[^>]*>)<\/a>$/","$1" /></MTEntryAssets></MTSetVarBlock>
</mt:if>
Yikes.
To complicate things further, each time I cut and pasted this code around I had to modify it slightly depending upon the size of the photo I wanted to output. This presented the following problems:
- my template code was getting very messy.
- my template code was getting harder and harder to update because I had the same code in multiple places
What I found myself in need of is something a developer might call a "function" or "macro." In layman's terms, I needed a way to write this template code once, and then to invoke along with a few parameters in order to change its behavior and output as needed.
Luckily, Movable Type has exactly what I needed: a little template tag called MTSetVarTemplate. This template tag allowed me to define just such a macro so that I could reduce all of the complicated template code above into something far simpler:
<MTVar name="photo" max_size="90">
Wow, what an improvement!
Okay, so you've created a new blog in MT4. You've followed the system prompts and have picked a style from the available defaults. Now you want to customize the style you've chosen, perhaps use a different font, or a different color for an element. You go to look at the stylesheet template in Design > Templates > Stylesheet, and instead of seeing CSS code to edit, you see this:
Now what?
As communities grow, so does the need to showcase your authors. Having a hefty community of authors is a great thing (content producers FTW!) but everyone wants their fifteen minutes. This handy little tip can help make sure everyone gets their fair share of the limelight.
A couple of things to note first: Make sure that you’ve got author archives set up. You can hack this to work with author profiles, but that’s not the scope of this tutorial. Also, this tutorial assumes that your server is parsing whatever page this is on as PHP. As a rule of thumb, if your file extension is .html, this probably won’t work; if you’re file extension is .php, you should be fine.
<?php
$displayed_authors = array(); // Will hold indexes from $authors for authors already displayed
$show = 25; // How many authors should we show?
<mt:Authors>
$authors[<mt:AuthorId />] = '<li><a href="<mt:EntryLink archive_type="Author" />"><mt:EntryAuthorDisplayName encode_php='1' /></a></li>';
</mt:Authors>
for ($i=1; $i <= $show; $i++) {
$rn = array_rand($authors);
// Loops until it finds an author not displayed
while(in_array($rn, $displayed_authors)) {
$rn = array_rand($authors);
}
array_push($displayed_authors, $rn);
echo $authors[$rn];
}
?>
First, we set up an array where we’ll store all the authors that have already been displayed. More on that in a minute. Then we define how many authors we want to show with this block.
A list of comments, particularly a long list, that doesn’t include any style differentiation can get a little hard to read.
Using two alternating comment styles will give your readers’ eyes a break and your blog a little more style. Happily, Movable Type 4 provides a relatively simple way to do this.
The new templates and template structure in Movable Type 4 streamline and simplify the process of making site wide changes to your blog. Well that’s what they do once you actually understand the templates and template structure. But understanding how the templates work together and digesting the tag soup that swims in each of those templates? Not exactly simple. So at the suggestion of a friend I thought I’d create a series of basic tutorials that help explain the templates and template structure. The tutorials will begin at a relatively basic entry level.
Instead of just breaking down templates line by line I thought it would be easier and more practical to look at templates in the context of actually doing something. You don’t really need to understand every single line of code if you know where and how to make the changes to accomplish what you want. For this first tutorial we’ll be making changes to the sidebar. Specifically we’re going to remove a widget we don’t want and add Google AdSense ads in the sidebar, after the archive content, on every page of a blog.
Over the last few years, the term beta has been somewhat misused. It used to be that when you used beta software, you expected to encounter problems. But when Google started throwing around the term for its offerings, people came to expect that beta software wasn't so bad. Still need an example? Despite Gmail arguably being the leader in web-based email systems, it's had that tag for more than two years.
There are plenty of examples, but hopefully you get the point I'm trying to make - that beta just doesn't mean what it used to. So there really shouldn't be a surprise that people install beta software. It used to be that only a certain crowd would install beta software, but these days, just about everyone will do so, thinking that they can expect to get what they have come to expect. That's not always the case, as sometimes you can get nasty surprises.
A few weeks ago I got a telephone call from my web host letting me know that "one of your Movable Type CGI scripts is using up half the resources of the server and would you please disable the script before we find it necessary to close your account?" Don't you just love news like that? Fortunately, it all got sorted out within a couple of hours; here's the scoop.
If you've had your MT blog for a while, since before version 3.2, you may have upgraded your MT installation, but didn't bother to make changes to the code in your templates. In the templates for one of the earlier versions of MT, if you use Typekey authentication, the Individual Entry Archive Template calls the comments.cgi script to invoke a javascript file that reads back to the commenter their name. Turns out that every time a page displays that includes a Typekey-authenticated comment, the CGI script will run. One of my sites is fairly high traffic, and starting some time last year I was getting over 25,000 requests to the comments.cgi script each day.
This is the line of code that can bring your server to its knees:
The solution is easy. Really easy.
This tutorial is written by LMT author Arvind Satyanarayan of Movalog. Tutorial cross posted on Movalog and LMT.
With the release of Movable Type version 3.2, Six Apart opted to not include the calendar feature that had been included by default in earlier versions. The reason for this is that calendars, with the days linking to blog archives, are resource intensive; they can significantly lengthen rebuild times. Six Apart has posted the code that was used in previous versions of MT. However, many users have found that simply copying and pasting the code as given doesn't yield a properly formatted calendar.
The following code will create a calendar that will be correctly formatted and styled according your stylesheet. Simply copy the code and put it in the sidebar section of your index template. A good location would be just above <div class="module-search module">.
