Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John 88 posts 112 karma points
    Aug 12, 2011 @ 02:59
    John
    0

    Razor and Macro's

    Hi, it appears as they I cannot use a Macro from within a Razor macro? The result I get when trying to do something like this:

    <umbraco:Macro language="cshtml" runat="server">

    @if (Model.IsCurrentOwner()) {

    <div>Some text</div>

    <umbraco:Macro Prop1="X" alias="MyMacro" runat="server" />

    }

    </umbraco:Macro>

    Is the error: The 'Text' property of 'umbraco:Macro' does not allow child objects

     

    Is there any way I can achieve the above?

    Cheers

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Aug 12, 2011 @ 09:29
    Dirk De Grave
    0

    John,

    you could use umbraco.library.RenderMacroContent() if that macro references a xslt file. If that macro references a user control, it won't work. Alternatively, could move the logic for IsCurrentOwner() into the macro

     

    Hope this helps.

    Regards,

    /Dirk

  • John 88 posts 112 karma points
    Aug 22, 2011 @ 01:33
    John
    0

    Yeah I wanted to leave the logic for IsCurrentOwner seperate, though, as it's more extensible this way - it means I can easily make any body of content only visible to the owner rather than changing / creating a lot more controls.

    What I've ended up doing is creating a RenderControl method using the Server.Execute method mentioned here: http://www.jonkragh.com/index.php/rendering-an-asp-net-usercontrol-to-a-string/comment-page-1/ meaning I can do this:

     <umbraco:Macro language="cshtml" runat="server">
        @using X;
        @if ((Model as DynamicNode).IsContentOwner()) {
          <div>
            <div>
              <div>
                @{
                  var control =
                    UIHelper.RenderControl<Macro>(
                      uc =>
                        {
                          uc.Attributes.Add("Alias", "macroAlias");
                          uc.Attributes.Add("ControlProperty1", "1");
                          uc.Attributes.Add("ControlProperty2", "X");
                        }
                    );
                }
                @Html.Raw(control)
              </div>
            </div>
          </div>
        }
      </umbraco:Macro>
  • Troels Larsen 75 posts 280 karma points
    Nov 08, 2011 @ 14:48
    Troels Larsen
    0

    Could u post u UIHelper.RenderControl method ? 

  • John 88 posts 112 karma points
    Nov 08, 2011 @ 21:31
    John
    0

    Sure thing, there's actually 3 methods:

            /// <summary>
    /// Renders the user control.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="controlPath">The control path.</param>
    /// <param name="initControlAction">The init control action.</param>
    /// <param name="context">The context.</param>
    /// <returns>
    /// The rendered control output
    /// </returns>
    public static string RenderControl<T>(string controlPath, Action<T> initControlAction, HttpContextWrapper context = null)
    where T : UserControl
    {
    var pageHolder = new Page();
    var control = (T)pageHolder.LoadControl(controlPath);

    pageHolder.Controls.Add(control);

    return RenderControl(pageHolder, control, initControlAction, context);
    }

    /// <summary>
    /// Renders the control.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="initControlAction">The init control action.</param>
    /// <param name="context">The context.</param>
    /// <returns>
    /// The rendered control output
    /// </returns>
    public static string RenderControl<T>(Action<T> initControlAction, HttpContextWrapper context = null)
    where T : Control
    {
    var pageHolder = new Page();
    var control = pageHolder.LoadControl(typeof(T), null);

    pageHolder.Controls.Add(control);

    return RenderControl(pageHolder, control, initControlAction, context);
    }

    /// <summary>
    /// Renders the control.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="pageHolder">The page holder.</param>
    /// <param name="control">The control.</param>
    /// <param name="initControlAction">The init control action.</param>
    /// <param name="context">The context.</param>
    /// <returns>
    /// The rendered control output
    /// </returns>
    private static string RenderControl<T>(Page pageHolder, Control control, Action<T> initControlAction, HttpContextWrapper context)
    where T : Control
    {
    var result = new StringWriter();

    // init any properties / actions the user has specified
    initControlAction(control as T);

    context = ContentHelper.GetHttpContext(context);

    // disable tracing if not requested
    pageHolder.Trace.IsEnabled = (!String.IsNullOrEmpty(context.Request["umbDebugShowTrace"]) && GlobalSettings.DebugMode);

    // render the control to the string writer
    context.Server.Execute(pageHolder, result, true);

    return result.ToString();
    }
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies