Exemple #1
0
        /// <summary>
        /// Updates the cache.
        /// </summary>
        /// <param name="viewPath">The view path.</param>
        /// <param name="viewLoader">The view loader.</param>
        /// <returns>System.String.</returns>
        /// <exception cref="System.NullReferenceException">Could not find file {0}..Formatted(viewPath)</exception>
        private static void UpdateCache(string viewPath, Func <string, string> viewLoader)
        {
            viewPath = viewPath.ToLower();

            string finalview = viewLoader(viewPath);


            lock (_viewKey)
            {
                if (finalview.IsNullOrWhiteSpace())
                {
                    Sitecore.Diagnostics.Log.Warn("Could not update cached view because view content was null or empty {0}".Formatted(viewPath), "");
                    _viewCache.Remove(viewPath);
                }
                else
                {
                    try
                    {
                        var cached = new CachedView();

                        cached.ViewContent = finalview;

                        var template = RazorEngine.Razor.CreateTemplate(cached.ViewContent);
                        cached.Type          = template.GetType().BaseType.GetGenericArguments()[0];
                        cached.Name          = viewPath;
                        _viewCache[viewPath] = cached;
                    }
                    catch (Exception ex)
                    {
                        Sitecore.Diagnostics.Log.Error("Failed to update Razor cache.", ex, "");
                        _viewCache.Remove(viewPath);
                    }
                }
            }
        }
        /// <summary>
        /// Updates the cache.
        /// </summary>
        /// <param name="viewPath">The view path.</param>
        /// <param name="viewLoader">The view loader.</param>
        /// <returns>System.String.</returns>
        /// <exception cref="System.NullReferenceException">Could not find file {0}..Formatted(viewPath)</exception>
        private static void UpdateCache(string viewPath, Func<string, string> viewLoader)
        {
            viewPath = viewPath.ToLower();

            string finalview = viewLoader(viewPath);


            lock (_viewKey)
            {
                if (finalview.IsNullOrWhiteSpace())
                {
                    Sitecore.Diagnostics.Log.Warn("Could not update cached view because view content was null or empty {0}".Formatted(viewPath), "");
                    _viewCache.Remove(viewPath);
                }
                else{
                    try
                    {
                        var cached = new CachedView();

                        cached.ViewContent = finalview;

                        List<string> placeholders = new List<string>();

                        var matches = _placeholders.Matches(cached.ViewContent);
                        foreach (Match match in matches)
                        {
                            placeholders.Add(match.Groups[1].Value);    
                        }

                        cached.Placeholders = placeholders;
                        var template = RazorEngine.Razor.CreateTemplate(cached.ViewContent);
                        
                        cached.Type = template.GetType().BaseType.IsGenericType
                            ? template.GetType().BaseType.GetGenericArguments()[0]
                            : template.GetType().BaseType;
                        cached.Name = viewPath;
                        _viewCache[viewPath] = cached;
                    }
                    catch (Exception ex)
                    {
                        Sitecore.Diagnostics.Log.Error("Failed to update Razor cache.", ex, "");
                        _viewCache.Remove(viewPath);
                    }
                }
            }
        }