PushGlobal() public method

Pushes a new model context accessible to the template.
public PushGlobal ( IScriptObject scriptObject ) : void
scriptObject IScriptObject The script object.
return void
        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);
        }
Exemplo n.º 2
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));
    }
Exemplo n.º 3
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());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Evaluates the template using the specified context
        /// </summary>
        /// <param name="model">An object model to use with the evaluation.</param>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        /// <returns>Returns the result of the last statement</returns>
        public object Evaluate(object model = null)
        {
            var scriptObject = new ScriptObject();

            if (model != null)
            {
                scriptObject.Import(model);
            }

            var context = new TemplateContext {
                EnableOutput = false
            };

            context.PushGlobal(scriptObject);
            var result = Evaluate(context);

            context.PopGlobal();
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Evaluates the template using the specified context
        /// </summary>
        /// <param name="model">An object model to use with the evaluation.</param>
        /// <param name="memberRenamer">The member renamer used to import this .NET object and transitive objects. See member renamer documentation for more details.</param>
        /// <param name="memberFilter">The member filter used to filter members for .NET objects being accessed through the template, including the model being passed to this method.</param>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        /// <returns>Returns the result of the last statement</returns>
        public object Evaluate(object model = null, MemberRenamerDelegate memberRenamer = null, MemberFilterDelegate memberFilter = null)
        {
            var scriptObject = new ScriptObject();

            if (model != null)
            {
                scriptObject.Import(model, renamer: memberRenamer, filter: memberFilter);
            }

            var context = new TemplateContext
            {
                EnableOutput  = false,
                MemberRenamer = memberRenamer,
                MemberFilter  = memberFilter
            };

            context.PushGlobal(scriptObject);
            var result = Evaluate(context);

            context.PopGlobal();
            return(result);
        }
Exemplo n.º 6
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();
        }