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
    dan
    0

    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
    
        </script>
    

    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>
    </div>
    </ins></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
    0

    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
    0

    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;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library"
      xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" 
      xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" 
      xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" 
      xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" 
      xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" 
      xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" 
      xmlns:seoChecker.MetaData="urn:seoChecker.MetaData" 
      xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
      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.
      </iframe>
    </xsl:template>
    </xsl:stylesheet>
    
  • dan 54 posts 133 karma points
    Oct 07, 2015 @ 14:54
    dan
    0

    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
    0

    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
    dan
    0

    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
    0

    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:

Draft