Beispiel #1
0
        protected string Render(string source, BlockModel blockModel)
        {
            string           text;
            ControllerBase   controller;
            BlocksController blocksController;

            if (Helper != null)
            {
                controller       = Helper.ViewContext.Controller;
                blocksController = controller as BlocksController;
                text             = ViewData == null
                    ? Helper.Partial(source, blockModel).ToString()            // will use the "current" viewData
                    : Helper.Partial(source, blockModel, ViewData).ToString(); // uses our own local viewData
            }
            else if (ControllerContext != null)
            {
                controller       = ControllerContext.Controller;
                blocksController = controller as BlocksController;
                if (!RunContext.IsTesting && blocksController == null)
                {
                    throw new Exception("Oops.");
                }

                // this basically repeats what Controller.View is doing

                var viewEngineResult = ViewEngines.Engines.FindView(ControllerContext, source, null);
                if (viewEngineResult == null)
                {
                    throw new Exception("Null ViewEngineResult.");
                }
                var view = viewEngineResult.View;
                if (view == null)
                {
                    // we need to generate an exception containing all the locations we searched
                    // copied over from ViewResult source code
                    var locationsText = new StringBuilder();
                    foreach (var location in viewEngineResult.SearchedLocations)
                    {
                        locationsText.AppendLine();
                        locationsText.Append(location);
                    }
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      "The view '{0}' or its master was not found or no view engine supports the searched locations. The following locations were searched:{1}", // MvcResources.Common_ViewNotFound
                                                                      source, locationsText));
                }

                controller.ViewData.Model = blockModel;

                using (var sw = new StringWriter())
                {
                    var ctx = new ViewContext(ControllerContext, view,
                                              controller.ViewData,
                                              controller.TempData,
                                              sw);
                    view.Render(ctx, sw);
                    text = sw.ToString();
                }
            }
            else
            {
                throw new Exception("Oops.");
            }

            // propagate meta upwards
            if (blockModel.HasMeta)
            {
                Meta = blockModel.Meta;
            }

            var traceBlocksInHtml = blocksController != null && blocksController.TraceBlocksInHtml;

            return(!traceBlocksInHtml
                ? text
                : string.Format("<!-- block:{0} -->{1}{2}{1}<!-- /block:{0} -->{1}",
                                source, Environment.NewLine, text));
        }
Beispiel #2
0
        /*
         * public BlockModel(TContent content, RenderingBlock block)
         *  : base(content, block)
         * {
         *  Content = content;
         * }
         *
         * public BlockModel(TContent content, RenderingBlock block, CultureInfo culture)
         *  : base(content, block, culture)
         * {
         *  Content = content;
         * }
         */

        public BlockModel(TContent content, BlockModel model)
            : base(model.Content, model.Block, model.CurrentCulture)
        {
            _model  = model;
            Content = content;
        }
Beispiel #3
0
 protected string Render(BlockModel blockModel)
 {
     return(Render(Block.Source, blockModel));
 }