예제 #1
0
        public HandlebarsCompiler(HandlebarsSettings settings)
        {
            scriptEngine = new ScriptEngine();
            scriptEngine.Execute(Properties.Resources.handlebars);

            var knownHelpersJson = JsonConvert.SerializeObject(settings.KnownHelpers);
            var knownHelpersOnly = JsonConvert.False;
            if (settings.KnownHelpersOnly) {
                knownHelpersOnly = JsonConvert.True;
            }

            // Create a global compile function that returns the template compiled into javascript source.
            scriptEngine.Execute(@"
                var precompile = function (template) {
                    return Handlebars.precompile(template, { knownHelpers: " + knownHelpersJson + @", knownHelpersOnly: " + knownHelpersOnly + @" });
                };"
            );
        }
예제 #2
0
        public void CanCompileHandlebarsTemplate()
        {
            var settings = new HandlebarsSettings { KnownHelpersOnly = false, KnownHelpers = new List<string> { "t" } };

            var compiler = new HandlebarsCompiler(settings);
            var result = compiler.Compile("Hello {{world}}", new CompileContext());

            var engine = new ScriptEngine();
            engine.ExecuteFile("handlebars.js");
            var source = @"
            (function(){
            var template = new Hogan.template(" + result.Output + @");
            return template({world:'Andrew'});
            }());";
            var templateRender = engine.Evaluate<string>(source);

            templateRender.ShouldEqual("Hello Andrew");
        }