Ejemplo n.º 1
0
        /// <summary>
        /// Transforms the template from plain source and inserts variables.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="callerTemplate"></param>
        /// <returns></returns>
        public string TransformTemplate(string input, SpartanTemplate callerTemplate)
        {
            // Create a template instance and populate it with data.
            Template template = Template.Parse(input);

            var context = new TemplateContext()
            {
                TemplateLoader = new DefaultTemplateLoader()
            };

            var tempParams = new ScriptObject();

            foreach (var pair in callerTemplate.TemplateContext)
            {
                tempParams.Add(pair.Key, pair.Value);
            }

            // TODO: Clean this up!
            context.PushGlobal(tempParams);

            string result = template.Render(context);

            // Return the string.
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gives an instance of SpartanTemplate with a content variable already included.
        /// </summary>
        /// <param name="basePagePath"></param>
        /// <param name="contentPagePath"></param>
        /// <param name="templatingProcessor"></param>
        /// <param name="view"></param>
        public SpartanTemplate(string basePagePath, string contentPagePath, TemplatingProcessor templatingProcessor,
                               IView view)
        {
            // Define the FileInfo objects we will later use to read the templates.
            FileInfo basePageFile    = new FileInfo($"_Templates/{basePagePath}");
            FileInfo contentPageFile = new FileInfo($"_Templates/{contentPagePath}");

            // Load using the base page first
            Load(templatingProcessor, basePageFile, view);

            #region Setup content
            // Define a new template
            SpartanTemplate contentTemplate = new SpartanTemplate(contentPageFile, templatingProcessor, view);
            TemplateContext.Add("content", contentTemplate.Render());
            #endregion
        }