The template context contains the state of the page, the model.
Exemple #1
0
        /// <summary>
        /// Renders this template using the specified context. See remarks.
        /// </summary>
        /// <param name="context">The template context.</param>
        /// <exception cref="System.ArgumentNullException">If context is null</exception>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        /// <remarks>
        /// When using this method, the result of rendering this page is output to <see cref="TemplateContext.Output"/>
        /// </remarks>
        public void Render(TemplateContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (HasErrors) throw new InvalidOperationException("This template has errors. Check the <Messages> property for more details");

            // Make sure that we are using the same options
            if (SourceFilePath != null)
            {
                context.PushSourceFile(SourceFilePath);
            }

            try
            {
                context.Result = null;
                Page?.Evaluate(context);

                if (Page != null && context.EnableOutput && context.Result != null)
                {
                    context.Write(Page.Span, context.Result);
                    context.Result = null;
                }
            }
            finally
            {
                if (SourceFilePath != null)
                {
                    context.PopSourceFile();
                }
            }
        }
        public static string RenderJson(string json, string content)
        {
            var expando     = JsonConvert.DeserializeObject <ExpandoObject>(json);
            var sObject     = BuildScriptObject(expando);
            var templateCtx = new Scriban.TemplateContext();

            templateCtx.PushGlobal(sObject);
            var template = Scriban.Template.Parse(content);
            var result   = template.Render(templateCtx);

            return(result);
        }
Exemple #3
0
    public string Generate(Type recordType)
    {
        var             typeInfo     = new TypeInfo(recordType);
        ISet <TypeInfo> dependencies = new HashSet <TypeInfo>();

        typeInfo.CollectDependencies(dependencies);
        var scriptObject = new ScriptObject();

        scriptObject["type"]         = typeInfo;
        scriptObject["dependencies"] = dependencies;
        var context = new Scriban.TemplateContext();

        context.PushGlobal(scriptObject);
        return(template.Render(context));
    }
Exemple #4
0
        /// <summary>
        /// Renders this template using the specified object model.
        /// </summary>
        /// <param name="model">The object model.</param>
        /// <returns>A rendering result as a string </returns>
        public string Render(object model = null)
        {
            var scriptObject = new ScriptObject();
            if (model != null)
            {
                scriptObject.Import(model);
            }

            var context = new TemplateContext();
            context.PushGlobal(scriptObject);
            Render(context);
            context.PopGlobal();

            return context.Output.ToString();
        }