/// <summary>
        /// Registers a template of type RazorTemplateBase to compile.
        /// </summary>
        /// <typeparam name="T">Type of RazorTemplateBase.</typeparam>
        /// <param name="templateID">The template's unique identifier.</param>
        /// <param name="templateString">The template string to compile.</param>
        /// <param name="namespaces">The namespaces to add to the template.</param>
        public void RegisterTemplate <T>(string templateID, string templateString, IEnumerable <string> namespaces, DateTime templateLastUpdated) where T : RazorTemplateBase
        {
            if (templateString == null)
            {
                throw new ArgumentNullException("templateString");
            }

            string key = TranslateKey(typeof(T), templateID);

            RazorTemplateEntry entry = new RazorTemplateEntry()
            {
                TemplateType    = typeof(T),
                TemplateID      = templateID,
                TemplateUpdated = templateLastUpdated,
                TemplateString  = templateString,
                TemplateName    = "Rzr" + Guid.NewGuid().ToString("N") + "Template",
                Namespaces      = namespaces
            };

            if (!TemplateItems.ContainsKey(key))
            {
                TemplateItems[key] = entry;
            }
            else
            {
                if (TemplateItems[key].TemplateUpdated < templateLastUpdated || TemplateItems[key].Assembly == null)
                {
                    TemplateItems[key] = entry;
                }
            }
        }
        /// <summary>
        /// Removes a template from the entry list.
        /// </summary>
        /// <param name="templateID"></param>
        public void RemoveTemplate(string templateID)
        {
            RazorTemplateEntry entryToRemove = TemplateItems.Where(e => e.Value.TemplateID.Equals(templateID)).Select(e => e.Value).FirstOrDefault();

            if (entryToRemove != null)
            {
                TemplateItems.Remove(TranslateKey(entryToRemove.TemplateType, entryToRemove.TemplateID));
            }
        }
Пример #3
0
        /// <summary>
        /// Generates the code from a C# razor template.
        /// </summary>
        /// <param name="entry">The RazorTemplateEntry instance.</param>
        /// <returns>The GeneratorResults object.</returns>
        private static GeneratorResults GenerateCode(RazorTemplateEntry entry)
        {
            RazorEngineHost host = new RazorEngineHost(new CSharpRazorCodeLanguage());

            host.DefaultBaseClass      = entry.TemplateType.FullName;
            host.DefaultNamespace      = "Tridion.Extensions.Mediators.Razor.Templating";
            host.DefaultClassName      = entry.TemplateName;
            host.GeneratedClassContext = new System.Web.Razor.Generator.GeneratedClassContext("Execute", "Write", "WriteLiteral", "WriteTo", "WriteLiteralTo", "Tridion.Extensions.Mediators.Razor.Templating.TemplateWriter", "DefineSection");
            host.NamespaceImports.Add("System");
            foreach (string ns in entry.Namespaces)
            {
                host.NamespaceImports.Add(ns);
            }

            GeneratorResults razorResult = null;

            using (TextReader reader = new StringReader(entry.TemplateString))
            {
                razorResult = new RazorTemplateEngine(host).GenerateCode(reader);
            }

            return(razorResult);
        }
        /// <summary>
        /// Generates the RazorTemplate that is ready to be executed.
        /// </summary>
        /// <typeparam name="T">Type of RazorTemplateBase</typeparam>
        /// <returns>The RazorTemplate that is ready to execute.</returns>
        public T GenerateTemplate <T>(string templateID) where T : RazorTemplateBase
        {
            RazorTemplateEntry entry = null;

            if (TemplateItems.Count == 0)
            {
                throw new InvalidOperationException("Templates have not been compiled.");
            }

            try
            {
                entry = TemplateItems[TranslateKey(typeof(T), templateID)];
            }
            catch (KeyNotFoundException)
            {
                throw new ArgumentOutOfRangeException("No template has been registered under name.");
            }


            var template = (T)entry.Assembly.CreateInstance("Tridion.Extensions.Mediators.Razor.Templating." + entry.TemplateName);

            return(template);
        }
        /// <summary>
        /// Generates the code from a C# razor template.
        /// </summary>
        /// <param name="entry">The RazorTemplateEntry instance.</param>
        /// <returns>The GeneratorResults object.</returns>
        private static GeneratorResults GenerateCode(RazorTemplateEntry entry)
        {
            RazorEngineHost host = new RazorEngineHost(new CSharpRazorCodeLanguage());
            host.DefaultBaseClass = entry.TemplateType.FullName;
            host.DefaultNamespace = "Tridion.Extensions.Mediators.Razor.Templating";
            host.DefaultClassName = entry.TemplateName;
            host.GeneratedClassContext = new System.Web.Razor.Generator.GeneratedClassContext("Execute", "Write", "WriteLiteral", "WriteTo", "WriteLiteralTo", "Tridion.Extensions.Mediators.Razor.Templating.TemplateWriter", "DefineSection");
            host.NamespaceImports.Add("System");
            foreach (string ns in entry.Namespaces)
            {
                host.NamespaceImports.Add(ns);
            }

            GeneratorResults razorResult = null;
            using (TextReader reader = new StringReader(entry.TemplateString))
            {
                razorResult = new RazorTemplateEngine(host).GenerateCode(reader);
            }

            return razorResult;
        }