internal static BlockControllerBase CreateController(HtmlHelper helper, RenderingBlock block, IPublishedContent content, CultureInfo currentCulture, ViewDataDictionary viewData) { var c = CreateController(block, content, currentCulture); c.Helper = helper; c.ViewData = viewData ?? helper.ViewData; return(c); }
public void TestReplace() { var b = new RenderingBlock(); Int3 u = new Int3(1, 2, 3); b.Set(u, Matrix4x4.identity); b.Set(u, Matrix4x4.identity); b.Set(u, Matrix4x4.identity); Assert.AreEqual(1, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); }
public void TestRenderingBlockBasic() { var b = new RenderingBlock(); Int3 u = new Int3(1, 2, 3); Int3 v = new Int3(4, 5, 6); Int3 w = new Int3(0, 0, 0); Assert.AreEqual(0, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Set(u, Matrix4x4.identity); Assert.AreEqual(1, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Clear(v); Assert.AreEqual(1, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Clear(u); Assert.AreEqual(0, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Set(u, Matrix4x4.identity); Assert.AreEqual(1, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Set(v, Matrix4x4.identity); Assert.AreEqual(2, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Clear(w); Assert.AreEqual(2, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Clear(u); Assert.AreEqual(1, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Set(w, Matrix4x4.identity); Assert.AreEqual(2, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Clear(v); Assert.AreEqual(1, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Clear(u); Assert.AreEqual(1, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); b.Clear(w); Assert.AreEqual(0, b.GetNumOccupied()); Assert.IsTrue(b.CheckInvariants()); }
private static string GetCacheKey(RenderingBlock block, IPublishedContent content, HttpRequestBase request, ViewDataDictionary viewData) { var key = "Zbu.Blocks__" + block.Source; var cache = block.Cache; if (cache.ByPage) { key += "__p:" + content.Id; } if (!string.IsNullOrWhiteSpace(cache.ByConst)) { key += "__c:" + cache.ByConst; } if (cache.ByMember) { // sure it's obsolete but what's the new way of getting the 'current' member ID? // currently even v7 HtmlHelperRenderExtensions uses the legacy API var member = Member.GetCurrentMember(); key += "__m:" + (member == null ? 0 : member.Id); } if (cache.ByQueryString != null) { key = cache.ByQueryString.Aggregate(key, (current, vb) => current + "__" + request.QueryString[vb]); } if (cache.ByProperty != null) { key = cache.ByProperty.Aggregate(key, (current, alias) => { var recurse = alias.StartsWith("_"); var value = content.GetPropertyValue(recurse ? alias.Substring(1) : alias, recurse); return(current + "__v:" + value); }); } var custom = cache.GetCacheCustom(request, block, content, viewData); if (!string.IsNullOrWhiteSpace(custom)) { key += "__x:" + custom; } return(key.ToLowerInvariant()); }
private static MvcHtmlString Block(this HtmlHelper helper, RenderingBlock block, ViewDataDictionary viewData) { var rendered = Renderer.BlockWithCache(helper, block, viewData); var text = rendered.Item1; var meta = rendered.Item2; var model = helper.ViewData.Model as BlockModel; if (model == null) { throw new Exception("oops."); } if (BlocksController.Settings.MergeMeta != null && meta != null) { BlocksController.Settings.MergeMeta(model, meta); } return(new MvcHtmlString(text)); }
private static BlockControllerBase CreateController(RenderingBlock block, IPublishedContent content, CultureInfo currentCulture) { Func <BlockControllerBase> func; var controller = BlockControllers.TryGetValue(block.Source, out func) ? func() : new DefaultBlockController(); controller.Block = block; controller.SetContent(content); controller.CurrentCulture = currentCulture; // support unit tests if (RunContext.IsTesting && UmbracoContext.Current == null) #if UMBRACO_6 { // U6 is missing the parameter-less constructor // don't run unit tests on U6 but on U7... controller.Umbraco = null; } #else { controller.Umbraco = new UmbracoHelper(); }
public static string ViewTextWithCache(ControllerContext context, RenderingBlock block, IPublishedContent content, CultureInfo currentCulture, bool refresh) { var cache = RunContext.RuntimeCache; var key = GetCacheKey(block, content, context.HttpContext.Request, null); // in order to refresh we have to flush before getting if (refresh) { cache.ClearCacheByKeySearch(key); } // render cached var text = (string)cache.GetCacheItem( key, () => ViewText(context, block, content, currentCulture), new TimeSpan(0, 0, 0, block.Cache.Duration), // duration false, // sliding System.Web.Caching.CacheItemPriority.NotRemovable); return(text); }
public void Set(Int3 u, Matrix4x4 transform) { Int3 blockNum = Int3.Floor(u / Lf); RenderingBlock block = null; if (!blocksTable.TryGetValue(blockNum, out block)) { block = new RenderingBlock(); blocksTable.Add(blockNum, block); } Int3 blockCell = u - (blockNum * L); Debug.Assert(blockCell.x >= 0); Debug.Assert(blockCell.x < L); Debug.Assert(blockCell.y >= 0); Debug.Assert(blockCell.y < L); Debug.Assert(blockCell.z >= 0); Debug.Assert(blockCell.z < L); block.Set(blockCell, transform); }
// TODO possible optimization: keep a pool of rendering blocks, so if we // fully clear a block, we don't just toss it to garbage or leave the // memory wasted. public void Clear(Int3 u) { Int3 blockNum = Int3.Floor(u / Lf); RenderingBlock block = null; if (!blocksTable.TryGetValue(blockNum, out block)) { return; } Int3 blockCell = u - (blockNum * L); Debug.Assert(blockCell.x >= 0); Debug.Assert(blockCell.x < L); Debug.Assert(blockCell.y >= 0); Debug.Assert(blockCell.y < L); Debug.Assert(blockCell.z >= 0); Debug.Assert(blockCell.z < L); block.Clear(blockCell); // TODO if a block is totally cleared, free it (back into a pool!) }
public static MvcHtmlString Block(this HtmlHelper helper, RenderingBlock block, object o) { var viewData = o == null ? null : o.AsViewDataDictionary(); return(helper.Block(block, viewData)); }
public static MvcHtmlString Block(this HtmlHelper helper, RenderingBlock block) { return(helper.Block(block, null)); }
public BlockModel(IPublishedContent content, RenderingBlock block, CultureInfo culture) : base(content, culture) { Block = block; }
public BlockModel(IPublishedContent content, RenderingBlock block) : base(content) { Block = block; }
internal static BlockControllerBase CreateController(ControllerContext context, RenderingBlock block, IPublishedContent content, CultureInfo currentCulture) { var c = CreateController(block, content, currentCulture); c.ControllerContext = context; c.ViewData = new ViewDataDictionary(); return(c); }
// caching version public static Tuple <string, IDictionary <string, object> > BlockWithCache(HtmlHelper helper, RenderingBlock block, ViewDataDictionary viewData) { // nothing? if (block == null) { return(null); } // get the current block model // BlockModel<TContent> : BlockModel var currentBlockModel = helper.ViewData.Model as BlockModel; if (currentBlockModel == null) { throw new Exception("Model does not inherit from BlockModel."); } var cacheMode = block.Cache == null ? CacheMode.Ignore : block.Cache.GetCacheMode(block, currentBlockModel.Content, viewData); if (block.Cache == null || cacheMode == CacheMode.Ignore) // test null again so ReSharper is happy { return(Block(helper, block, viewData)); } var cache = RunContext.RuntimeCache; var request = RunContext.IsTesting ? null : helper.ViewContext.HttpContext.Request; // todo: Environment.GetRequest(helper) OR mock request var key = GetCacheKey(block, currentBlockModel.Content, request, viewData); // in order to refresh we have to flush before getting if (cacheMode == CacheMode.Refresh) { cache.ClearCacheByKeySearch(key); } var cached = (Tuple <string, IDictionary <String, object> >)cache.GetCacheItem( key, () => Block(helper, block, viewData), new TimeSpan(0, 0, 0, block.Cache.Duration), // duration false, // sliding System.Web.Caching.CacheItemPriority.NotRemovable); return(cached); }
// basic, non-caching version private static Tuple <string, IDictionary <string, object> > Block(HtmlHelper helper, RenderingBlock block, ViewDataDictionary viewData) { // nothing? if (block == null) { return(null); } // get the current block model // BlockModel<TContent> : BlockModel var currentBlockModel = helper.ViewData.Model as BlockModel; if (currentBlockModel == null) { throw new Exception("Model does not inherit from BlockModel."); } var controller = BlockControllerBase.CreateController(helper, block, currentBlockModel.Content, currentBlockModel.CurrentCulture, viewData); var text = controller.RenderInternal(); var meta = controller.Meta; // this is how we get it back... return(Tuple.Create(text, meta)); // we have: // UmbracoViewPage<TModel> : WebViewPage<TModel> // Model is TModel and we know how to map // IPublishedContent, RenderModel, RenderModel<TContent> // UmbracoViewPage : UmbracoViewPage<IPublishedContent> // Model is IPublishedContent // // and deprecating: // UmbracoTemplatePage<TContent> : UmbracoViewPage<RenderModel<TContent>> // Model is RenderModel<TContent> ie Content is TContent // UmbracoTemplatePage : UmbracoViewPage<RenderModel> // Model is RenderModel ie Content is IPublishedContent // UmbracoViewPage<TModel> can map from BlockModel or BlockModel<TContent> // because they inherit from RenderModel, and there is no way it can map // from anything to BlockModel so that's not an issue. // However it does not know how to map BlockModel to BlockModel<TContent> // and that is an issue. So it will fallback to using a TypeConverter, which // we implemented in BlockModelTypeConverter. }
public static string ViewText(ControllerContext context, RenderingBlock block, IPublishedContent content, CultureInfo currentCulture) { var controller = BlockControllerBase.CreateController(context, block, content, currentCulture); return(controller.RenderInternal()); }