Context
Inheritance: ICloneable
Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
 /// <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;
 }
Exemplo n.º 4
0
 /// <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;
 }
Exemplo n.º 5
0
 /// <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;
 }
Exemplo n.º 6
0
 public Engine(TemplateContext context)
 {
     if (context == null)
     {
         _ctx = new TemplateContext();
     }
     else
     {
         _ctx = context;
     }
 }
Exemplo n.º 7
0
        /// <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;
        }
Exemplo n.º 8
0
        /// <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;
        }
Exemplo n.º 9
0
 public Template(TemplateContext context, String text)
 {
     this._context = context;
     this._text = text;
 }
Exemplo n.º 10
0
 public static TemplateContext CreateContext(TemplateContext context)
 {
     TemplateContext ctx = (TemplateContext)context.Clone();
     ctx.TempData = new VariableScope(context.TempData);
     return ctx;
 }
Exemplo n.º 11
0
 /// <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;
 }
Exemplo n.º 12
0
 /// <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;
 }
Exemplo n.º 13
0
 /// <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;
 }
Exemplo n.º 14
0
        /// <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));
        }
Exemplo n.º 15
0
        /// <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));
        }
Exemplo n.º 16
0
 /// <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;
 }