Dynamic Template plug-in for Windows Live Writer
(Right-click and save to your WLW plugins directory)
A note about security: Like WLW plug-ins, templates are powerful and a malicious template could easily damage your system! Don’t use templates from anyone you don’t trust!
Dynamic Template is a plug-in that makes it easier to insert common snippets of HTML or text into your Windows Live Writer posts.
Templates can be as simple as literally the same exact text every time (for example, “Powered by Windows Live Writer”). Or they can get slightly fancier by prompting for input whenever the template is inserted.
Example 1: Hello World
If you enter “happy” and hit OK, the string “Hello world, I am happy today.” is inserted wherever the cursor is.
Here’s what the template for the above looks like:
The grid above describes the values that the user will be prompted for whenever the template is inserted. Variable Name is the name that can be used to identify the variable from within the template body. Data Type lets you choose what kind of values the user can enter, and also controls what the type of the variable will be. The Label is the label that the user will see.
The Template Body is just HTML, with some additional features. As you can see here, you can use <%= expression %> to include the value of a variable.
Non-programmers can probably stop here. Just using what’s described above, you already have a fair amount of possibilities in terms of templates you can cook up.
Example 2: Technorati Tags
Dynamic Template is more powerful and flexible than many template (or “HTML macro”) systems, because its templates can be written in C#. If you saw the example above and thought “Hmmm, that looks like JSP/ASP,” you’re right. The templating language used here is much like JSP/ASP: it consists of C# expressions and statements embedded into HTML.
For our next example, let’s consider Technorati tags. Windows Live Writer has a built-in “Insert Tags” feature, but lots of folks like to insert their Technorati tags in a style where multiple words are joined by plus instead of space, for example:
Technorati Tags: windows+live+writer
This isn’t possible with the built-in tagging feature, which produces:
Technorati Tags: windows live writer
You can customize the format for the built-in tagging feature, but not in a way that will get you the words separated with plus. (It just occurred to me that you could just type the multi-word tag with + instead of space. But let’s say you didn’t want to do that.)
Here’s what the template would look like:
Notice that statement blocks are surrounded by <% and %>.
The input is split linewise to come up with a list of tags. Each tag is URL-encoded for the tagging URL, and has space replaced with plus for display purposes. As you can see, you can use arbitrary C# code (although you are currently limited to using classes in the mscorlib, System, and System.Web assemblies).
In the Technorati example above, a function called UrlEncode is used–that’s one of several built-in functions available to templates. The complete list follows:
These pass straight through to the corresponding methods on HttpUtility.
There’s also one built-in string variable, _content. 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).