Beispiel #1
0
        /// <summary>
        /// Render widget
        /// Return render result
        /// </summary>
        /// <param name="context">Template context</param>
        /// <param name="widget">Template widget</param>
        /// <returns></returns>
        public virtual string RenderWidget(Context context, TemplateWidget widget)
        {
            // Get from cache
            var info = GetWidgetInfo(widget.Path);
            IKeyValueCache <string, string> renderCache = null;
            string key          = widget.GetCacheKey();
            string renderResult = null;

            if (info.CacheTime > 0)
            {
                renderCache = WidgetRenderCache.GetOrAdd(info.CacheBy ?? "", _ => {
                    var cacheFactory = Application.Ioc.Resolve <ICacheFactory>();
                    var policyNames  = info.GetCacheIsolationPolicyNames();
                    return(cacheFactory.CreateCache <string, string>(
                               CacheFactoryOptions.Default.WithIsolationPolicies(policyNames)));
                });
                renderResult = renderCache.GetOrDefault(key);
                if (renderResult != null)
                {
                    return(renderResult);
                }
            }
            // Render widget
            var renderer = Application.Ioc.Resolve <ITemplateWidgetRenderer>();

            renderResult = renderer.Render(context, widget);
            // Store to cache
            if (info.CacheTime > 0)
            {
                renderCache.Put(key, renderResult, TimeSpan.FromSeconds(info.CacheTime));
            }
            return(renderResult);
        }
Beispiel #2
0
        /// <summary>
        /// Render widget
        /// Return reder result
        /// </summary>
        /// <param name="context">Template context</param>
        /// <param name="widget">Template widget</param>
        /// <returns></returns>
        public virtual string RenderWidget(Context context, TemplateWidget widget)
        {
            // Get from cache
            var info = GetWidgetInfo(widget.Path);
            IKeyValueCache <string, string> renderCache = null;
            string key          = null;
            string renderResult = null;

            if (info.CacheTime > 0)
            {
                renderCache = WidgetRenderCache.GetOrAdd(info.CacheBy ?? "", _ => {
                    var cacheFactory = Application.Ioc.Resolve <ICacheFactory>();
                    var policyNames  = info.GetCacheIsolationPolicyNames();
                    return(cacheFactory.CreateCache <string, string>(
                               CacheFactoryOptions.Default.WithIsolationPolicies(policyNames)));
                });
                key          = widget.GetCacheKey();
                renderResult = renderCache.GetOrDefault(key);
                if (renderResult != null)
                {
                    return(renderResult);
                }
            }
            // Render widget
            var writer = new StringWriter();

            writer.Write($"<div class='template_widget' data-widget='{key}'>");
            var scope = Hash.FromAnonymousObject(widget.Args);

            context.Stack(scope, () => {
                var includeTag = new Include();
                var htmlPath   = widget.Path + TemplateWidgetInfo.HtmlExtension;
                includeTag.Initialize("include", htmlPath, null);
                includeTag.Render(context, writer);
            });
            writer.Write("</div>");
            renderResult = writer.ToString();
            // Store to cache
            if (info.CacheTime > 0)
            {
                renderCache.Put(key, renderResult, TimeSpan.FromSeconds(info.CacheTime));
            }
            return(renderResult);
        }