private string Render(params object[] args) { try { // Converts the incoming object arguments to proper TemplateArguments TemplateArguments arguments = new TemplateArguments(args); string pageTemplate = _templateMgr.Render("views\\user\\" + MethodName + ".haml", arguments); arguments.Clear(); arguments.Add("text", pageTemplate); return _templateMgr.Render("views\\layouts\\application.haml", arguments); } catch(FileNotFoundException err) { throw new NotFoundException("Failed to find template. Details: " + err.Message, err); } catch(InvalidOperationException err) { throw new InternalServerException("Failed to render template. Details: " + err.Message, err); } catch (TemplateException err) { throw new InternalServerException("Failed to compile template. Details: " + err.Message, err); } catch (ArgumentException err) { throw new InternalServerException("Failed to render templates", err); } }
/// <summary> /// Initializes the class with all the arguments of the parameter class. /// </summary> /// <param name="arguments">Cannot be null</param> /// <exception cref="ArgumentNullException">If arguments is null</exception> public TemplateArguments(TemplateArguments arguments) { if(arguments == null) throw new ArgumentNullException("arguments"); _arguments = new Dictionary<string, ArgumentContainer>(arguments._arguments); }
/// <summary> /// Compiles the specified args. /// </summary> /// <param name="args">Arguments, should contain "name, value, name, value" etc.</param> /// <param name="template">c# code that will be included in the generated template class</param> /// <param name="templateId">Id of the template class</param> /// <returns>Tiny template if successful; otherwise null.</returns> /// <exception cref="CompilerException">If compilation fails</exception> /// <exception cref="ArgumentException">If args are incorrect</exception> public ITinyTemplate Compile(TemplateArguments args, string template, string templateId) { ArgumentContainer[] arguments = args.GetArguments(); foreach (ArgumentContainer arg in arguments) { _compiler.Add(arg.Type); } string members = string.Empty; string body = string.Empty; foreach (ArgumentContainer arg in arguments) { members += Compiler.GetTypeName(arg.Type) + " " + arg.Name + ";" + Environment.NewLine; body += "this." + arg.Name + " = (" + Compiler.GetTypeName(arg.Type) + ")args[\"" + arg.Name + "\"].Object;" + Environment.NewLine; } body += template; _generatedTemplate = TemplateBase.Replace("{id}", templateId).Replace("{members}", members).Replace("{body}", body); _compiler.Compile(_generatedTemplate); return(_compiler.CreateInstance <ITinyTemplate>()); }
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 void TestDuplicate() { _arguments = new TemplateArguments("Test", 1); Assert.Throws(typeof (ArgumentException), delegate { _arguments.Add("Test", 2); }); }
/// <summary> /// Initializes the class with all the arguments of the parameter class. /// </summary> /// <param name="arguments">Cannot be null</param> /// <exception cref="ArgumentNullException">If arguments is null</exception> public TemplateArguments(TemplateArguments arguments) { if (arguments == null) { throw new ArgumentNullException("arguments"); } _arguments = new Dictionary <string, ArgumentContainer>(arguments._arguments); }
public void TestCodeGenerated() { ParseAndGenerate(@"<html><% if (a == 'a') { %>Hello<% } %></html>"); TemplateArguments args = new TemplateArguments("a", 'b'); ITinyTemplate template = _compiler.Compile(args, _sb.ToString(), "nun"); Assert.Equal("<html></html>\r\n", template.Invoke(args, null)); Assert.Equal("<html>Hello</html>\r\n", template.Invoke(new TemplateArguments("a", 'a'), null)); }
/// <summary> /// A function that merges two argument holders updating and adding values /// </summary> /// <param name="arguments"></param> /// <exception cref="ArgumentNullException">If arguments is null</exception> public void Update(TemplateArguments arguments) { if(arguments == null) throw new ArgumentNullException("arguments"); foreach (ArgumentContainer argument in arguments.GetArguments()) { if(_arguments.ContainsKey(argument.Name)) _arguments[argument.Name] = argument; else _arguments.Add(argument.Name, argument); } }
/// <summary> /// Generate HTML from a template. /// </summary> /// <param name="filename">Path and filename</param> /// <param name="args">Variables used in the template. Should be specified as "name, value, name, value" where name is variable name and value is variable contents.</param> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="CompilerException"></exception> /// <exception cref="ArgumentException"></exception> /// <example> /// <code> /// string html = cache.Generate("views\\users\\view.haml", new TemplateArguments("user", dbUser, "isAdmin", dbUser.IsAdmin), null); /// </code> /// </example> public string Render(string filename, TemplateArguments args) { if (args == null) { args = new TemplateArguments(); } // Generate a new proper filename (the generator gets saved aswell) : todo, this works perfectly but isnt so good looking, is it? GetGeneratorForWildCard(ref filename); // Generate a name identifying the template string templateName = "TemplateClass" + filename.GetHashCode() + args.GetHashCode(); templateName = templateName.Replace('-', 'N'); TemplateInfoImp info; lock (_compiledTemplates) { if (_compiledTemplates.ContainsKey(templateName)) { info = _compiledTemplates[templateName]; } else { info = new TemplateInfoImp { Filename = filename, Template = null, CompiledWhen = DateTime.MinValue }; _compiledTemplates.Add(templateName, info); } } lock (info) { if (!CheckTemplate(info) || info.Template == null) { string code = GenerateCode(ref filename); info.Template = Compile(filename, code, args, templateName); info.CompiledWhen = DateTime.Now; info.Filename = filename; } return(info.Template.Invoke(args, this)); } }
/// <summary> /// A function that merges two argument holders updating and adding values /// </summary> /// <param name="arguments"></param> /// <exception cref="ArgumentNullException">If arguments is null</exception> public void Update(TemplateArguments arguments) { if (arguments == null) { throw new ArgumentNullException("arguments"); } foreach (ArgumentContainer argument in arguments.GetArguments()) { if (_arguments.ContainsKey(argument.Name)) { _arguments[argument.Name] = argument; } else { _arguments.Add(argument.Name, argument); } } }
/// <summary> /// Compiles the specified args. /// </summary> /// <param name="args">Arguments, should contain "name, value, name, value" etc.</param> /// <param name="template">c# code that will be included in the generated template class</param> /// <param name="templateId">Id of the template class</param> /// <returns>Tiny template if successful; otherwise null.</returns> /// <exception cref="CompilerException">If compilation fails</exception> /// <exception cref="ArgumentException">If args are incorrect</exception> public ITinyTemplate Compile(TemplateArguments args, string template, string templateId) { ArgumentContainer[] arguments = args.GetArguments(); foreach (ArgumentContainer arg in arguments) _compiler.Add(arg.Type); string members = string.Empty; string body = string.Empty; foreach (ArgumentContainer arg in arguments) { members += Compiler.GetTypeName(arg.Type) + " " + arg.Name + ";" + Environment.NewLine; body += "this." + arg.Name + " = (" + Compiler.GetTypeName(arg.Type) + ")args[\"" + arg.Name + "\"].Object;" + Environment.NewLine; } body += template; _generatedTemplate = TemplateBase.Replace("{id}", templateId).Replace("{members}", members).Replace("{body}", body); _compiler.Compile(_generatedTemplate); return _compiler.CreateInstance<ITinyTemplate>(); }
/// <summary> /// Compiles the specified code. /// </summary> /// <param name="fileName">Name of template.</param> /// <param name="code">c# code generated from a template.</param> /// <param name="arguments">Arguments as in name, value, name, value, name, value</param> /// <param name="templateId"> /// An id to specify the exact instance of a template. Made from joining the 'TemplateClass' with the hashcode of the filename /// and the hash code of the supplied arguments /// </param> /// <returns>Template</returns> /// <exception cref="TemplateException">If compilation fails</exception> /// <exception cref="ArgumentException">Code is not specified.</exception> protected ITinyTemplate Compile(string fileName, string code, TemplateArguments arguments, string templateId) { if (string.IsNullOrEmpty(code)) { throw new ArgumentException("Code is not specified."); } TemplateCompiler compiler = new TemplateCompiler(); foreach (Type type in _includedTypes) { compiler.Add(type); } try { return(compiler.Compile(arguments, code, templateId)); } catch (CompilerException err) { throw new TemplateException(fileName, err); } }
public void TestNonExisting() { _arguments = new TemplateArguments(); Assert.Throws(typeof(ArgumentException), delegate { _arguments.Update("Test", 2); }); }
public void TestNullObject() { _arguments = new TemplateArguments(); Assert.Throws(typeof(ArgumentNullException), delegate { _arguments.Add("Test", null); }); }
/// <summary> /// Compiles the specified code. /// </summary> /// <param name="fileName">Name of template.</param> /// <param name="code">c# code generated from a template.</param> /// <param name="arguments">Arguments as in name, value, name, value, name, value</param> /// <param name="templateId"> /// An id to specify the exact instance of a template. Made from joining the 'TemplateClass' with the hashcode of the filename /// and the hash code of the supplied arguments /// </param> /// <returns>Template</returns> /// <exception cref="TemplateException">If compilation fails</exception> /// <exception cref="ArgumentException">Code is not specified.</exception> protected ITinyTemplate Compile(string fileName, string code, TemplateArguments arguments, string templateId) { if (string.IsNullOrEmpty(code)) throw new ArgumentException("Code is not specified."); TemplateCompiler compiler = new TemplateCompiler(); foreach (Type type in _includedTypes) compiler.Add(type); try { return compiler.Compile(arguments, code, templateId); } catch(CompilerException err) { throw new TemplateException(fileName, err); } }
public void TestWrongTypeSubmitted() { _arguments = new TemplateArguments(); Assert.Throws(typeof (ArgumentException), delegate { _arguments.Add("TestString", 4, typeof (float)); }); }
public void TestNullStringSubmitted() { Assert.Throws(typeof(ArgumentNullException), delegate { _arguments = new TemplateArguments(null, 1); }); }
public void TestNoTypeSubmittedTo() { Assert.Throws(typeof (ArgumentNullException), delegate { _arguments = new TemplateArguments("User", null); }); }
/// <summary> /// Merge arguments array and Arguments property. /// </summary> /// <param name="args">Arguments array to merge</param> /// <returns>arguments/parameters that can be used in the template.</returns> /// <remarks>Will add Request/Response/Session arguments</remarks> private TemplateArguments MergeArguments(object[] args) { // Create a new argument holder TemplateArguments arguments = new TemplateArguments(); arguments.Add("Request", Request, typeof(IHttpRequest)); arguments.Add("Response", Response); arguments.Add("Session", Session); arguments.Add("Controller", this, typeof(ViewController)); arguments.Update(_arguments); arguments.Update(new TemplateArguments(args)); return arguments; }
/// <summary> /// Render a partial /// </summary> /// <param name="filename">Path and filename</param> /// <param name="templateArguments">Variables used in the template. Should be specified as "name, value, name, value" where name is variable name and value is variable contents.</param> /// <param name="partialArguments">Arguments passed from parent template</param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="TemplateException"></exception> /// <exception cref="ArgumentException"></exception> public string RenderPartial(string filename, TemplateArguments templateArguments, TemplateArguments partialArguments) { templateArguments.Update(partialArguments); return Render(filename, templateArguments); }
/// <summary> /// Generate HTML from a template. /// </summary> /// <param name="filename">Path and filename</param> /// <param name="args">Variables used in the template. Should be specified as "name, value, name, value" where name is variable name and value is variable contents.</param> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="CompilerException"></exception> /// <exception cref="ArgumentException"></exception> /// <example> /// <code> /// string html = cache.Generate("views\\users\\view.haml", new TemplateArguments("user", dbUser, "isAdmin", dbUser.IsAdmin), null); /// </code> /// </example> public string Render(string filename, TemplateArguments args) { if (args == null) args = new TemplateArguments(); // Generate a new proper filename (the generator gets saved aswell) : todo, this works perfectly but isnt so good looking, is it? GetGeneratorForWildCard(ref filename); // Generate a name identifying the template string templateName = "TemplateClass" + filename.GetHashCode() + args.GetHashCode(); templateName = templateName.Replace('-', 'N'); TemplateInfoImp info; lock (_compiledTemplates) { if (_compiledTemplates.ContainsKey(templateName)) info = _compiledTemplates[templateName]; else { info = new TemplateInfoImp { Filename = filename, Template = null, CompiledWhen = DateTime.MinValue }; _compiledTemplates.Add(templateName, info); } } lock (info) { if (!CheckTemplate(info) || info.Template == null) { string code = GenerateCode(ref filename); info.Template = Compile(filename, code, args, templateName); info.CompiledWhen = DateTime.Now; info.Filename = filename; } return info.Template.Invoke(args, this); } }
/// <summary> /// Render a partial /// </summary> /// <param name="filename">Path and filename</param> /// <param name="templateArguments">Variables used in the template. Should be specified as "name, value, name, value" where name is variable name and value is variable contents.</param> /// <param name="partialArguments">Arguments passed from parent template</param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="TemplateException"></exception> /// <exception cref="ArgumentException"></exception> public string RenderPartial(string filename, TemplateArguments templateArguments, TemplateArguments partialArguments) { templateArguments.Update(partialArguments); return(Render(filename, templateArguments)); }