예제 #1
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);
     });
 }
예제 #2
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);
        }
예제 #3
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));
        }