Copied to clipboard

Flag this post as spam?

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


  • Tarunjit 103 posts 146 karma points
    Apr 14, 2016 @ 07:17
    Tarunjit
    0

    Pass Parameters to Umbraco Macro From Static Content Page

    Hi All,

    Can we pass parameters to Macro from static content pages? If yes please let me know the way to do it.

    As per my information we can place macro in the Rich Text Editor from one of the options available in the toolbar. I am not sure how to pass parameter to such an embedded macro.

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Apr 14, 2016 @ 11:03
    Dennis Adolfi
    0

    Hi Tarunjit.

    Yes, you can pass in the parameters as dictionary parameters.

    Here is a very basic example:

    The macro partial:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ var myCustomParameter = Model.MacroParameters["myCustomParameter"].ToString();   }
    
    <p>@(myCustomParameter)</p>
    

    The macro parameters:

    enter image description here

    Passing the parameters and render the macro in view:

    @Umbraco.RenderMacro("MyCustomMacro", new {myCustomParameter="My Value"})
    

    Here is a little more info regarding macros and managing parameters: https://our.umbraco.org/documentation/Reference/Templating/Macros/managing-macros (The images is Umbraco 6 but the principle is the same.)

    Best of luck to you, let me know if you need anything else.

    / Dennis

    Ps. You can also use the "Insert Macro" tool from the Template, see animation:

    enter image description here

  • Tarunjit 103 posts 146 karma points
    Apr 14, 2016 @ 12:51
    Tarunjit
    1

    Hi Dennis,

    Thanks for the answer.

    As per the solution provided by you here we are inserting macro in a template.

    But suppose we have a different scenario in which we have 15 content pages which are implementing the same template but out of these 15 content pages we need a macro and need to pass parameters to the macro in only 2 of these content pages.

    In that scenario there is no need to handle macro in the template but rather we should be handling macro in those 2 content pages. But we are not sure how to pass parameters to macros through the content pages.

    Please help me on this.

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Apr 14, 2016 @ 13:04
    Dennis Adolfi
    0

    In that scenario, there is a datatype called Macro container, that you´d probobly are looking to use.

    Create a new property for your content page documenttype, with the datatype "Macro Container" then add this to your Template. See animation:

    enter image description here

    This way, you only have to set this property for the 2 pages, and not the other 13 in your example.

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Apr 14, 2016 @ 14:23
    Dennis Adolfi
    0

    Would that work for you?

  • Tarunjit 103 posts 146 karma points
    Apr 15, 2016 @ 05:09
    Tarunjit
    0

    Hi Dennis,

    Thanks for an elaborative answer. We are now able to pass the parameter from the static content page.

    But suppose as per my previous example of using macro in only 2 of static pages we won’t include our macro in template but will rather add macro only in the static content page using Rich Text Box editor. In that scenario while adding macro it asks for the parameter value and when we write @Model.LastName in parameter value text box our macro doesn’t work, and if pass some hard coded text value our macro works.

    Just for your information we have model passed into our template as @inherits Umbraco.Web.Mvc.UmbracoViewPage

    Please help me on this.

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Apr 15, 2016 @ 06:25
    Dennis Adolfi
    0

    In that scenario, i would skip having lastname as a parameter, and access the current content page directly from within the macro like this:

    @CurrentPage.LastName
    
  • Tarunjit 103 posts 146 karma points
    Apr 15, 2016 @ 11:34
    Tarunjit
    0

    Hi Dennis,

    Thanks for the answer.

    I tried it as @CurrentPage.LastName but it gives a blank value. I further debugged it and even if write any name after @CurrentPage, e.g. @CurrentPage.ABCDEF it gives me blank message.

    Just for your information we have model passed into our template as @inherits Umbraco.Web.Mvc.UmbracoViewPage .

    Model is accessible on the temple as @Model.LastName but same code if i write i.e. @Model.LastName in macro it gives an error.

    Please help me on this as i am struck here.

Please Sign in or register to post replies

Write your reply to:

Draft