Skip to content

AbstractTemplateSet

Alexander Krutov edited this page Jul 2, 2015 · 13 revisions

Note: It's recommended to familiarize with key concepts before reading this article.

This is is a base abstract class for all template sets used by Radish.

If you would like to control whole process of creating documentation content, like using custom templates with own replacement rules and tags, you should inherit from AbstractTemplateSet, but in most cases customization of the documentation output (like changing CSS styles and markup, or adding custom tags to existing templates) can be done with overriding BasicTemplateSet methods or even with inheritance from one of SimpleTemplateSet or BootstrapTemplateSet classes.

Hierarchy of template sets classes in Radish library is:

            +---------------------+
            | AbstractTemplateSet |
            +---------------------+
                       ^
                       │
            +---------------------+
            |  BasicTemplateSet   |
            +---------------------+
                    ^      ^
                    │      │
  +-------------------+  +----------------------+
  | SimpleTemplateSet |  | BootstrapTemplateSet |
  +-------------------+  +----------------------+

AbstractTemplateSet has only one method required to be implemented in descendants:

public abstract class AbstractTemplateSet
{
    // ...
    public abstract string Page { get; set; }
    // ...
}

The Page getter should return template for whole documentation page. Simpliest implementation of AbstractTemplateSet looks like:

public class CustomTemplateSet : AbstractTemplateSet
{
    // overriding 'Page' template
    public override string Page { get; set; }
    
    public CustomTemplateSet() : base() 
    {
       // set 'Page' template content
       Page = 
           "<html><body>" + 
               "Hello world from Radish!" + 
           "</body></html>";
    }
}

After applying this template set class to Documentor (see Templating article), we will get a static HTML page with a text:

Hello world from Radish!

In the example above we do not use any tags and replacement rules. Let's modify the code by adding a tag {{tag}} to the Page template and introduce a replacement rule that will replace the tag with a "Here was a tag" text:

public class CustomTemplateSet : AbstractTemplateSet
{
    // overriding 'Page' template
    public override string Page { get; set; }

    public CustomTemplateSet() : base() 
    {
       // set 'Page' template content with tag '{{tag}}'
       Page = 
           "<html><body>" + 
               "Hello world from Radish!<br>" +
               "{{tag}}" + 
           "</body></html>";
       
       // adding replacement rule with handler 'MyRuleHandler'
       this.AddRule(ts => ts.Page, "tag", MyRuleHandler);
    }

    // handler will be executed each time when Radish meets "{{tag}}" in the 'Page' template
    private string MyRuleHandler(DocumentorContext context, params object[] parameters)
    {
        return "Here was a tag";
    }
}

And this will give us:

Hello world from Radish!
Here was a tag

Let's go deeper now and modify the rule handler in order to render something useful, for example, amount of REST API methods defined with Radish attributes. For these purposes each handler has DocumentorContext instance passed as first argument of the function:

public class CustomTemplateSet : AbstractTemplateSet
{
    // ...

    // use 'DocumentorContext' to access REST methods info
    private string MyRuleHandler(DocumentorContext context, params object[] parameters)
    {
        int methodsCount = context.RestMethods.Count();
        return String.Format("REST API methods count: {0}", methodsCount);
    }
}

If we mark two REST handlers in our API controllers with MethodAttribute, we get following documentation content:

Hello world from Radish!
REST API methods count: 2

Let's do someting more complex with our CustomTemplateSet. For example, we need to display on the page all REST API methods in form «Method type» (bold) and «Method path» (italic), like:

REST API Methods:
GET /api/pets/{id}
GET /api/pets/

For this purposes we introduce a separate template for a particular REST method with following content:

<b>{{method-type}}</b> <i>{{method-path}}</i>

Here we have just defined two tags for method type and path values respectively.

Our Page template now becomes:

<html>
   <body>
      REST API Methods:<br>
      {{methods-list}}
   </body>
</html>

Here we have defined {{methods-list}} tag that will be replaced to list of methods as we want.

CustomTemplateSet will look like:

public class CustomTemplateSet : AbstractTemplateSet
{
    // overriding 'Page' template
    public override string Page { get; set; }

    // NEW: template for rendering particular REST API method
    public string Method { get; set; }

    public CustomTemplateSet() : base()
    {
        // set 'Page' template content
        Page =
            "<html>" +
              "<body>" +
                "REST API Methods:<br>" +
                "{{methods-list}}" +
              "</body>" +
            "</html>";

        // NEW: set 'Method' template content
        Method = "<b>{{method-type}}</b> <i>{{method-path}}</i>";

        // adding replacement rules
        this.AddRule(ts => ts.Page, "methods-list", Page_MethodsList_Handler);
        this.AddRule(ts => ts.Method, "method-type", Method_MethodType_Handler);
        this.AddRule(ts => ts.Method, "method-path", Method_MethodPath_Handler);
    }

    // handler for '{{methods-list}}' tag in the 'Page' template
    private string Page_MethodsList_Handler(DocumentorContext context, params object[] parameters)
    {
        // 'methods-list' tag content
        string content = "";
        // iterate thru all REST methods and render 'Method' template for each 'MethodInfo'
        foreach (MethodInfo mi in context.RestMethods)
        {
            content += context.RenderTemplate<CustomTemplateSet>(ts => ts.Method, mi);
        }
        return content;
    }

    // handler for '{{method-type}}' tag in the 'Method' template
    private string Method_MethodType_Handler(DocumentorContext context, params object[] parameters)
    {
        MethodInfo mi = parameters[0] as MethodInfo;
        MethodAttribute attr = mi.GetRadishAttribute<MethodAttribute>();
        return attr.MethodType;
    }

    // handler for '{{method-path}}' tag in the 'Method' template
    private string Method_MethodPath_Handler(DocumentorContext context, params object[] parameters)
    {
        MethodInfo mi = parameters[0] as MethodInfo;
        MethodAttribute attr = mi.GetRadishAttribute<MethodAttribute>();
        return attr.Route;
    }
}

We have defined 2 templates: Page is overriden from the base AbstractTemplateSet class, and second one is our custom template Method. Page template is a top-level, Method template is nested. To initate rendering of nested templates we call RenderTemplate function of DocumentorContext instance. Also we have defined 3 tags, 3 replacement rules and 3 rule handlers for each tag.

Page_MethodsList_Handler iterates thru all REST methods from DocumentorContext and initiates rendering of Method template. Here we pass extra parameter to RenderTemplate(...) function: instance of MethodInfo. It means that all rule handlers of Method template will accept MethodInfo instance as a parameter. In this way we inform handlers about a REST API method that is being rendered at the moment.

In other words, calling RenderTemplate(...) function works like a chain link between two levels of templates, and initiates rendering of lower level of nesting. In the same way, you can implement multiple nesting levels of templates if you need.

For more examples see implementation of BasicTemplateSet class.


Note: in the examples above we have often used string concatenation while building documentation content. This is done only for simplifying the code. In real projects it's strongly reccommended to use StringBuilder for these purposes because string concatenation increases memory consumption.

Clone this wiki locally