Skip to content

JenTechSystems/Chevron

 
 

Repository files navigation

Icon

Wraps HandlebarsJS to make it usable from .net.

Nuget

Has a dependency on the MsieJavaScriptEngine package.

ILMerges the MsieJavaScriptEngine assembly to avoid an extra dependency

Has a dependency on the ClearScript.V8 package.

Has a dependency on the MsieJavaScriptEngine and Chevron.IE packages.

ILMerges the MsieJavaScriptEngine and Chevron.IE assembly to avoid an any extra dependencies.

Has a dependency on the ClearScript.V8 and Chevron.V8 packages.

Usage

Rendering a template

Input

var source = @"<p>Hello, my name is {{name}}. I am from {{hometown}}. I have ' +
        '{{kids.length}} kids:</p>' +
        '<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";

var context = new
{
    name = "Alan",
    hometown = "Somewhere, TX",
    kids = new[]
        {
            new
            {
                name = "Sally",
                age = "4"
            }
        }
};

using (var handleBars = new Handlebars())
{
    handleBars.RegisterTemplate("myTemplate", source);
    Approvals.Verify(handleBars.Transform("myTemplate", context));
}

Output

<p>Hello, my name is Alan. I am from Somewhere, TX. I have 1 kids:</p>
<ul>
	<li>Sally is 4</li>
</ul>

Register Helpers

Input

var source = "<ul>{{#posts}}<li>{{link_to}}</li>{{/posts}}</ul>";
var context = new
{
    posts = new[]
        {
            new
            {
                url = "/hello-world",
                body = "Hello World!"
            }
        }
};
using (var handleBars = new Handlebars())
{
    handleBars.RegisterHelper("link_to",
        @"function() {
return new Handlebars.SafeString(""<a href='"" + this.url + ""'>"" + this.body + ""</a>"");
}");
    handleBars.RegisterTemplate("myTemplate", source);
    Approvals.Verify(handleBars.Transform("myTemplate", context));
}

Output

<ul>
	<li>
		<a href='/hello-world'>Hello World!</a>
	</li>
</ul>

Register Partials

Input

var source = "<ul>{{#people}}<li>{{> link}}</li>{{/people}}</ul>";
var context = new
{
    people = new[]
        {
            new
            {
                name = "Alan",
                id = 1
            },
            new
            {
                name = "Yehuda",
                id = 2
            }
        }
};
using (var handleBars = new Handlebars())
{
    handleBars.RegisterPartial("link",@"<a href=""/people/{{id}}"">{{name}}</a>");
    handleBars.RegisterTemplate("myTemplate", source);
    Approvals.Verify(handleBars.Transform("myTemplate", context));
}

Output

<ul>
	<li><a href="/people/1">Alan</a></li>
	<li><a href="/people/2">Yehuda</a></li>
</ul>

HandlebarsJS

The binary ships with a resource merged version of HandlebarsJS. Also see the License.

Current merged version

The current version included in the library is v1.3.0. If you feel a newer version should be included at any point in time please raise an issue.

Running a custom version

If you want to run a custom version of HandlebaseJS simply place the custom handlebars.js in the current running directory and that file will be used instead of the merged version.

Icon

Mustache designed by Matt Brooks from The Noun Project

About

Wraps Handlebars to make it usable from .net.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published