예제 #1
0
 private void RegisterHandlebarsTemplates()
 {
     if (!VeilStaticConfiguration.IsParserRegistered("handlebars"))
     {
         VeilStaticConfiguration.RegisterParser(new HandlebarsTemplateParserRegistration());
     }
     context.RegisterTemplate("role", "{{ this }}");
     context.RegisterTemplate("master", "Hello {{ Name }} {{body}} See Ya!");
 }
예제 #2
0
 private static Func <string, Type, SyntaxTreeNode> CreateIncludeParser(string parserKey, IVeilContext context)
 {
     return((includeName, modelType) =>
     {
         var template = context.GetTemplateByName(includeName, parserKey);
         if (template == null)
         {
             throw new InvalidOperationException("Unable to load template '{0}' using parser '{1}'".FormatInvariant(includeName, parserKey));
         }
         return VeilStaticConfiguration.GetParserInstance(parserKey).Parse(template, modelType);
     });
 }
예제 #3
0
 private void RegisterSuperSimpleTemplates()
 {
     if (!VeilStaticConfiguration.IsParserRegistered("supersimple"))
     {
         VeilStaticConfiguration.RegisterParser(new SuperSimpleParserRegistration());
     }
     context.RegisterTemplate("Role", "@Current;");
     context.RegisterTemplate("Roles", "<ul>@Each.Current;<li>@Partial['Role'];</li>@EndEach;</ul>");
     context.RegisterTemplate("Department", "@Model.DepartmentName @Model.Company.CompanyName");
     context.RegisterTemplate("Master", "Hello @Model.Name; @Section['Middle'] See Ya!");
     context.RegisterTemplate("MiddleMaster", "@Master['Master'] @Section['Middle']from @Model.Department.DepartmentName @Section['Content'];@EndSection");
 }
예제 #4
0
        private static ITemplateParser GetParser(string parserKey)
        {
            if (String.IsNullOrEmpty(parserKey))
            {
                throw new ArgumentNullException("parserKey");
            }
            if (!VeilStaticConfiguration.IsParserRegistered(parserKey))
            {
                throw new ArgumentException("A parser with key '{0}' is not registered.".FormatInvariant(parserKey), "parserKey");
            }

            var parser = VeilStaticConfiguration.GetParserInstance(parserKey);

            return(parser);
        }
예제 #5
0
        /// <summary>
        /// Parses and compiles the specified template
        /// </summary>
        /// <typeparam name="T">The type of the model that will be passed to the template</typeparam>
        /// <param name="parserKey">Key of the <see cref="ITemplateParser"/> to use to parse the template.</param>
        /// <param name="templateContents">The contents of the template to compile</param>
        /// <returns>A compiled action ready to be executed as needed to render the template</returns>
        public Action <TextWriter, T> Compile <T>(string parserKey, TextReader templateContents)
        {
            if (String.IsNullOrEmpty(parserKey))
            {
                throw new ArgumentNullException("parserKey");
            }
            if (templateContents == null)
            {
                throw new ArgumentNullException("templateContents");
            }
            if (!VeilStaticConfiguration.IsParserRegistered(parserKey))
            {
                throw new ArgumentException("A parser with key '{0}' is not registered.".FormatInvariant(parserKey), "parserKey");
            }

            var parser     = VeilStaticConfiguration.GetParserInstance(parserKey);
            var syntaxTree = parser.Parse(templateContents, typeof(T));

            return(new VeilTemplateCompiler <T>(CreateIncludeParser(parserKey, context)).Compile(syntaxTree));
        }