Example #1
0
        public string RenderTemplate(string templatePath, Type modelType, object model)
        {
            var key            = new FullPathTemplateKey(null, templatePath, ResolveType.Layout, null);
            var templateSource = _templateManager.Resolve(key);
            var result         = Engine.Razor.RunCompile(templateSource, key, modelType, model);

            return(result);
        }
        public ITemplateSource Resolve(ITemplateKey key)
        {
            ITemplateSource result;

            if (_dynamicTemplates.TryGetValue(key, out result))
            {
                return(result);
            }

            var ctx = new TemplateSourceResolveContext()
            {
                Key      = key,
                Source   = string.Empty,
                FileName = key.Name
            };

            if (!string.IsNullOrWhiteSpace(key.Name))
            {
                // I need to read resolvers from WorkContext here, because if I injected them in the class constructor they wouldn't work properly.
                // They cache their content throughout multiple requests and fail to update content templates when needed.
                // This happens because IRazorTemplateManager inherits from ISingletonDependency.
                var resolvers = _orchardServices.WorkContext
                                .Resolve <IEnumerable <ICustomRazorTemplateResolver> >()
                                .OrderByDescending(r => r.Priority);
                foreach (var res in resolvers)
                {
                    if (!string.IsNullOrWhiteSpace(ctx.Source))
                    {
                        break;
                    }
                    res.ResolveTemplateSource(ctx);
                }
            }

            if (string.IsNullOrWhiteSpace(ctx.Source))
            {
                return(_defaultTemplateManager.Resolve(key));
            }
            else
            {
                return(new LoadedTemplateSource(ctx.Source, ctx.FileName));
            }
        }
        private string CompileTemplate(string templateKey, Type modelType, object args)
        {
            var templateSource = _templateManager.Resolve(new NameOnlyTemplateKey(templateKey, ResolveType.Global, null));

            return(Engine.Razor.RunCompile(templateSource, templateKey, modelType, args));
        }