예제 #1
0
        internal string Render(TemplateAST ast)
        {
            TemplateVisitor v = new TemplateVisitor(_ctx, _cfg);

            ast.Root.Accept(v);
            return(v.Result);
        }
예제 #2
0
        public TemplateAST Parse(string path)
        {
            string templateFile = Path.Combine(_engine.Path, path);

            _log.Debug("Begin parse template file [{0}].", templateFile);

            if (!File.Exists(templateFile))
            {
                ExceptionHelper.ThrowFileNotFound(templateFile);
            }

            using (StreamReader r = new StreamReader(templateFile))
            {
                string        template = r.ReadToEnd();
                LexicalParser lp       = new LexicalParser();
                lp.SetParseContent(template);
                SemanticParser sp = new SemanticParser();
                sp.SetParseContent(lp.Parse());
                TemplateAST ast = sp.Parse();

                _log.Debug("Parse template file [{0}] success.", templateFile);

                ParseIncludeTemplate(ast);
                return(ast);
            }
        }
예제 #3
0
        private void ParseIncludeTemplate(TemplateAST ast)
        {
            IncludeNodeVisitor v = new IncludeNodeVisitor();

            ast.Root.Accept(v);

            foreach (IncludeNode inc in v.Includes)
            {
                TemplateParser inner    = new TemplateParser(_engine);
                TemplateAST    innerAst = inner.Parse(inc.Path.UnBracketing(StringPair.DoubleQuote));
                inc.Parse(innerAst);
            }
        }
예제 #4
0
        /// <summary>
        /// Render a template
        /// </summary>
        /// <param name="fileName">Only file name here, no directory info, otherwise will cause Exception</param>
        /// <returns></returns>
        public string Render(string fileName)
        {
            fileName.ThrowIfNullArgument(nameof(fileName));
            if (fileName.IndexOf(System.IO.Path.DirectorySeparatorChar) != -1 || fileName.IndexOf(System.IO.Path.AltDirectorySeparatorChar) != -1)
            {
                ExceptionHelper.ThrowOnlyFileNameAllowed(fileName);
            }

            try
            {
                TemplateAST ast = _cache.Get(fileName);
                if (ast == null)
                {
                    ast = this.Parse(fileName);
                    _cache.Put(fileName, ast);
                }
                return(this.Render(ast));
            }
            catch (ELParseException)
            {
                throw;
            }
            catch (EvalException)
            {
                throw;
            }
            catch (TemplateParseException)
            {
                throw;
            }
            catch (TemplateRenderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowWrapped(ex);
                return(string.Empty);
            }
        }
예제 #5
0
        public string RenderRaw(string textTemplate)
        {
            textTemplate.ThrowIfNullArgument(nameof(textTemplate));

            try
            {
                LexicalParser lp = new LexicalParser();
                lp.SetParseContent(textTemplate);
                SemanticParser sp = new SemanticParser();
                sp.SetParseContent(lp.Parse());
                TemplateAST ast = sp.Parse();

                return(this.Render(ast));
            }
            catch (ELParseException)
            {
                throw;
            }
            catch (EvalException)
            {
                throw;
            }
            catch (TemplateParseException)
            {
                throw;
            }
            catch (TemplateRenderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowWrapped(ex);
                return(string.Empty);
            }
        }