Copied to clipboard

Flag this post as spam?

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

  • dan 54 posts 133 karma points
    Oct 05, 2015 @ 18:22

    Passing a variable to a PartialViewMacroPage

    I have a macro tied to a PartialViewMacroPage and I have parameters set at the macro level that I'm trying to get the value from in the PartialViewMacroPage.

    enter image description here

    When I add the macro using the rich editor, I'm setting parameters.

    I can get as far as identifying if it's there but it trying to access the value and it comes up empty.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
         var galParam = Model.MacroParameters["galleryWidth"];
    <script type="text/javascript">
               var test = "@galParam"  //empty

    If I check the html in the rich editor I can see the parameter values present and the values I set.

    <div class="umb-macro-holder mceNonEditable"><!-- <?UMBRACO_MACRO macroAlias="PhotoGallery" delay="3500" galleryWidth="600" /> --> <ins>
    <div class="mod_gallery_container">
    <div id="mod_gallery"></div>
    <div id="galleryTitleBar"></div>

    What else do I need to check or add to make this work?

  • Tom Engan 430 posts 1173 karma points
    Oct 07, 2015 @ 08:11
    Tom Engan

    I only have some experience with macros, so I thought I could give you my suggestion. I have integrated a macro for image carousel in Umbraco (Bootstrap), where I have a parameter with name 'SliderData' that I refer to in this way:

     dynamic sliderData = (! String.IsNullOrEmpty (Model.MacroParameters ["SliderData"]. ToString ()))?
             Json.Decode ((dynamic) HttpUtility.HtmlDecode (Model.MacroParameters ["SliderData"]. ToString ())): null;

    And in the same page, I get my data as follows:

    @foreach (var slide in sliderData)

    But the type isn't a Textbox, it's a integrated data type with name 'simple slider editor' (from a Property editor named SimpleSliderEditor), and this applies of course to more values in a matrix than a single value, but maybe it can put you on an idea of how to solve your task?

  • Tom Engan 430 posts 1173 karma points
    Oct 07, 2015 @ 09:46
    Tom Engan

    However, in another example of a macro I have with values from two Textboxes, are using XSLT (which I did not like, but it works), and I get the parameters' location' and' height' with this code:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets seoChecker.MetaData PS.XSLTsearch ">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:variable name="location" select="/macro/location"/>
    <xsl:variable name="height" select="/macro/height"/>
    <xsl:template match="/">
      <iframe src="{$location}" height="{$height}" style="border:0; width: 100%">
        Beklager, din nettleser st√łtter ikke IFRAMES, og kartet kan ikke vises.
  • dan 54 posts 133 karma points
    Oct 07, 2015 @ 14:54

    Thanks Tom. None of that code will work so long as

    Model.MacroParameters ["whatever"] returns empty. I feel like there is an extra step somewhere that isn't obvious.

    This should also be documented somewhere too. How to tie a parameter on a macro to a partialpageview not just for xslt but for razor too.

  • Tom Engan 430 posts 1173 karma points
    Oct 07, 2015 @ 15:02
    Tom Engan

    I'm a little unsure what you mean. I have used XSLT method for inserting an iframe as a macro in the grid, where I can insert GoogleMyMaps. The values do I then enter together with the content, then I save content in the grid, and the map with the entered values (in my xlst-codes the two parameters 'location' and 'height') display properly on the page.

    Original Source

  • dan 54 posts 133 karma points
    Oct 08, 2015 @ 14:39

    I'm not debating your code. I believe the xslt works though I haven't tried it yet.

    I'm just not crazy about switching to XSLT. I feel that razor is easier to look at which is why I'm bummed I can't find a working example or documentation that makes sense how to target the parameter in a PartialViewMacroPage in Razor.

    Between razor and XSLT, its super simple to just have Model.MacroParameters ["whatever"].

    Why one works but not the other doesn't make sense despite using the same snippet.

  • Ahmad Shabeeb 2 posts 22 karma points
    Jan 21, 2020 @ 10:50
    Ahmad Shabeeb

    For anyone who has a similar problem. I could find the problem.

    All params of aspecific Macro should be declared in the macro admin.

    I am using Umbraco 8.1 and to do this you: 1. Go to Settings 2. Click on Macros 3. Click on the Macro you want to add the params to 4. on the upright corner there is a burger menu with the label "Parameters"

    You are welcome.

    It is so annoying that the docs are so short and not clear most of the time. One of the downsides of Umbraco I guess.

Please Sign in or register to post replies

Write your reply to: