How to write a “Recent Comments” macro for dasBlog

After I moved to dasBlog, I found myself looking for a possibility to display the most recent comments on the sidebar, so that people can easily see if somebody responded to their comments. But apparently there is nothing like this in dasBlog (at least I haven’t found anything), so I decided to write my own macro. Custom macros are supported by the latest versions of dasBlog. Basicly, a macro is just a class library (.DLL file) with classes and methods. If one of these methods returns a System.Web.UI.Control object, we can call it in our templates.

I started by creating a new class project in Visual Studio. I added references to the libraries newtelligence.DasBlog.Runtime.dll and newtelligence.DasBlog.Web.Core.dll. Both files are found in the /bin directory of your dasBlog installation. I also added these namespaces to the using part of my class file. Then i created a new class RecentCommentsMacro, that inherits from SharedBasePage and I implemented a constructor. The whole thing now looks something like (the entire code is attached to this post, you don’t have to copy and paste):
using System;
using System.Web;
using System.Web.UI;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;

namespace Saxx {
    public class RecentCommentsMacro : SharedBasePage {

        protected SharedBasePage requestPage;
        protected Entry currentItem;

        public RecentCommentsMacro(SharedBasePage page, Entry item) {
            requestPage = page;
            currentItem = item;
        }
}

We can access the data of dasBlog by using the DataService property of the requestPage object. Knowing this, I wrote a method RecentComments(X, Y) that displays the latest X comments and truncates its text to Y characters. My method caches the entire control until a new comment has been posted, so we don’t have to get all comments and sort them whenever a page gets called (which is usually pretty often). I don’t think I’ve to explain the code in detail, it’s quite straight forward:
public static LiteralControl commentsLiteral = new LiteralControl();
public static DateTime lastUpdate = DateTime.MinValue;

public virtual Control RecentComments(int numberOfComments, int maxLengthOfComment) {
    if(numberOfComments < 1)
        numberOfComments = 10;
    if(maxLengthOfComment < 1)
        maxLengthOfComment = 30;

    IBlogDataService dataService = requestPage.DataService;

    if (dataService.GetLastCommentUpdate() > lastUpdate) {
        CommentCollection comments = dataService.GetAllComments();
        if (comments.Count > 0) {
            comments.Sort(new CommentsSorter());

            commentsLiteral.Text = "";
            for (int i = 0; i < numberOfComments; i++) {
       &nbs
p;        Comment comment = comments[i];
                commentsLiteral.Text += "<div class="recentComments">" + comment.Author + ": <a href="" + Utils.GetCommentViewUrl(comment.TargetEntryId) + "" alt="">";                              
                commentsLiteral.Text += TruncateString(Server.HtmlDecode(comment.Content), maxLengthOfComment);
                commentsLiteral.Text += "</a></div>";
            }
        }
    }
    return commentsLiteral;        
}

I know, I should have used StringBuilders, better caching, a lot more comments etc. (so don’t make the same mistakes I did), but the thingi works and I think it gives an idea how to write a macro. Compile the project and copy the .DLL file to the /bin directory of your dasBlog installation. Now we’ve to edit the web.config file a bit, so that dasBlog knows there’s a new macro out there. First add or uncomment this line to/in your web.config file (if not already done):
<section name="newtelligence.DasBlog.Macros" type="newtelligence.DasBlog.Web.Core.MacroSectionHandler, newtelligence.DasBlog.Web.Core" />

Then add these lines to the file (usually there is already something like it in the web.config file, but it’s in comment tags).
<newtelligence.DasBlog.Macros>
   <add macro="saxxmacros" type="Saxx.RecentCommentsMacro, Saxx.Macros"/>
</newtelligence.DasBlog.Macros>

Note that saxxmacros is an alias that you can choose freely (just remember it later), Saxx.RecentCommentsMacro is the name of the class (including namespace) and Saxx.Macros is the name of the DLL file. So, we’re done with the web.config file. Now you can start and edit your templates.

When you want to call your method in one of your templates, it has to look something like
<%RecentComments(12, 20)|saxxmacros%>
where RecentComments is (of course) the name of your method and saxxmacros is the alias you chose before.

That’s it. Have a look at the numerous methods of the DataService property I mentioned earlier – you can access almost every aspect of dasBlog through it. Knowing this you should be able to write about every macro you can think of …

14 Gedanken zu „How to write a “Recent Comments” macro for dasBlog“

  1. So, ich hab den super-secret Bot Check überarbeitet und der ist jetzt so nifty, dass er nicht mehr den Text, sondern das Verhalten des Benutzers analysiert – nett, wofür es heutzutage schon fertige ASP.NET Komponenten gibt 😉