<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
   <title>Posts by Byrne Reese on Learning Movable Type</title>
   <link rel="alternate" type="text/html" href="http://www.learningmovabletype.com/" />
   <link rel="self" type="application/atom+xml" href="http://www.learningmovabletype.com/contributors/byrnereese/" />
   <id>tag:,2008-02-25:/5</id>
   <updated>2008-02-15T23:01:50Z</updated>
   <subtitle>Tutorials and helpful tips for the Movable Type web publishing system</subtitle>
   <generator uri="http://www.movabletype.org/">Movable Type Publishing Platform 4.01</generator>

<entry>
   <title>Template Macros: The coolest template trick you don&apos;t know about</title>
   <link rel="alternate" type="text/html" href="http://www.learningmovabletype.com/a/template_macros_the_coolest_template_trick_you_don/" />
   <id>tag:www.learningmovabletype.com,2008://5.2083</id>
   
   <published>2008-02-15T19:42:22Z</published>
   <updated>2008-02-15T23:01:50Z</updated>
   
   <summary>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...</summary>
   <author>
      <name>Byrne Reese</name>
      <uri>http://www.majordojo.com</uri>
   </author>
   
      <category term="Tags" scheme="http://www.sixapart.com/ns/types#category" />
   
      <category term="Templates" scheme="http://www.sixapart.com/ns/types#category" />
   
   <category term="macros" label="macros" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="mtsetvartemplate" label="MTSetVarTemplate" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="templatetags" label="template tags" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="templates" label="templates" scheme="http://www.sixapart.com/ns/types#tag" />
   
   <content type="html" xml:lang="en" xml:base="http://www.learningmovabletype.com/">
      <![CDATA[<p>Recently when working on updating the <a href="http://www.majordojo.com/2008/02/stop-design-photo-gallery-plugin-for-movable-type-41.php">Photo Gallery plugin</a> 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 <a href="http://www.majordojo.com/photos/">results</a>.</p>

<p>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:</p>

<pre><code>&lt;MTSetVarBlock name="width" trim="1"&gt;&lt;MTEntryAssets&gt;&lt;MTAssetProperty 
    property="image_width"&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;MTSetVarBlock name="height" trim="1"&gt;&lt;MTEntryAssets&gt;&lt;MTAssetProperty 
    property="image_height"&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;mt:if name="width" gt="$height"&gt;
    &lt;MTSetVarBlock name="img"&gt;&lt;MTEntryAssets&gt;&lt;mt:AssetThumbnailLink width="90" 
        regex_replace="/^&lt;a[^&gt;]*&gt;(&lt;img[^&gt;]*&gt;)&lt;\/a&gt;$/","$1" /&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;mt:else&gt;
    &lt;MTSetVarBlock name="img"&gt;&lt;MTEntryAssets&gt;&lt;mt:AssetThumbnailLink height="90" 
        regex_replace="/^&lt;a[^&gt;]*&gt;(&lt;img[^&gt;]*&gt;)&lt;\/a&gt;$/","$1" /&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;/mt:if&gt;
</code></pre>

<p>Yikes.</p>

<p>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:</p>

<ul>
<li>my template code was getting <em>very</em> messy.</li>
<li>my template code was getting harder and harder to update because I had the same code in multiple places</li>
</ul>

<p>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.</p>

<p>Luckily, Movable Type has exactly what I needed: a little template tag called <a href="http://www.movabletype.org/documentation/appendices/tags/setvartemplate.html"><code>MTSetVarTemplate</code></a>. 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 <em>far</em> simpler:</p>

<pre><code>&lt;MTVar name="photo" max_size="90"&gt;
</code></pre>

<p>Wow, what an improvement!</p>
]]>
      <![CDATA[<p>To achieve this I defined the following template code inside of a template module I created. I then included that module in my header to ensure that I could use it anywhere I needed within my templates I needed to:</p>

<pre><code>&lt;mt:ignore&gt;
  This template module is responsible for outputting the associated image with the current
  photo/entry in context. You can pass in the max_size parameter to control the size of 
  the image.
  Example:
    &lt;mt:getvar name="photo" max_size="90"&gt;
&lt;/mt:ignore&gt;
&lt;MTSetVarTemplate name="photo"&gt;
&lt;MTSetVarBlock name="width" trim="1"&gt;&lt;MTEntryAssets&gt;&lt;MTAssetProperty property="image_width"&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;MTSetVarBlock name="height" trim="1"&gt;&lt;MTEntryAssets&gt;&lt;MTAssetProperty property="image_height"&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;mt:if name="width" gt="$height"&gt;
        &lt;MTSetVarBlock name="img"&gt;&lt;MTEntryAssets&gt;&lt;mt:AssetThumbnailLink width="$max_size" regex_replace="/^&lt;a[^&gt;]*&gt;(&lt;img[^&gt;]*&gt;)&lt;\/a&gt;$/","$1" /&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;mt:else&gt;
        &lt;MTSetVarBlock name="img"&gt;&lt;MTEntryAssets&gt;&lt;mt:AssetThumbnailLink height="$max_size" regex_replace="/^&lt;a[^&gt;]*&gt;(&lt;img[^&gt;]*&gt;)&lt;\/a&gt;$/","$1" /&gt;&lt;/MTEntryAssets&gt;&lt;/MTSetVarBlock&gt;
&lt;/mt:if&gt;
&lt;mt:getvar name="img" trim="1"&gt;
&lt;/MTSetVarTemplate&gt;
</code></pre>

<p>This is still a lot to chew on, so let me break down exactly what is going on:</p>

<ul>
<li>First, I use the <code>&lt;mt:ignore&gt;</code> tag to document what I had done and allow others to more easily re-use the module I created.</li>
<li>I define the name of the macro by using the <code>name</code> attribute on the <code>&lt;mt:setvartemplate&gt;</code> tag.</li>
<li>I invoke the macro, by using the <code>&lt;mt:getvar&gt;</code> tag.</li>
<li>I am able to access parameters I pass into the macro using a common programming syntax, in this case using the <code>$max_size</code> variable.</li>
<li>I am able to pass parameters into the macro by including them in my <code>&lt;mt:getvar&gt;</code> tag like any other attribute. For example (notice the <code>max_size</code> attribute corresponding to the variable of the same name inside of my macro):
<mt:var name="photo" max_size="90"></li>
</ul>

<p>I can't tell you how pleasantly surprised to find this feature. Using it greatly improved the readability of my templates, and also made them much each to maintain over time. And now looking back i can think of a million places through out my templates on every site I have ever worked on where this could really help make my template simpler and easier to maintain.</p>
]]>
   </content>
