protected void Application_Start(object sender, EventArgs e) { string path; if (System.Web.HttpRuntime.AppDomainAppPath.EndsWith("\\")) path = string.Concat(System.Web.HttpRuntime.AppDomainAppPath, "templets\\Green\\"); else path = string.Concat(System.Web.HttpRuntime.AppDomainAppPath, "\\templets\\Green\\"); //设定资源路径(原1.1中是TemplateContext.Paths) Resources.Paths.Add(path); //设置基本数据 在这里配置好,无须每个页面再配置 TemplateContext ctx = new TemplateContext(); ctx.TempData.Push("Site",new { Name = "jntemplate", //网站名称 Title = "jntemplate 演示站点",//首页TITLE Keywords = "jntemplate",//首页Keywords, Author = "jiniannet.com", Description = "asp.net 开源模板引擎", //首页Description Url = "/",//网站URL TemplateUrl = "/templets/Green/" }); Engine engine = new Engine(ctx); BuildManager.Engines.Add(engine); }
/// <summary> /// 引擎配置 /// </summary> /// <param name="conf">配置内容</param> /// <param name="ctx">默认模板上下文</param> public static void Configure(EngineConfig conf, TemplateContext ctx) { if (conf == null) { throw new ArgumentNullException("conf"); } if (conf.TagParsers == null) { conf.TagParsers = conf.TagParsers = Field.RSEOLVER_TYPES; } _context = ctx; ITagParser[] parsers = new ITagParser[conf.TagParsers.Length]; for (Int32 i = 0; i < conf.TagParsers.Length; i++) { parsers[i] = (ITagParser)Activator.CreateInstance(Type.GetType(conf.TagParsers[i])); } ICache cache = null; if (!string.IsNullOrEmpty(conf.CachingProvider)) { cache = (ICache)Activator.CreateInstance(Type.GetType(conf.CachingProvider)); } Parser.TagTypeResolver resolver = new Parser.TagTypeResolver(parsers); _engineRuntime = new Runtime(resolver, cache, conf); }
/// <summary> /// Template /// </summary> /// <param name="ctx">TemplateContext 对象</param> /// <param name="text">模板内容</param> public Template(TemplateContext ctx, String text) { if (ctx == null) { throw new ArgumentNullException("\"ctx\" cannot be null."); } Context = ctx; TemplateContent = text; }
/// <summary> /// Template /// </summary> /// <param name="ctx">TemplateContext 对象</param> /// <param name="text">模板内容</param> public Template(TemplateContext ctx, String text) { if (ctx == null) { throw new System.ArgumentNullException("ctx"); } Context = ctx; TemplateContent = text; }
/// <summary> /// 从指定TemplateContext创建一个类似的实例 /// </summary> /// <param name="context"></param> /// <returns></returns> public static TemplateContext CreateContext(TemplateContext context) { TemplateContext ctx = new TemplateContext(); ctx.TempData = new VariableScope(context.TempData); ctx.Charset = context.Charset; //ctx.CurrentPath = context.CurrentPath; ctx.ThrowExceptions = context.ThrowExceptions; return ctx; }
public Engine(TemplateContext context) { if (context == null) { _ctx = new TemplateContext(); } else { _ctx = context; } }
/// <summary> /// 从指定路径加载模板 /// </summary> /// <param name="path">模板文件</param> /// <param name="ctx">模板上下文</param> /// <returns>ITemplate</returns> public static ITemplate LoadTemplate(String path, TemplateContext ctx) { Template template = new Template(ctx, null); if (!String.IsNullOrEmpty(path)) { String fullPath = path; //判断是否本地路径的绝对形式 if (fullPath.IndexOf(System.IO.Path.VolumeSeparatorChar) != -1 //win下判断是否包含卷分隔符(即:) c:\user\Administrator\default.html || fullPath[0] == '/') //liunx判断是否为/开头,/usr/share/doc/default.html win下请不要使用/开头的路径 { ctx.CurrentPath = System.IO.Path.GetDirectoryName(fullPath); template.TemplateContent = Resources.Load(fullPath, template.Context.Charset); } else { Int32 index = Resources.FindPath(path, out fullPath); //如果是相对路径,则进行路径搜索 if (index != -1) { //设定当前工作目录 如果模板中存在Inclub或load标签,它们的处理路径会以CurrentPath 设定的路径为基准 ctx.CurrentPath = Runtime.ResourceDirectories[index]; template.TemplateContent = Resources.Load(fullPath, template.Context.Charset); } } } return template; }
/// <summary> /// 创建模板上下文 /// </summary> /// <returns></returns> public static TemplateContext CreateContext() { TemplateContext ctx; if (_context == null) { ctx = new TemplateContext(); ctx.Charset = Encoding.Default; ctx.StripWhiteSpace = Runtime.StripWhiteSpace; ctx.ThrowExceptions = Runtime.ThrowExceptions; } else { ctx = TemplateContext.CreateContext(_context); } return ctx; }
public Template(TemplateContext context, String text) { this._context = context; this._text = text; }
public static TemplateContext CreateContext(TemplateContext context) { TemplateContext ctx = (TemplateContext)context.Clone(); ctx.TempData = new VariableScope(context.TempData); return ctx; }
/// <summary> /// 从指定TemplateContext创建一个类似的实例 /// </summary> /// <param name="context"></param> /// <returns>TemplateContext</returns> public static TemplateContext CreateContext(TemplateContext context) { if (context == null) { throw new ArgumentNullException("context"); } TemplateContext ctx = new TemplateContext(); ctx.TempData = new VariableScope(context.TempData); ctx.Charset = context.Charset; ctx.CurrentPath = context.CurrentPath; ctx.ThrowExceptions = context.ThrowExceptions; ctx.StripWhiteSpace = context.StripWhiteSpace; return ctx; }
/// <summary> /// 从指定路径加载模板 /// </summary> /// <param name="path">模板文件</param> /// <param name="ctx">模板上下文</param> /// <returns>ITemplate</returns> public static ITemplate LoadTemplate(String path, TemplateContext ctx) { String fullPath; String text = Resources.Load(ResourceDirectories, path, ctx.Charset, out fullPath); Template template = new Template(ctx, text); if (fullPath != null) { template.TemplateKey = fullPath; ctx.CurrentPath = System.IO.Path.GetDirectoryName(fullPath); } return template; }
/// <summary> /// Initializes a new instance of the <see cref="Template"/> class /// </summary> /// <param name="context">The <see cref="TemplateContext"/>.</param> /// <param name="reader">The template contents.</param> public Template(TemplateContext context, IReader reader) { this.Context = context; this.reader = reader; }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="tag"></param> /// <param name="ctx"></param> /// <returns></returns> private static object Execute(this TemplateContext ctx, string name, ITag tag) { var func = ctx.ExecutorBuilder.Build(name); return(func(tag, ctx)); }
/// <summary> /// Execute the tags. /// </summary> /// <param name="tag">The <see cref="ITag"/>.</param> /// <param name="ctx">The <see cref="TemplateContext"/>.</param> /// <returns></returns> public static object Execute(this TemplateContext ctx, ITag tag) { var func = ctx.ExecutorBuilder.Build(tag); return(func(tag, ctx)); }
/// <summary> /// Initializes a new instance of the <see cref="CompileTemplate"/> class /// </summary> /// <param name="context">The <see cref="TemplateContext"/>.</param> /// <param name="text">The template contents.</param> public CompileTemplate(TemplateContext context, string text) { Context = context; TemplateContent = text; }