public void CompileHoganTemplate() { const string template = @"<div class=""entry""> <h1>{{title}}</h1> {{#author}} <h2>By {{firstName}} {{lastName}}</h2> {{/author}} </div>"; var embeddedResourceLoader = new EmbeddedResourceLoader(); var hoganCompiler = new HoganCompiler(embeddedResourceLoader); const string expectedCompiledTemplate = @"{code: function (c,p,i) { var t=this;t.b(i=i||"""");t.b(""<div class=\""entry\"">"");t.b(""\n"" + i);t.b("" <h1>"");t.b(t.v(t.f(""title"",c,p,0)));t.b(""</h1>"");t.b(""\n"");t.b(""\n"" + i);if(t.s(t.f(""author"",c,p,1),c,p,0,55,99,""{{ }}"")){t.rs(c,p,function(c,p,t){t.b("" <h2>By "");t.b(t.v(t.f(""firstName"",c,p,0)));t.b("" "");t.b(t.v(t.f(""lastName"",c,p,0)));t.b(""</h2>"");t.b(""\n"" + i);});c.pop();}t.b(""</div>"");return t.fl(); },partials: {}, subs: { }}"; var compiledTemplate = hoganCompiler.Compile(template); Assert.AreEqual(expectedCompiledTemplate, compiledTemplate); }
public void RenderHoganTemplate() { const string template = @"<div class=""entry""> <h1>{{title}}</h1> {{#author}} <h2>By {{firstName}} {{lastName}}</h2> {{/author}} </div>"; const string templateName = "Authors.mustache"; var embeddedResourceLoader = new EmbeddedResourceLoader(); var hoganCompiler = new HoganCompiler(embeddedResourceLoader); var compiledTemplate = hoganCompiler.Compile(template); hoganCompiler.AddTemplate(templateName, compiledTemplate); var model = new { title = "The Title", author = new [] { new { firstName = "FirstName", lastName = "LastName" }, new { firstName = "2ndFirstName", lastName = "2ndLastName" }, } }; const string expectedTemplateOuput = @"<div class=""entry""> <h1>The Title</h1> <h2>By FirstName LastName</h2> <h2>By 2ndFirstName 2ndLastName</h2> </div>"; var templateOutput = hoganCompiler.Render(templateName, model); Assert.AreEqual(expectedTemplateOuput, templateOutput); }
public void CompileIcedCoffeeTemplate() { const string script = @"# Assignment: number = 42 opposite = true # Conditions: number = -42 if opposite # Functions: square = (x) -> x * x # Arrays: list = [1, 2, 3, 4, 5] # Objects: math = root: Math.sqrt square: square cube: (x) -> x * square x # Splats: race = (winner, runners...) -> print winner, runners # Existence: alert ""I knew it!"" if elvis? # Array comprehensions: cubes = (math.cube num for num in list)"; var embeddedResourceLoader = new EmbeddedResourceLoader(); var icedCoffeeCompiler = new IcedCoffeeCompiler(embeddedResourceLoader); var expectedCompiledScript = @"(function() { var cubes, list, math, num, number, opposite, race, square, __slice = [].slice; number = 42; opposite = true; if (opposite) { number = -42; } square = function(x) { return x * x; }; list = [1, 2, 3, 4, 5]; math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); } }; race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners); }; if (typeof elvis !== ""undefined"" && elvis !== null) { alert(""I knew it!""); } cubes = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { num = list[_i]; _results.push(math.cube(num)); } return _results; })(); }).call(this); ".Replace("\r", ""); var compiledTemplate = icedCoffeeCompiler.Compile(script); Assert.AreEqual(expectedCompiledScript, compiledTemplate); }
public void CompileLiveScriptTemplate() { const string script = @" take = (n, [x, ...xs]:list) --> | n <= 0 => [] | empty list => [] | otherwise => [x] ++ take n - 1, xs take 2, [1 2 3 4 5] #=> [1, 2] take-three = take 3 take-three [3 to 8] #=> [3, 4, 5] # Function composition, 'reverse' from prelude.ls last-three = reverse >> take-three >> reverse last-three [1 to 8] #=> [6, 7, 8] "; var embeddedResourceLoader = new EmbeddedResourceLoader(); var liveScriptCompiler = new LiveScriptCompiler(embeddedResourceLoader); var expectedCompiledScript = @"(function(){ var take, takeThree, lastThree, slice$ = [].slice; take = curry$(function(n, list){ var x, xs; x = list[0], xs = slice$.call(list, 1); switch (false) { case !(n <= 0): return []; case !empty(list): return []; default: return [x].concat(take(n - 1, xs)); } }); take(2, [1, 2, 3, 4, 5]); takeThree = take(3); takeThree([3, 4, 5, 6, 7, 8]); lastThree = function(){ return reverse(takeThree(reverse.apply(this, arguments))); }; lastThree([1, 2, 3, 4, 5, 6, 7, 8]); function curry$(f, bound){ var context, _curry = function(args) { return f.length > 1 ? function(){ var params = args ? args.concat() : []; context = bound ? context || this : this; return params.push.apply(params, arguments) < f.length && arguments.length ? _curry.call(context, params) : f.apply(context, params); } : f; }; return _curry(); } }).call(this); ".Replace("\r", ""); var compiledTemplate = liveScriptCompiler.Compile(script); Assert.AreEqual(expectedCompiledScript, compiledTemplate); }