Exemplo n.º 1
0
        /// <summary>
        /// Render all queued output blocks injected via <see cref="Delayed"/>.
        /// <para>
        /// <example>
        /// Print all delayed blocks using default identifier (i.e. not provided)
        /// <code>
        /// @using (Html.Delayed()) {
        ///     <b>show me later</b>
        ///     <span>@Model.Name</span>
        ///     etc
        /// }
        /// </code>
        /// -- then later --
        /// <code>
        /// @using (Html.Delayed()) {
        ///     <b>more for later</b>
        ///     etc
        /// }
        /// </code>
        /// -- then later --
        /// <code>
        /// @Html.RenderDelayed() // will print both delayed blocks
        /// </code>
        /// </example>
        /// </para>
        /// <para>
        /// <example>
        /// Allow multiple repetitions of rendered blocks, using same <code>@Html.Delayed()...</code> as before.  Code:
        /// <code>
        /// @Html.RenderDelayed(removeAfterRendering: false); /* will print */
        /// @Html.RenderDelayed() /* will print again because not removed before */
        /// </code>
        /// </example>
        /// </para>
        /// </summary>
        /// <param name="helper">the helper from which we use the context</param>
        /// <param name="injectionBlockId">optional unique identifier to specify one or many injection blocks</param>
        /// <param name="removeAfterRendering">only render this once</param>
        /// <returns>rendered output content</returns>
        public static MvcHtmlString RenderDelayed(this HtmlHelper helper, string injectionBlockId = null, bool removeAfterRendering = true)
        {
            var stack = DelayedInjectionBlock.GetQueue(helper, injectionBlockId);

            if (removeAfterRendering)
            {
                var sb = new StringBuilder(
#if DEBUG
                    string.Format("<!-- delayed-block: {0} -->", injectionBlockId)
#endif
                    );
                // .count faster than .any
                while (stack.Count > 0)
                {
                    sb.AppendLine(stack.Dequeue());
                }
                return(MvcHtmlString.Create(sb.ToString()));
            }

            return(MvcHtmlString.Create(
#if DEBUG
                       string.Format("<!-- delayed-block: {0} -->", injectionBlockId) +
#endif
                       string.Join(Environment.NewLine, stack)));
        }
Exemplo n.º 2
0
        public static MvcHtmlString RenderDelayed(this HtmlHelper helper, string injectionBlockId = null, bool removeAfterRendering = true)
        {
            Queue <string> queue = DelayedInjectionBlock.GetQueue(helper, injectionBlockId);

            if (!removeAfterRendering)
            {
                return(MvcHtmlString.Create(string.Join(Environment.NewLine, queue)));
            }

            StringBuilder stringBuilder = new StringBuilder();

            while (queue.Count > 0)
            {
                stringBuilder.AppendLine(queue.Dequeue());
            }

            return(MvcHtmlString.Create(stringBuilder.ToString()));
        }