public void TestEchoGenerated() { ParseAndGenerate(@"<html name=""<%= name %>"">"); TemplateArguments args = new TemplateArguments("name", "jonas"); ITinyTemplate template = _compiler.Compile(args, _sb.ToString(), "nun"); string result = template.Invoke(args, null); Assert.Equal("<html name=\"jonas\">\r\n", result); }
public override BundleResponse GenerateBundleResponse(BundleContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (!BundleTable.EnableOptimizations) { return(new BundleResponse(string.Empty, new List <BundleFile>())); } var bundleFiles = EnumerateFiles(context); var ignoredFiles = context.BundleCollection.IgnoreList.FilterIgnoredFiles(context, bundleFiles); var files = Orderer.OrderFiles(context, ignoredFiles).ToList(); if (string.IsNullOrWhiteSpace(_options.WorkingDir)) { _options.WorkingDir = "/"; } var compiler = new TemplateCompiler(_options); var virtualFiles = files.Select(f => f.VirtualFile).ToArray(); var result = compiler.Compile(virtualFiles); return(ApplyTransforms(context, result, files)); }
/// <summary> /// Compiles and renders a template. /// </summary> /// <param name="path">The fully qualified path of the file to load.</param> /// <param name="context">The <see cref="TemplateContext"/>.</param> /// <returns></returns> public static string CompileFileAndExec(this TemplateContext context, string path) { var full = context.FindFullPath(path); if (full == null) { throw new Exception.TemplateException($"\"{ path }\" cannot be found."); } var template = context.Options.CompilerResults.GetOrAdd(full, (name) => { var res = context.Options.Loader.Load(path, context.Options.Encoding, context.Options.ResourceDirectories.ToArray()); if (res == null) { throw new Exception.TemplateException($"Path:\"{path}\", the file could not be found."); } if (string.IsNullOrEmpty(name)) { name = res.FullPath; } return(TemplateCompiler.Compile(name, res.Content, context.Options, (c) => context.CopyTo(c))); }); using (var sw = new System.IO.StringWriter()) { template.Render(sw, context); return(sw.ToString()); } }
public void SimpleBlockTemplateCompilesCorrectly() { var source = FileHelper.GetTemplateSource("simple-block.html"); var compiler = new TemplateCompiler(source); var result = compiler.Compile().ToString(); Assert.AreEqual(result, "function (model){var t='';t+='<ul>'; for(var i = 0; i < model.users.length; i++) { t+='<li>'+ model.users[i].name ;t+='</li>'; } t+='</ul>';return t;}"); }
public static void TemplateCompiler_Compile_Constant() { var template = RawUriTemplate.Parse("/test/url?some=value&foo=bar"); Func <object, string> compiled = TemplateCompiler.Compile <object>(template); string result = compiled(new object()); Assert.Equal("/test/url?some=value&foo=bar", result); }
public void SimpleTemplateCompilesCorrectly() { var source = FileHelper.GetTemplateSource("simple.html"); var compiler = new TemplateCompiler(source); var result = compiler.Compile().ToString(); Assert.AreEqual(result, "function (model){var t='';t+='<div>'+ model.test ;t+='</div>';return t;}"); }
public static void TemplateCompiler_Compile_EnumQuery_Nullable_Named_Null() { var values = new Values(); var template = RawUriTemplate.Parse("?f={NormalNullable.First=foo}&s={NormalNullable.Second=bar}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("", result); }
public static void TemplateCompiler_Compile_DefaultPath() { var values = new Values(); var template = RawUriTemplate.Parse("/test/{NullableField=Default}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("/test/Default", result); }
public static void TemplateCompiler_Compile_NullableQuery_Null() { var values = new Values(); var template = RawUriTemplate.Parse("?f={NullableField:x}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("", result); }
public static void TemplateCompiler_Compile_CollectionQuery_Empty() { var values = new Values(); var template = RawUriTemplate.Parse("?f={Collection[]}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("", result); }
public static void TemplateCompiler_Compile_Formattable_Default() { var values = new Values(); var template = RawUriTemplate.Parse("?f={Formattable=Value}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("?f=Value", result); }
/** Compile a template */ public virtual CompiledTemplate Compile(string srcName, string name, List <FormalArgument> args, string template, IToken templateToken) // for error location { //System.out.println("TemplateGroup.Compile: "+enclosingTemplateName); TemplateCompiler c = new TemplateCompiler(this); return(c.Compile(srcName, name, args, template, templateToken)); }
public void ShouldUseCustomModuleName() { var options = new TemplateCompilerOptions { ModuleName = "myapp", WorkingDir = "/fixtures" }; var compiler = new TemplateCompiler(options); var result = compiler.Compile(new VirtualFile[] {_template1}); Assert.Equal(File.ReadAllText("../../../expected/compiled2.js"), result); }
/// <inheritdoc /> public ICompilerResult Compile(string name, string content, Action <CompileContext> action = null) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(nameof(content)); } if (string.IsNullOrEmpty(name)) { name = content.GetHashCode().ToString(); } return(Options.CompilerResults[name] = TemplateCompiler.Compile(name, content, Options, action)); }
public static void TemplateCompiler_Compile_NotFormattable() { var values = new Values() { NotFormattable = new NotFormattable() }; var template = RawUriTemplate.Parse("?f={NotFormattable}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("?f=Foo%21", result); }
public static void TemplateCompiler_Compile_EnumQuery_Flags_Named() { var values = new Values() { Flags = FlagsEnum.First | FlagsEnum.Second }; var template = RawUriTemplate.Parse("?f={Flags.First=first}&s={Flags.Second=baz}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("?f=first&s=baz", result); }
public static void TemplateCompiler_Compile_EnumQuery_Named() { var values = new Values() { Normal = NormalEnum.Second }; var template = RawUriTemplate.Parse("?f={Normal.First}&s={Normal.Second=second}&t={Normal.Second}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("?s=second&t", result); }
public static void TemplateCompiler_Compile_Query() { var values = new Values() { Field = 100 }; var template = RawUriTemplate.Parse("?f={Field}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("?f=100", result); }
public void ShouldUseCustomModuleName() { var options = new TemplateCompilerOptions { ModuleName = "myapp", WorkingDir = "/fixtures" }; var compiler = new TemplateCompiler(options); var result = compiler.Compile(new VirtualFile[] { _template1 }); Assert.Equal(File.ReadAllText("../../../expected/compiled2.js"), result); }
public void ShouldRenderStandaloneModule() { var options = new TemplateCompilerOptions { ModuleName = "", WorkingDir = "/fixtures", Standalone = true }; var compiler = new TemplateCompiler(options); var result = compiler.Compile(new VirtualFile[] { _template1 }); Assert.Equal(File.ReadAllText("../../../expected/compiled5.js"), result); }
public void ShouldLowercaseTemplateName() { var options = new TemplateCompilerOptions { ModuleName = "", WorkingDir = "/fixtures", LowercaseTemplateName = true }; var template = new TestVirtualFile("/fixtures/TEMPLATE1.html", "test"); var compiler = new TemplateCompiler(options); var result = compiler.Compile(new VirtualFile[] { template }); Assert.Equal(File.ReadAllText("../../../expected/compiled1.js"), result); }
/// <inheritdoc /> public ICompilerResult CompileFile(string name, string path, Action <CompileContext> action = null) { var res = Options.Loader.Load(path, Options.Encoding, Options.ResourceDirectories.ToArray()); if (res == null) { throw new Exception.TemplateException($"Path:\"{path}\", the file could not be found."); } if (string.IsNullOrEmpty(name)) { name = res.FullPath; } return(Options.CompilerResults[name] = TemplateCompiler.Compile(name, res.Content, Options, action)); }
public void ShouldCombineMultipleFiles() { var options = new TemplateCompilerOptions { Prefix = "/templates", ModuleName = "myapp", WorkingDir = "/fixtures" }; var compiler = new TemplateCompiler(options); var result = compiler.Compile(new VirtualFile[] { _template1, _template2 }); Assert.Equal(File.ReadAllText("../../../expected/compiled4.js"), result); }
public static void TemplateCompiler_Compile_CollectionQuery() { var values = new Values() { Collection = new[] { "Test", null, "Foo" } }; var template = RawUriTemplate.Parse("?f={Collection[]}"); Func <Values, string> compiled = TemplateCompiler.Compile <Values>(template); string result = compiled(values); Assert.Equal("?f=Test&f&f=Foo", result); }
/// <summary> /// Construct an <see cref="ExpressionTemplate"/>. /// </summary> /// <param name="template">The template text.</param> /// <param name="formatProvider">Optionally, an <see cref="IFormatProvider"/> to use when formatting /// embedded values.</param> /// <param name="nameResolver">Optionally, a <see cref="NameResolver"/> /// with which to resolve function names that appear in the template.</param> public ExpressionTemplate( string template, IFormatProvider?formatProvider = null, NameResolver?nameResolver = null) { if (template == null) { throw new ArgumentNullException(nameof(template)); } if (!TemplateParser.TryParse(template, out var parsed, out var error)) { throw new ArgumentException(error); } _compiled = TemplateCompiler.Compile(parsed, DefaultFunctionNameResolver.Build(nameResolver)); _formatProvider = formatProvider; }
/// <summary> /// Construct an <see cref="ExpressionTemplate"/>. /// </summary> /// <param name="template">The template text.</param> /// <param name="formatProvider">Optionally, an <see cref="IFormatProvider"/> to use when formatting /// embedded values.</param> /// <param name="result">The parsed template, if successful.</param> /// <param name="error">A description of the error, if unsuccessful.</param> /// <param name="nameResolver">Optionally, a <see cref="NameResolver"/> /// with which to resolve function names that appear in the template.</param> /// <returns><c langword="true">true</c> if the template was well-formed.</returns> public static bool TryParse( string template, IFormatProvider?formatProvider, NameResolver?nameResolver, [MaybeNullWhen(false)] out ExpressionTemplate result, [MaybeNullWhen(true)] out string error) { if (template == null) { throw new ArgumentNullException(nameof(template)); } if (!TemplateParser.TryParse(template, out var parsed, out error)) { result = null; return(false); } result = new ExpressionTemplate(TemplateCompiler.Compile(parsed, DefaultFunctionNameResolver.Build(nameResolver)), formatProvider); return(true); }
public override BundleResponse GenerateBundleResponse(BundleContext context) { if (context == null) throw new ArgumentNullException("context"); if (!BundleTable.EnableOptimizations) { return new BundleResponse(string.Empty, new List<BundleFile>()); } var bundleFiles = EnumerateFiles(context); var ignoredFiles = context.BundleCollection.IgnoreList.FilterIgnoredFiles(context, bundleFiles); var files = Orderer.OrderFiles(context, ignoredFiles).ToList(); if (string.IsNullOrWhiteSpace(_options.WorkingDir)) { _options.WorkingDir = "/"; } var compiler = new TemplateCompiler(_options); var virtualFiles = files.Select(f => f.VirtualFile).ToArray(); var result = compiler.Compile(virtualFiles); return ApplyTransforms(context, result, files); }
/// <inheritdoc /> public virtual void Render(TextWriter writer, TemplateContext context) { var text = this.TemplateContent; var t = context.Options.CompilerResults.GetOrAdd(this.TemplateKey, (key) => { return(TemplateCompiler.Compile(key, text, context.Options, (ctx) => { context.CopyTo(ctx); })); }); if (t == null) { throw new Exception.TemplateException($"compile error."); } try { t.Render(writer, context); } catch (System.Exception e) { context.AddError(e); } }
public void ShouldRenderStandaloneModule() { var options = new TemplateCompilerOptions { ModuleName = "", WorkingDir = "/fixtures", Standalone = true }; var compiler = new TemplateCompiler(options); var result = compiler.Compile(new VirtualFile[] {_template1}); Assert.Equal(File.ReadAllText("../../../expected/compiled5.js"), result); }
public void ShouldLowercaseTemplateName() { var options = new TemplateCompilerOptions { ModuleName = "", WorkingDir = "/fixtures", LowercaseTemplateName = true }; var template = new TestVirtualFile("/fixtures/TEMPLATE1.html", "test"); var compiler = new TemplateCompiler(options); var result = compiler.Compile(new VirtualFile[] {template}); Assert.Equal(File.ReadAllText("../../../expected/compiled1.js"), result); }
/// <summary> /// Compiles a Twofold template into internal representation. /// </summary> /// <param name="templateName">Name of Twofold template.</param> /// <returns>The compiled template or null if an error occured.</returns> public CompiledTemplate Compile(string templateName) { var templateCompiler = new TemplateCompiler(this.TemplateLoader, this.MessageHandler, this.ReferencedAssemblies); return(templateCompiler.Compile(templateName)); }
public static void TemplateCompiler_Compile_EnumQuery_NotEnum() { var template = RawUriTemplate.Parse("?f={Field.First}"); InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => TemplateCompiler.Compile <Values>(template)); Assert.Equal("An enum was expected for Field.", ex.Message); }