Creating Macros for dasBlog

From version 1.8 dasBlog of it supports custom macros through a plugin framework. I have released a set of plugins called dasBlog Extra Macros. I am also working on a few more macros. But in general there are not many who are using this feature. There are just about 100 downloads so far. I think not many know about of this feature and there is not much documentation on how to build macros. I will try to write a brief how to regarding this. If it comes out well I will add it to dasBlog’s documentation.

Create a “Class Library” project in Visual Studio.Net. Add a class to this project. Let us call the class HelloWorldMacros and use the namespace HelloWorldPlugin. Now add a constructor to the class with the SharedBasePage and Entry as parameters.

SharedBasePage is a class in the assembly newtelligence.DasBlog.Web.Core and Entry is in newtelligence.DasBlog.Runtime. The SharedBasePage provides a lot of useful information. You can go through the code or use Reflector to figure out how to get the imformation you need.

If the macro is present in the itemTemplate.blogtemplate template then the current blog post being rendered is passed as Entry otherwise this is null. So it is always better to check for null before using the Entry parameter.

Here is how the constructor would look like:

public HelloWorldMacros(SharedBasePage page, Entry item)
    requestPage = page;
    currentItem = item;

A single class can have as many macros as you want. Let us create a macro called HelloWorld. This will accept a string as input. The output will be “Hello World” followed by the input. The following method defines the macro:

//The Hello World Macro
public virtual Control HelloWorld(string name)
   return new LiteralControl(“Hello World! “ + name);

As you can see this is just a virtual method returning a Control. It is just that simple. You can use the two parameters passed to the constructor to do something useful with the macro. For this class to compile you have to use the following assemblies.

using System;
using System.Web;
using System.Web.UI;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;

The complete class file is available for download with this post. Now you have to build this project. Follow the following steps to get the macro running.

1. Copy the generated dll file to the bin directory of your dasBlog installation.
2. Uncomment or add the following in dasBlog’s web.config :
    <section name=”newtelligence.DasBlog.Macros”           type=”newtelligence.DasBlog.Web.Core.MacroSectionHandler, newtelligence.DasBlog.Web.Core” />
3. The add the following snippet to the same file:
             <add macro=”hello” type=”HelloWorldPlugin.HelloWorldMacros, HelloWorldPlugin”/>

Here HelloWorldPlugin is the namespace you used and HelloWorldMacros is the class name. hello is an alias.
4. Edit the template file homeTemplate.blogtemplate are found in <dasBlog Home>\themes\<your theme name>\. Add <%HelloWorld(“It works!”)|hello%> to it.

Now just visit the blog to see the macro in action. Hope you found this how to useful. This is probably my first how to. So comments and suggestions are welcome. :-)

Update: Added txt extension to the cs file. IIS does not allow download of cs files.

HelloWorldPlugin.cs (.68 KB)

3 thoughts on “Creating Macros for dasBlog”

Comments are closed.