Пример #1
0
        /// <summary>
        /// Parses a string
        /// </summary>
        /// <param name="content">Template to parse</param>
        /// <param name="model">Template model</param>
        /// <param name="modelType">Type of the model</param>
        /// <returns></returns>
        public static string ParseString(this IRazorLightEngine engine, string content, object model, Type modelType)
        {
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException(nameof(content));
            }

            ITemplateSource templateSource = new LoadedTemplateSource(content);

            ModelTypeInfo     modelTypeInfo = new ModelTypeInfo(modelType);
            CompilationResult result        = engine.Core.CompileSource(templateSource, modelTypeInfo);

            result.EnsureSuccessful();

            TemplatePage page = engine.Activate(result.CompiledType);

            page.PageContext = new PageContext()
            {
                ModelTypeInfo = modelTypeInfo
            };

            return(engine.RunTemplate(page, model));
        }