</entry>

<entry>
   <title>Running Publish Queue under daemontools</title>
   <link rel="alternate" type="text/html" href="http://www.learningmovabletype.com/a/running_publish_queue_under_daemontools/" />
   <id>tag:www.learningmovabletype.com,2008://5.2046</id>
   
   <published>2008-01-27T22:05:45Z</published>
   <updated>2008-01-27T23:01:42Z</updated>
   
   <summary>In Movable Type 4.0 users had the option of offloading the task of publishing to a separate program or application called Publish Queue. This had the advantage of dramatically increasing Movable Type&apos;s performance and reliability. Most users run Pubish Queue via a scheduled task that wakes up at a fixed...</summary>
   <author>
      <name>Byrne Reese</name>
      <uri>http://www.majordojo.com</uri>
   </author>
   
      <category term="General Tips and Tricks" scheme="http://www.sixapart.com/ns/types#category" />
   
      <category term="Servers" scheme="http://www.sixapart.com/ns/types#category" />
   
   <category term="daemontools" label="Daemontools" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="publishqueue" label="Publish Queue" scheme="http://www.sixapart.com/ns/types#tag" />
   
   <content type="html" xml:lang="en" xml:base="http://www.learningmovabletype.com/">
      <![CDATA[<p>In Movable Type 4.0 users had the option of offloading the task of publishing to a separate program or application called <a href="http://www.movabletype.org/documentation/administrator/publishing/publish-queue.html">Publish Queue</a>. This had the advantage of dramatically increasing Movable Type's performance and reliability. </p>

<p>Most users run Pubish Queue via a scheduled task that wakes up at a fixed internal, executes and then quits. However, some users use Publish Queue's "daemon mode" to make it so that it is always running. But its own daemon mode does not have the capability to monitor and restart itself should it unexpectedly quit, exit or die.</p>

<p>Should Publish Queue crash the consequence would be that publishing would simply stop without you ever really knowing about it. Plus you couldn't start it up again without you manually going into the server and starting the publish queue up again. This rarely happens in actuality, but in a business critical application, you <em>never want this to happen. Period.</em></p>

<p>Of course this problem is not unique to Movable Type. Many programs in Linux should be running at all times to ensure that the operating system functions properly. That is why there exists a suite of utilities in Linux called "damon tools." Together these tools can be configured to monitor <em>any</em> script and ensure that it is always running. If the script/application dies, then daemon tools will restart it. Handy.</p>

<p>This article discussed the process for those unfamiliar with daemon tools with how to get started and how to configure it so that you can relax knowing that Movable Type's background publishing engine will never stop running.</p>
]]>
      <![CDATA[<p>Ok, before we get started I have to admit something: this article is a little technical. <em>cough</em>. It requires an above average knowledge of Linux and requires people to interact with a command line that many are not comfortable doing. If you are one of those people then your key take away from this article is the knowledge that a solution exists to this problem. Your job will then be to send a link to this article to your system administrator or a more technical friend or yours and tell them, "help me do <em>this</em>."</p>

<p>For all the technical folks who are sticking around, this article will show you how to start up Publish Queue and have it monitored by <a href="http://www.daemon-tools.cc/dtcc/announcements.php">daemon tools</a> so that it will automatically be restarted (or "respawned" to use the proper term) if the process for whatever reason dies.</p>

<p><strong>Prerequisites</strong></p>

<p>You will need each of the following:</p>

<ul>
<li>Movable Type 4.0 or greater</li>
<li>daemon tools, a Linux package</li>
</ul>

<p>The simplest way to install daemon tools is via yum or apt. For example, running the following command as root should do the trick:</p>

<pre><code>yum install daemontools
</code></pre>

<p>If this doesn't work or is not appropriate for you Linux distribution, then I must refer you to the daemon tools website to follow their more <a href="http://www.daemon-help.com/index.php?id=installation_lite">specific instructions</a>.</p>

<p><strong>Setting up the Publish Queue service</strong></p>

<p>Once daemon tools is installed you need to create a script that will be run anytime the system wants to start or respawn Publish Queue. This is done by creating a few directories and files on your system. Execute the following command:</p>

<blockquote>
  <p>mkdir /etc/publishqueue</p>
</blockquote>

<p>Then create a file called <code>/etc/publishqueue/run</code> and enter the following contents into the file:</p>

<pre><code>#!/bin/sh
MT_HOME=/var/www/cgi-bin/mt
cd $MT_HOME
exec ./tools/run-periodic-tasks -daemon
</code></pre>

<p>Change the value of <code>MT_HOME</code> to be the location of the directory that contains <code>mt.cgi</code>.</p>

<p>Finally, execute the following command:</p>

<pre><code>ln -s /etc/publishqueue /service/publishqueue
</code></pre>

<p>That will setup daemon tools with the correct scripts and handlers to run Publish Queue.</p>

<p><strong>Setting up svscan</strong></p>

<p>A program comes with daemon tools called <code>svscan</code>. It is responsible for monitoring a list of services and if any of them stop running to restart them. In lieu of my documenting the process, let me refer you to a more <a href="http://www.thedjbway.org/svscanboot.html">complete and definitive article that can guide you through this process</a>. There are lots of ways to setup svscan, but I chose to use inittab as the means by which my system will monitor and respawn processes.</p>

<p>Once you have successfully edited <code>/etc/inittab</code> you can execute the following command to complete the entire daemonization process:</p>

<blockquote>
  <p>telinit q</p>
</blockquote>

<p>This command will instruct your system to reprocess <code>/etc/inittab</code> and your publish queue daemon should then start momentarily. Run the following command and see if you can see publish queue running:</p>

<blockquote>
  <p>ps -aef</p>
</blockquote>

<p>You should see something like:</p>

<pre><code>root 30663     1  0 Jan21 ? /bin/sh /usr/local/bin/svscan-start
root 30666 30663  0 Jan21 ? /bin/sh /usr/local/bin/svscan-start
root 30667 30666  0 Jan21 ? svscan /service
root 30669 30667  0 Jan21 ? supervise publishqueue
root 26564 30669  0 04:00 ? /usr/bin/perl -w run-periodic-tasks -daemon
</code></pre>

<p><strong>Summary</strong></p>

<p>And that as they say, is that. I know these instructions can be an intimidating, and hopefully in the future we can find ways to automate this process. In the meantime, I would really like to hear how others have solved this problem. </p>

<p>Also, if you have any questions or problems following the steps above, please leave a comment and I will see if I can't help you resolve your problem.</p>
]]>
   </content>
