Copied to clipboard

Flag this post as spam?

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


  • Oscar Svedberg 19 posts 82 karma points
    Jan 27, 2015 @ 23:49
    Oscar Svedberg
    0

    Programmatically pass parameter to Macro when rendered in a Grid layout

    Hi!

    I'm developing a site where I make a lot of use of the Grid layout. I've created a number of Macros that I pass certain parameters to. This all works great, and I really love working with the Grid!

    Now to my problem; I want to pass the width of the column hosting the Macro to the Macro itself. I tried doing this programmatically, but it seems you can't pass parameters to a Macro without having to create them in the database, is that right?

    I've tried this:

    @inherits UmbracoViewPage<dynamic>
    @using Umbraco.Web.Templates
    
    
    @if (Model.value != null)
    {
        var columnSize = ViewData.Eval("ColWidth").ToString();  
        string macroAlias = Model.value.macroAlias.ToString();
        ViewDataDictionary parameters = new ViewDataDictionary();
    
    foreach (dynamic mpd in Model.value.macroParamsDictionary)
    {
        parameters.Add(mpd.Name, mpd.Value);
    }
    
    parameters.Add("columnSize", columnSize); // Add my own, custom parameter?
    
    <text>
        @Umbraco.RenderMacro(macroAlias, parameters)
    </text>
    }
    

    But that doesn't seem to work. The parameters collection does have two objects going in to the RenderMacro, but coming out the other side it only has the ones being created in the loop.

    Is there a way to pass "hidden" parameters to a Macro without the editor having to see them?

    Thank you in advance!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 27, 2015 @ 23:59
    Dennis Aaen
    0

    Hi Oscar,

    The way the macro normally looks like when it has a macro parameter added to it is like this.

    @Umbraco.RenderMacro("macroAlias", new {parameterAlias="parameterValue"})

    And if you have more than one parameter it will look like this:

    @Umbraco.RenderMacro("macroAlias", new {parameterAlias1="parameterValue1", parameterAlias2="parameterValue2"})

    Perhaps this can help you further.

    /Dennis

  • Oscar Svedberg 19 posts 82 karma points
    Jan 28, 2015 @ 00:04
    Oscar Svedberg
    0

    Thanks for your reply!

    But the parameter still needs to be created in the database, right? That's what I'm looking to circumvent.

Please Sign in or register to post replies

Write your reply to:

Draft