Exemplo n.º 1
0
        /// <summary>
        /// Includes the template with the specified name.
        /// </summary>
        /// <param name="name">The name of the template to include.</param>
        /// <param name="model">The model to pass to the template.</param>
        /// <returns>The template writer helper.</returns>
        public virtual TemplateWriter Include(string name, object model)
        {
            var instance = TemplateService.Resolve(name, model);

            if (instance == null)
            {
                throw new ArgumentException("No template could be resolved with name '" + name + "'");
            }

            return(new TemplateWriter(tw => tw.Write(instance.Run(new ExecuteContext()))));
        }
        /// <summary>
        /// Creates an instance of a <see cref="TemplateService"/>.
        /// </summary>
        /// <param name="configuration">The <see cref="TemplateServiceConfigurationElement"/> that represents the configuration.</param>
        /// <param name="defaultNamespaces">The enumerable of namespaces to add as default.</param>
        /// <returns>A new instance of <see cref="TemplateService"/>.</returns>
        public static TemplateService CreateTemplateService(TemplateServiceConfigurationElement configuration, IEnumerable <string> defaultNamespaces = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            ILanguageProvider provider = null;
            MarkupParser      parser   = null;
            Type templateBaseType      = null;

            if (!string.IsNullOrEmpty(configuration.LanguageProvider))
            {
                provider = (ILanguageProvider)GetInstance(configuration.LanguageProvider);
            }

            if (!string.IsNullOrEmpty(configuration.MarkupParser))
            {
                parser = (MarkupParser)GetInstance(configuration.MarkupParser);
            }

            if (!string.IsNullOrEmpty(configuration.TemplateBase))
            {
                templateBaseType = GetType(configuration.TemplateBase);
            }

            var namespaces = configuration.Namespaces
                             .Cast <NamespaceConfigurationElement>()
                             .Select(n => n.Namespace);

            if (defaultNamespaces != null)
            {
                namespaces = defaultNamespaces
                             .Concat(namespaces)
                             .Distinct();
            }

            var service = new TemplateService(provider, templateBaseType, parser);

            foreach (string ns in namespaces)
            {
                service.Namespaces.Add(ns);
            }

            return(service);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Resolves the layout template.
 /// </summary>
 /// <param name="name">The name of the layout template.</param>
 /// <returns>An instance of <see cref="ITemplate"/>.</returns>
 protected virtual ITemplate ResolveLayout(string name)
 {
     return(TemplateService.Resolve(name, null));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Resolves the layout template.
 /// </summary>
 /// <param name="name">The name of the layout template.</param>
 /// <returns>An instance of <see cref="ITemplate"/>.</returns>
 protected override ITemplate ResolveLayout(string name)
 {
     return(TemplateService.Resolve(name, (T)currentModel));
 }
Exemplo n.º 5
0
 public void InitializeService(TemplateServiceConfiguration pageConfiguration)
 {
     PageTemplateService = new RazorEngine.Templating.TemplateService(pageConfiguration);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Parses a string using the the razor MVC view page tempalte .
 /// </summary>
 /// <param name="string_to_parse">The string_to_parse.</param>
 /// <returns></returns>
 public static string ParseRazorMVCViewPageTempalte(string string_to_parse)
 {
     if(string_to_parse == null) { return String.Empty; }
     if(string_to_parse == String.Empty) { return String.Empty; }
     var config = new RazorEngine.Configuration.TemplateServiceConfiguration();
     config.Namespaces.Add("Rendition");
     var service = new RazorEngine.Templating.TemplateService(config);
     string rParseResult = "";
     try{
         rParseResult = Razor.Parse(string_to_parse);
     }catch(Exception ex){
         if(!WasThreadAbortedOrClosed(ex)) {
             showHTMLErrorOnRewriteException(getInnermostException(ex), HttpContext.Current);
         }
     }
     return rParseResult;
 }
Exemplo n.º 7
0
 public void InitializeService(TemplateServiceConfiguration pageConfiguration)
 {
     PageTemplateService = new RazorEngine.Templating.TemplateService(pageConfiguration);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Sets the service in the template.
 /// </summary>
 /// <param name="template">The template to set the service on.</param>
 /// <param name="service">The template service managing the template.</param>
 private static void SetService(ITemplate template, TemplateService service)
 {
     template.Service = service;
 }