Exemplo n.º 1
0
        /// <summary>
        /// Renders a template from a string input to a file output.
        /// Same text templates are compiled and cached for re-use.
        /// </summary>
        /// <param name="templateText">Text of the template to run</param>
        /// <param name="context">Optional context to pass</param>
        /// <param name="outputFile">Output file where output is sent to</param>
        /// <returns></returns>
        public bool RenderTemplateToFile(string templateText, object context, string outputFile)
        {
            CompiledAssemblyItem assItem = GetAssemblyFromStringAndCache(templateText);

            if (assItem == null)
            {
                return(false);
            }

            StreamWriter writer = null;

            try
            {
                writer = new StreamWriter(outputFile, false,
                                          Engine.Configuration.OutputEncoding,
                                          Engine.Configuration.StreamBufferSize);
            }
            catch (Exception ex)
            {
                SetError("Unable to write template output to " + outputFile + ": " + ex.Message);
                return(false);
            }

            return(this.RenderTemplateFromAssembly(assItem.AssemblyId, context, writer));
        }
        /// <summary>
        /// Call this method to actually render a template to the specified outputfile
        /// </summary>"
        /// <param name="templateText">The template text to parse and render</param>
        /// <param name="model">
        /// Any object that will be available in the template as a dynamic of this.Context or
        /// if the type matches the template type this.Model.
        /// </param>
        /// <param name="writer">Optional textwriter that output is written to</param>
        /// <param name="inferModelType">If true infers the model type if no @model or @inherits tag is provided</param>
        /// <returns>rendering results or null on failure. If a writer is a passed string.Empty is returned or null for failure</returns>
        public string RenderTemplate(string templateText,
                                     object model        = null,
                                     TextWriter writer   = null,
                                     bool inferModelType = false)
        {
            if (inferModelType && model != null &&
                !templateText.Trim().StartsWith("@model ") &&
                !templateText.Trim().StartsWith("@inherits "))
            {
                templateText = "@model " + model.GetType().FullName + "\r\n" + templateText;
            }

            CompiledAssemblyItem assItem = GetAssemblyFromStringAndCache(templateText);

            if (assItem == null)
            {
                return(null);
            }

            // String result will be empty as output will be rendered into the
            // Response object's stream output. However a null result denotes
            // an error
            string result = Engine.RenderTemplateFromAssembly(assItem.AssemblyId, model, writer);

            if (result == null)
            {
                ErrorMessage = Engine.ErrorMessage;
                return(null);
            }

            return(result);
        }
        /// <summary>
        /// Renders a template from a string input to a file output.
        /// Same text templates are compiled and cached for re-use.
        /// </summary>
        /// <param name="templateText">Text of the template to run</param>
        /// <param name="model">Optional model to pass</param>
        /// <param name="outputFile">Output file where output is sent to</param>
        /// <param name="inferModelType">If true infers the model type if no @model or @inherits tag is provided</param>
        /// <returns></returns>
        public bool RenderTemplateToFile(string templateText, object model, string outputFile, bool inferModelType = false)
        {
            if (inferModelType && model != null &&
                !templateText.Trim().StartsWith("@model ") &&
                !templateText.Trim().StartsWith("@inherits "))
            {
                templateText = "@model " + model.GetType().FullName + "\r\n" + templateText;
            }

            CompiledAssemblyItem assItem = GetAssemblyFromStringAndCache(templateText);

            if (assItem == null)
            {
                return(false);
            }

            StreamWriter writer = null;

            try
            {
                writer = new StreamWriter(outputFile, false,
                                          Engine.Configuration.OutputEncoding,
                                          Engine.Configuration.StreamBufferSize);
            }
            catch (Exception ex)
            {
                SetError("Unable to write template output to " + outputFile + ": " + ex.Message);
                return(false);
            }

            return(RenderTemplateFromAssembly(assItem.AssemblyId, model, writer));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Internally tries to retrieve a previously compiled template from cache
        /// if not found compiles a template into an assembly
        /// always returns an assembly id as a string.
        /// </summary>
        /// <param name="templateText">The text to parse</param>
        /// <returns>assembly id as a string or null on error</returns>
        protected virtual CompiledAssemblyItem GetAssemblyFromStringAndCache(string templateText)
        {
            int hash = templateText.GetHashCode();

            CompiledAssemblyItem item = null;

            this.LoadedAssemblies.TryGetValue(hash, out item);

            string assemblyId = null;

            // Check for cached instance
            if (item != null)
            {
                assemblyId = item.AssemblyId;
            }
            else
            {
                item = new CompiledAssemblyItem();
            }

            // No cached instance - create assembly and cache
            if (assemblyId == null)
            {
                string safeClassName = GetSafeClassName(null);
                assemblyId = Engine.CompileTemplate(templateText, GeneratedNamespace, safeClassName);

                if (assemblyId == null)
                {
                    this.ErrorMessage = Engine.ErrorMessage;
                    return(null);
                }

                item.AssemblyId     = assemblyId;
                item.CompileTimeUtc = DateTime.UtcNow;
                item.SafeClassName  = safeClassName;

                this.LoadedAssemblies[hash] = item;
            }

            return(item);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Call this method to actually render a template to the specified outputfile
        /// </summary>"
        /// <param name="templateText">The template text to parse and render</param>
        /// <param name="model">
        /// Any object that will be available in the template as a dynamic of this.Context or
        /// if the type matches the template type this.Model.
        /// </param>
        /// <returns>rendering results or null on failure. If a writer is a passed string.Empty is returned or null for failure</returns>
        public string RenderTemplate(string templateText, object model = null, TextWriter writer = null)
        {
            CompiledAssemblyItem assItem = GetAssemblyFromStringAndCache(templateText);

            if (assItem == null)
            {
                return(null);
            }

            // String result will be empty as output will be rendered into the
            // Response object's stream output. However a null result denotes
            // an error
            string result = Engine.RenderTemplateFromAssembly(assItem.AssemblyId, model, writer);

            if (result == null)
            {
                this.ErrorMessage = Engine.ErrorMessage;
                return(null);
            }

            return(result);
        }