Handlebars inside place holder

99 views Asked by At

Is it possible with handlebars for .NET do something like this:

var data = new { 
    title = "my title",
    body = "my body, look at {{title}}"
}

I mean is it possible to use handlebar inside handlebars-value. Then I have template:

string source = @"
  <b>Title</b> {{title}} <br/>
  <b>Body</b> {{body}}
";

And I would like to have:

<b>Title</b> my title <br/>
<b>Body</b> my body, look at my title

I know this is looks slightly weird, in this case we can easilly split body and title, but it's not my real project, just similiar example, I cannot make such splitting in real project. Does anyone know is it possible do such things with handlebars or not? And if it is possible then how to do it?

2

There are 2 answers

3
Kev On

You can do something along these lines using string.Format() method

0
Rex M On

This is possible, but you need to do a little extra work to make it happen. And it may not perform well, since unless you get more sophisticated, you will have to recompile the template each time you run it. Here's roughly how it would work:

Handlebars.RegisterHelper("template", (output, context, arguments) =>
    {
        var template = Handlebars.Compile((string)arguments[0]);
        var result = template(context);
        output.WriteSafeString(result);
    });

And your template would look like:

<b>Title</b> {{title}} <br/>
<b>Body</b> {{template body}}