Skip to content

Literal expressions support? #338

@VanquishedWombat

Description

@VanquishedWombat

TLDR: This is a possible issue with the parser not ignoring the dot in {{[xxx.yyyy]}} and how to work around that.

Hi - new to your excellent component so be gentle! My use case allows a specific set of admin users to craft templates. As I am replacing a previous system, I have to take on their current formatting which includes a leading @ or # and a dot (period/full-stop) as a segment delimiter. So they might write

{{@xxx.yyyy}}

I understand for handlebars the dot infers a path expression, and the @ or # are likely problematic too as they are not in the list of allowed identifiers.. So I researched at handlebarsjs.com (linked from your github page) and found literal segments. Using this approach I pre-process the end users template into

{{[@]xxx[.]yyyy}}

The docco says that for handlebars.js - like interpretation, this should be acceptable and the bracketed chars would be seen as literal.

However, using a c# dictionary containing the entry: @xxx.yyy I do not get the replacement I expect. In fact no replacement or error ensues.

Here is a simplified version of code which I just ran to produce the issue.

Dictionary<string, object> data = new Dictionary<string, object>{
        { "@xxx.yyyy", "Da valoo"},
        { "tag2", "The tag valoo" }
    };

string sourceVal = "Complax tag > {{[@]xxx[.]yyyy}} < simple tag > {{tag2}} <";

// so we can see the entry
foreach (KeyValuePair<string, object> entry in data)
{
    dbg.log(mn, " tagList[" + entry.Key + "] = [" + entry.Value + "]"); // shows: tagList[@xxx.yyyy] = [Da valoo], tagList[tag2] = [The tag valoo]
}

var template = Handlebars.Compile(sourceVal);
var result = template(data);
dbg.log(mn, "Result: " + result);  // shows: Result: Complax tag >  < simple tag > The tag valoo <

What am I doing wrong ?

EDIT:

I have tried a few more formats, as in:

@xxx.yyyy > {{[@xxx.yyyy]}} > Fail
#xxx.yyyy > {{[#xxx.yyyy]}} > Fail
xxx.yyyy > {{[xxx.yyyy]}} > Fail
@xxx:yyyy > {{[@xxx:yyyy]}} > Ok
@xxx-yyyy > {{[@xxx-yyyy]}} > Ok
xxx-yyyy > {{[xxx-yyyy]}} > Ok
xxx:yyyy > {{[xxx:yyyy]}} > Ok
#xxx:yyyy > {{[#xxx:yyyy]}} > Ok

The first column is the string list, the second column is the template, and the third is the outcome. I conclude that the dot in xxx.yyyy is being seen as significant even though the entire string is delimited by [ and ].

I also tried the same using single and double quotes in place of the [ and ].

How can I 'escape' the dot for the Handlebar.js parser?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions