Merge() public method

The AST node structure is merged with the context to produce the final output. Throws IOException if failure is due to a file related issue, and Exception otherwise
/// if template not found from any available source. /// /// if template cannot be parsed due to syntax (or other) error. /// /// anything else. ///
public Merge ( IContext context, TextWriter writer ) : void
context IContext Context with data elements accessed by template
writer System.IO.TextWriter writer for rendered template
return void
Ejemplo n.º 1
0
 public static string TemplateMerge(string name, Hashtable table)
 {
     template = engine.GetTemplate(filePath+name);
     VelocityContext content = new VelocityContext();
     foreach (DictionaryEntry entry in table)
     {
         content.Put(entry.Key.ToString(), entry.Value);
     }
     StringWriter writer = new StringWriter();
     template.Merge(content, writer);
     return writer.GetStringBuilder().ToString();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成字符
        /// </summary>
        /// <param name="templatFileName">模板文件名</param>
        public string BuildString(string templateFile)
        {
            //从文件中读取模板
            // NVelocity.Template template = velocity.GetTemplate(templateFile);
            NVelocity.Template template = GetTemplate(templateFile);

            //合并模板
            StringWriter writer = new StringWriter();

            template.Merge(context, writer);
            return(writer.ToString());
        }
    public Example1(System.String templateFile)
    {
        try {
            /*
             * setup
             */
            Velocity.Init("nvelocity.properties");

            /*
             *  Make a context object and populate with the data.  This
             *  is where the Velocity engine gets the data to resolve the
             *  references (ex. $list) in the template
             */
            VelocityContext context = new VelocityContext();
            context.Put("list", Names);

            /*
             *  get the Template object.  This is the parsed version of your
             *  template input file.  Note that getTemplate() can throw
             *   ResourceNotFoundException : if it doesn't find the template
             *   ParseErrorException : if there is something wrong with the VTL
             *   Exception : if something else goes wrong (this is generally
             *        indicative of as serious problem...)
             */
            Template template = null;

            try {
                template = Velocity.GetTemplate(templateFile)
                ;
            } catch (ResourceNotFoundException) {
                System.Console.Out.WriteLine("Example1 : error : cannot find template " + templateFile);
            } catch (ParseErrorException pee) {
                System.Console.Out.WriteLine("Example1 : Syntax error in template " + templateFile + ":" + pee);
            }

            /*
             *  Now have the template engine process your template using the
             *  data placed into the context.  Think of it as a  'merge'
             *  of the template and the data to produce the output stream.
             */
            if (template != null)
            {
                template.Merge(context, System.Console.Out);
            }
        } catch (System.Exception e) {
            System.Console.Out.WriteLine(e);
        }
    }
        private void GeneratePage(Template template, int pageIndex)
        {
            VelocityContext.Put("pageIndex", pageIndex);
            string reportPath = Helper.Paging.GetReportPath(pageIndex);
            var stringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(stringBuilder))
            {
                template.Merge(VelocityContext, stringWriter);

                using (var fileWriter = new StreamWriter(ReportWriter.ReportContainer.OpenWrite(reportPath, ContentType, encoding)))
                {
                    fileWriter.Write(FormatHtmlHelper.Flatten(stringBuilder.ToString()));
                }
            }

            ReportWriter.AddReportDocumentPath(reportPath);
        }
        public static string ParseVelocity(string path, string name, IDictionary <string, object> parameters)
        {
            StringWriter       writer     = new StringWriter();
            ExtendedProperties properties = new ExtendedProperties();

            properties.AddProperty("resource.loader", "file");
            properties.AddProperty("file.resource.loader.path", path);
            VelocityEngine engine = new VelocityEngine();

            engine.Init(properties);
            IContext context = new VelocityContext();

            foreach (KeyValuePair <string, object> pair in parameters)
            {
                context.Put(pair.Key, pair.Value);
            }
            Template template = engine.GetTemplate(name);

            if (template != null)
            {
                template.Merge(context, writer);
            }
            return(writer.ToString());
        }
        public static StringWriter GetFileText(string path, Hashtable ht)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("Argument Excception!");
            }
            try
            {
                string tmpPath  = path.Substring(0, path.LastIndexOf(@"\"));
                string filePath = path.Substring(path.LastIndexOf(@"\") + 1);

                VelocityEngine     velocity = new VelocityEngine();
                ExtendedProperties props    = new ExtendedProperties();
                props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, tmpPath);
                props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
                props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
                props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
                velocity.Init(props);

                Template temp    = velocity.GetTemplate(filePath);
                IContext context = new VelocityContext();
                foreach (string key in ht.Keys)
                {
                    context.Put(key, ht[key]);
                }
                StringWriter writer = new StringWriter();
                temp.Merge(context, writer);

                return(writer);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 7
0
 private string MergeTemplate(Template template, VelocityContext context)
 {
     var writer =new StringWriter();
     template.Merge(context, writer);
     return writer.GetStringBuilder().ToString();
 }
Ejemplo n.º 8
0
        /// <summary>
        /// ��ʾģ��
        /// </summary>
        /// <param name="templatFileName">ģ���ļ���</param>
        public void Display(string templatFileName)
        {
            //���ļ��ж�ȡģ��
            Template template = new Template();
            try
            {
                template = velocity.GetTemplate(templatFileName);
            }
            catch (Exception ex)
            {

            }
            //�ϲ�ģ��
            StringWriter writer = new StringWriter();
            template.Merge(context, writer);
            //���
            //            HttpContext.Current.Response.Clear();
            //            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.Write(writer.ToString());
            if (JavaScript.Length > 0)
            {
                HttpContext.Current.Response.Write("<script" + ">" + JavaScript + "</" + "script>");
            }
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
Ejemplo n.º 9
0
	/// <summary>
	/// merges the template with the context.  Only override this if you really, really
	/// really need to. (And don't call us with questions if it breaks :)
	/// </summary>
	/// <param name="template">template object returned by the handleRequest() method</param>
	/// <param name="ctx">context created by the CreateContext() method</param>
	/// <param name="response">TextWriter to write to (i.e. Response.Output)</param>
	protected virtual void MergeTemplate(Template template, IContext ctx, TextWriter response) {
	    template.Merge(ctx, response);
	}