예제 #1
0
        /// <summary>
        /// Configures the templating engine.
        /// </summary>
        private static void Configure()
        {
            var config = RazorEngineConfigurationSection.GetConfiguration();

            if (config != null)
            {
                if (!string.IsNullOrWhiteSpace(config.Factory))
                {
                    SetCompilerServiceFactory(config.Factory);
                }
                else
                {
                    CompilerServiceFactory = new DefaultCompilerServiceFactory();
                }

                if (config.TemplateServices.Count > 0)
                {
                    string @default = string.IsNullOrWhiteSpace(config.TemplateServices.Default)
                                          ? null
                                          : config.TemplateServices.Default;

                    foreach (TemplateServiceConfigurationElement serviceConfig in config.TemplateServices)
                    {
                        string name    = serviceConfig.Name;
                        var    service = ConfigurationServices.CreateTemplateService(serviceConfig);;
                        ConfigurationServices.AddNamespaces(service, config.Namespaces);

                        if (name == @default)
                        {
                            DefaultTemplateService = service;
                        }

                        Services.Add(name, service);
                    }
                }

                if (DefaultTemplateService == null)
                {
                    DefaultTemplateService = new TemplateService(CompilerServiceFactory.CreateCompilerService());
                    ConfigurationServices.AddNamespaces(DefaultTemplateService, config.Namespaces);
                }

                if (!string.IsNullOrWhiteSpace(config.Activator))
                {
                    DefaultTemplateService.SetActivator(ConfigurationServices.CreateInstance <IActivator>(config.Activator));
                }
            }
            else
            {
                ConfigureDefault();
            }
        }
예제 #2
0
 /// <summary>
 /// Sets the activator delegate used to create types.
 /// </summary>
 /// <param name="activator">The activator delegate to use.</param>
 public static void SetActivator(Func <Type, ITemplate> activator)
 {
     DefaultTemplateService.SetActivator(activator);
 }
예제 #3
0
 /// <summary>
 /// Sets the activator used to create types.
 /// </summary>
 /// <param name="activator">The activator to use.</param>
 public static void SetActivator(IActivator activator)
 {
     DefaultTemplateService.SetActivator(activator);
 }