Пример #1
0
        /// <summary>合并</summary>
        /// <param name="context">上下文环境</param>
        /// <param name="templatePath">模板相对路径</param>
        /// <returns></returns>
        public string Merge(VelocityContext context, string templatePath)
        {
            try
            {
                if (engine.TemplateExists(templatePath))
                {
                    Template template = engine.GetTemplate(templatePath);

                    return(Merge(context, template));
                }
                else
                {
                    return("Can't find \"" + templatePath + "\".");
                }
            }
            catch
            {
                // 发生内部错误, 重启引擎.
                instance = null;

                engine = null;

                return(string.Empty);
            }
        }
        private Template ResolveMasterTemplate(string masterName)
        {
            Template masterTemplate = null;

            if (!string.IsNullOrEmpty(masterName))
            {
                string targetMaster = Path.Combine(_masterFolder, masterName);

                if (!Path.HasExtension(targetMaster))
                {
                    targetMaster += ".vm";
                }

                if (!_engine.TemplateExists(targetMaster))
                {
                    throw new InvalidOperationException("Could not find view for master template named " + masterName +
                                                        ". I searched for '" + targetMaster + "' file. Maybe the file doesn't exist?");
                }

                masterTemplate = _engine.GetTemplate(targetMaster);
            }
            return(masterTemplate);
        }
Пример #3
0
        Template ResolveTemplate(string name)
        {
            if (!Path.HasExtension(name))
            {
                name += ".vm";
            }

            if (!velocityEngine.TemplateExists(name))
            {
                throw new InvalidOperationException(string.Format("Could not find a template named '{0}'", name));
            }

            return(velocityEngine.GetTemplate(name));
        }
Пример #4
0
        /// <summary>
        /// 生成模板内容
        /// </summary>
        /// <param name="context">当前应用上下文</param>
        /// <param name="view">模板文件</param>
        /// <returns></returns>
        public string ParseTemplate(VelocityContext context, string view)
        {
            string targetComponent = Path.Combine(componentFolder, view);

            if (!engine.TemplateExists(targetComponent))
            {
                throw new InvalidOperationException("没有找到模板文件 '" + targetComponent + "'");
            }

            //加载模板
            Template template = engine.GetTemplate(targetComponent);

            StringWriter writer = new StringWriter();

            template.Merge(context, writer);

            return(writer.GetStringBuilder().ToString());
        }