internal string Render(TemplateAST ast) { TemplateVisitor v = new TemplateVisitor(_ctx, _cfg); ast.Root.Accept(v); return(v.Result); }
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); } }
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); } }
/// <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); } }
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); } }