Ejemplo n.º 1
0
        /// <summary>
        /// Load a new engine instance and setup it up, parse template and generate code
        /// on a GeneratorResults form (this allows to execute it after)
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        private static GeneratorResults GenerateSourceCodeResult(string template)
        {
            RazorTemplateEngine engine      = RazorEngineHelper.SetupRazorEngine();
            GeneratorResults    razorResult = null;

            using (TextReader rdr = new StringReader(template))
            {
                razorResult = engine.GenerateCode(rdr);
            }
            return(razorResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Render an inline Razor template with the data given as model
        /// </summary>
        /// <param name="template">string with Razor syntax inside</param>
        /// <param name="model">any object model, even anonymous types</param>
        /// <returns></returns>
        public string RenderString(string template, dynamic model)
        {
            //this one works :
            string result = RazorEngineHelper.ProcessTemplateWithAnonymous(template, model);

            //does not work:
            //string result = RazorEngineHelper.ProcessTemplate(template, model);

            if (!string.IsNullOrEmpty(result))
            {
                return(result);
            }
            return(string.Empty);
        }