CreateContext() public static method

从指定TemplateContext创建一个类似的实例
public static CreateContext ( TemplateContext context ) : TemplateContext
context TemplateContext
return TemplateContext
コード例 #1
0
        /// <summary>
        /// 根据指定路径创建Template实例
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public ITemplate CreateTemplate(String path, Encoding encoding)
        {
            TemplateContext ctx      = TemplateContext.CreateContext(context);
            Template        template = new Template(ctx, null);

            if (encoding != null)
            {
                template.Context.Charset = encoding;
            }
            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 (Resources.FindPath(path, out fullPath) != -1)
                    {
                        //设定当前工作目录 如果模板中存在Inclub或load标签,它们的处理路径会以CurrentPath 设定的路径为基准
                        ctx.CurrentPath          = Resources.Paths[index];
                        template.TemplateContent = Resources.Load(fullPath, template.Context.Charset);
                    }
                }
            }

            return(template);
        }