Area: Optimizely B2B Commerce

Working with DotLiquid in Classic

Recommended reading 


What Is It?

DotLiquid is an HTML templating engine used in B2B Commerce website views. Additional info can be found on DotLiquid at http://dotliquidmarkup.org.

How Is B2B Commerce Using It?

For B2B Commerce versions 4.2 and prior, all views were implemented in Razor, which allows developers access to any .net code as well as our libraries. Beginning with B2B Commerce 4.3, access to the filesystem and backend libraries is restricted in order to support our SaaS based product, B2B Commerce Cloud. As a result, B2B Commerce only allows standard HTML and DotLiquid in website views. This still gives developers access to the objects provided to the view and allowing for basic HTML templating.

IMPORTANT: The syntax and documentation found for standard DotLiquid will work except for one difference: the default {{ }} syntax has been replaced by [[ ]] in an effort to avoid conflict with Angular code, which is frequently used in B2B Commerce views.

B2B Commerce DotLiquid Tags

B2B Commerce has several custom tags to help facilitate common functionality.


Renders an angular compatible regex for validating emails.

[% emailRegex %]


Renders the navigation menu.

[% navigationMenu %]


Renders partial views from .WebProject/Themes/[ThemeName]/Views/Directives/[FolderName]/[ViewName].

[% partialView '[FolderName]-[ViewName]' %]

NOTE: FolderName is optional


Renders an angular compatible regex for validating phone numbers.

[% phoneRegex %]


Gets a message from the MessageProvider with the message name as the first argument in quotes (single or double are both valid). Optionally it takes a default value as a second string argument. If it is used within single quotes in html it takes an optional last parameter named "EscapeSingleQuotes" that will escape all single quotes in the message returned.

[% siteMessage 'MessageName' %]
[% siteMessage 'MessageName' EscapeSingleQuotes %]
[% siteMessage 'MessageName' 'DefaultValue' %]
[% siteMessage 'MessageName' 'DefaultValue' EscapeSingleQuotes %]


Translates the provided string value using the TranslationLocalizer. If it is used within single quotes in html it takes an optional last parameter named "EscapeSingleQuotes" that will escape all single quotes in the message returned.

[% translate 'Phrase To Translate' %]


Renders a url for the page type specified. The first argument is the Page type. ie HomePage, CartPage, OrderDetailPage

[% urlForPage 'HomePage' %]


Renders the html required to define a zone.

[% zone 'Zone Name' %]

Custom DotLiquid Tags

DotLiquid provides the ability to create custom tags. In order to do this, you need to create a class that inherits from the DotLiquid.Tag class. You can use the "Create your own tags" section at https://github.com/dotliquid/dotliquid/wiki/DotLiquid-for-Developers as a reference.

  1. Create a class that inherits from DotLiquid.Tag. Use the Initialize method to validate and capture arguments. Use the Render method to write content for the custom tag. Below is an example of a custom tag that prints the first argument inside an <h1> element.
    public class MyCustomTag : DotLiquid.Tag
        // Matches the patterns:
        // 'AnyLettersAnyCase'
        // "AnyLettersAnyCase"
        private static readonly Regex ArgumentRegex = new Regex("^(['\"])([a-zA-Z]+)\\1$");
        private string arg1 = string.Empty;
        public override void Initialize(string tagName, string markup, List<string> tokens)
            markup = markup.Trim();
            base.Initialize(tagName, markup, tokens);
        private void ValidateMarkup(string markup)
            if (!ArgumentRegex.IsMatch(markup))
                throw new ArgumentException("The markup for the myCustomTag tag is incorrect. The expected form is [% myCustomTag 'arg1' %].", nameof(markup));
        private void CaptureArguments(string markup)
            this.arg1 = ArgumentRegex.Match(markup).Groups[2].Value;
        public override void Render(Context context, TextWriter result)
  2. Next, you need to register your custom tag with DotLiquid, so it can properly parse and render it within any views. You will need to create your startup file in the extensions project.
        public class RegisterCustomDotLiquidTags : IStartupTask
            public void Run(IAppBuilder app, HttpConfiguration config)
  3. Finally, rebuild your solution. You should now be able to use your custom tag in a view.
    [% myCustomTag 'arg1' %]
Do you find this information helpful? Please log in to provide feedback.

Last updated: Dec 11, 2020

Recommended reading