</entry>

<entry>
   <title>Date Based Category Archives in Movable Type</title>
   <link rel="alternate" type="text/html" href="http://www.learningmovabletype.com/a/001871date_based_category_archives/" />
   <id>tag:www.learningmovabletype.com,2007://10.1871</id>
   
   <published>2007-03-30T22:07:02Z</published>
   <updated>2007-06-26T05:36:58Z</updated>
   
   <summary>Originally published at majordojo under the title &quot;How to increase advertising revenue with Movable Type.&quot; Every once in a while a plugin is created for Movable Type that makes you wonder, “why hasn’t anyone created this until now?” And the most recent plugin that I have installed on majordojo is...</summary>
   <author>
      <name>Byrne Reese</name>
      <uri>http://www.majordojo.com</uri>
   </author>
   
      <category term="Categories" scheme="http://www.sixapart.com/ns/types#category" />
   
      <category term="Plugins" scheme="http://www.sixapart.com/ns/types#category" />
   
   <category term="archives" label="Archives" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="catcalendar" label="Cat Calendar" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="categories" label="Categories" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="plugins" label="Plugins" scheme="http://www.sixapart.com/ns/types#tag" />
   
   <content type="html" xml:lang="en" xml:base="http://www.learningmovabletype.com/">
      <![CDATA[<p><em>Originally published at <a href="http://www.majordojo.com">majordojo</a> under the title "<a href="http://www.majordojo.com/movable_type/how_to_increase_advertising_revenue_with_movable_type.php">How to increase advertising revenue with Movable Type</a>."</em></p>

<p><img alt="cute-kitten-picture-in-the-grass.jpg" src="http://www.learningmovabletype.com/files/cute-kitten-picture-in-the-grass-thumb.jpg" class="floatimgright" />Every once in a while a plugin is created for <a href="http://www.sixapart.com/movabletype/">Movable Type</a> that makes you wonder, “why hasn’t anyone created this until now?” And the most recent plugin that I have installed on majordojo is no exception. <a href="http://yacomink.com">Andy Yako-Mink</a>, inspired by a ProNet conference call, created a plugin named “<a href="http://yacomink.com/CatCalendar/">Cat Calendar</a>.” But before you start imagining a 365 Cat-a-Day desk calendar for your blog, let me dispel any preconception you might have about this plugin. Simply put, <b>Cat Calendar paginates Category Archive pages by date</b>.</p>

<p>But why do I think this is so significant? </p>

<p>First,  one of the consequences of Movable Type’s archiving system is that over time as you accumulate more and more content (which for a product that has been around for 5+ years is not completely unlikely) rebuilding specific pages becomes a very expensive operation. Category archives in particular because over time they only get larger. Rebuilding a single Category Archive page for example requires Movable Type to load a large number of entries into memory to be published to a single file. This results in a large memory foot print for Movable Type during the rebuild process; and for others that host more popular blogs it becomes a point of instability. </p>
]]>
      <![CDATA[<p>By paginating Category Archives, Movable Type’s publishing process not only becomes more stable, but it also gets a nice performance boost as well because the amount of content that gets rebuild at any given time is significantly reduced. Think about it: do you ever really need to rebuild entries from November 2005 in February 2007? Most of the time, no. Cat Calendar optimizes Movable Type’s publishing process to publish only what it needs to. </p>

<p>There is another benefit of Cat Calendar that cannot be overlooked: more page turns. By breaking really large category archive pages into multiple pages, you create greater potential for readers to browse multiple pages of your site by encouraging them to explore more posts within a category. As they browse and explore your blog, they generate more page turns, which if you are familiar with advertising is directly related to the earning potential of your blog; no mater which way you cut it, greater page volume results in higher ad impressions which in turn results in higher advertising revenue.</p>

<p>Installing Cat Calendar is easy and Andy has good instructions on how to do so. In fact, it is refreshing to see a plugin integrate so cleanly and completely into the Movable Type user experience. I encourage all Movable Type users: <a href="http://yacomink.com/CatCalendar/">get Cat Calendar now</a>, and throw some scratch Andy’s way. He deserves it.</p>

<ul>
<li><a href="http://www.majordojo.com/uploads/category_archive.txt">Byrne’s Category Archive Template</a></li>
<li><a href="http://www.majordojo.com/uploads/date-based-archive.txt">Byrne’s Date-Based Category Archive Template</a></li>
</ul>
]]>
   </content>
</entry>

</feed>

