Also available in: 日本
Download Now
from CodePlex

Dynamic Template is a powerful plugin for Windows Live Writer. You can use templates to easily insert oft-used snippets of HTML or text.

Or you can write templates that are powerful enough to be considered "mini-plugins" in their own right, while being radically simpler to write and distribute than traditional Windows Live Writer plugins. If you have ever written a PHP, JSP, or ASP page, you already have a good idea of how to write these kind of templates!

For example?

Level 1 - Plain HTML: Let's start with the simplest possible scenario: you just want to insert the same snippet of HTML into your posts on a regular basis, and you don't want to have to leave Writer to do it. For this example, imagine you want to "Powered by Windows Live Writer" at the bottom of your posts. Watch Example 1. (All videos linked from this page are very short Flash movies, created using Jing.)

Level 2 - HTML + Code: OK, that's useful, but it's been done before. What sets my plugin apart is that dynamic templates can contain snippets of C# code, like an ASP page! For this next example, imagine you're an Engadget blogger, liveblogging the latest Steve Jobs keynote (like this one). Every second counts, and the time spent inserting the little timestamp is time wasted. Let's write a template to automate that. Watch Example 2.

Level 3 - Using Selection: It's much easier to come up with useful scenarios once we introduce the ability to access what's currently selected in Windows Live Writer. Just use the built-in variable _selection in your template. This is incredibly useful for adding HTML formatting that isn't included with Windows Live Writer out of the box--for example, small caps. Watch Example 3. Or here's a cool idea: take whatever text you've entered into Writer, and treat it as HTML. Watch Example 4.

Level 4 - Taking Input: Alright, that's cool, but what really makes things interesting is when you start asking the user for information. How about a template for highlighting text in a color of the user's choice? Watch Example 5.

As you can see in Example 5, to ask for user input you just need to fill out a row in the Template Variables grid. The Variable Name is the name you will use to reference the value in your template—it must be a valid C# identifier. The Data Type affects both the type of that variable (Integer will be an int, Boolean will be a bool, etc.) and the appearance of the input dialog. The Label is the "friendly name" that will be displayed to the user in the input dialog.

Basic Syntax

Templates are primarily composed of HTML. You can embed C# statements using <% and %>. You can output C# expressions using <%= and %>. For example:

<% string currentTime = DateTime.Now.ToString("HH:mm:ss"); %>
<b><%= currentTime.ToUpper() %></b>

Note that expressions are not terminated with a semicolon, as that would make them a statement. Conversely, statements must be terminated with semicolon. If you get a confusing compile error when you try to save your template (like "CS1026: ) expected"), check for this first.

Built-in Functions and Variables

In Example 4 above, a function called HtmlDecode is used. That’s one of several built-in functions available to templates. The complete list follows:

  • HtmlEncode(string)
  • HtmlAttributeEncode(string)
  • HtmlDecode(string)
  • UrlEncode(string)
  • UrlPathEncode(string)
  • UrlDecode(string)

These pass straight through to the corresponding methods on HttpUtility.

There’s also one built-in string variable, _selection. If the user has anything selected when the template is inserted, the HTML of the selection will be assigned to this variable (requires Windows Live Writer Beta 2 or later).

Sharing Templates

Templates are stored as .wlwtemplate files, in your Application Data\WLWTemplates directory. You can e-mail these files to anyone who has the plugin installed—simply double-clicking the file will bring up a template installation prompt.

You can see the same prompt yourself by clicking on any of the samples below.

Samples

If you have the plugin installed, clicking on a link below will install the template.

Abbreviation: Creates an abbreviation that shows a definition on mouse hover. Example: WLW
HTMLize: Takes selected text and HTML-decodes it (as seen in the Example 4 video).
Repeat: Repeats a string multiple times.