Copied to clipboard

Flag this post as spam?

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


  • Jonas Eriksson 930 posts 1825 karma points
    Feb 24, 2011 @ 06:55
    Jonas Eriksson
    2

    Get macro parameter value?

    What's the syntax for using macro parameters within Razor?

  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 24, 2011 @ 08:12
    Gareth Evans
    5

    @Parameter.nameOfParameter

    If that doesn't work, try it all in lowercase - I think there's a problem with the casing that I can't solve because it happens further up than Razor

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Feb 24, 2011 @ 08:12
  • Gareth Evans 142 posts 334 karma points c-trib
    Feb 24, 2011 @ 09:20
    Gareth Evans
    1

    Seb: that was my original implementation but it was replaced with Elijah's , so it's @Parameter.nameOfParameter now

     

    Gareth

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Feb 24, 2011 @ 11:02
    Sebastiaan Janssen
    0

    Oh, nice, that makes a lot of sense. We were posting that at the same time haha!

  • Eran Meir 401 posts 543 karma points
    Feb 24, 2011 @ 11:39
    Eran Meir
    0

    you should add this to the wiki

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Feb 24, 2011 @ 14:23
    Sebastiaan Janssen
    0

    Could do, or if you have a moment, the wiki is open to edit by anyone! :-)

  • Pinal Bhatt 298 posts 390 karma points
    Feb 25, 2011 @ 10:38
    Pinal Bhatt
    0

    Following works fine

    @{var param = @Parameter.nameOfParameter;}

    or

    @(Html.Raw(Parameter.nameOfParameter))
  • Yannick Smits 321 posts 718 karma points
    Mar 29, 2011 @ 18:29
    Yannick Smits
    0

    Can we also set to this special parameter object when not using a dedicated macro? Like using it from a template:

    <umbraco:Macro Language="cshtml" runat="server">
    @RenderPage("LanguageSwitcher.cshtml")
    </
    umbraco:Macro>

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Mar 29, 2011 @ 19:08
    Sebastiaan Janssen
    0

    Would you really need to?

    Just before renderpage you could just do

    var myParam = 1;

    And then you can use it in LanguageSwitcher by just refering to myParam, right?

    You can just use the normal C# ways of getting dictionary items, Request.QueryString, Model.AncestorOrSelf() stuff.. Or am I missing something?

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 30, 2011 @ 02:58
    Jonas Eriksson
    1

    myParam wouldn't be valid from LanguageSwitcher since it's another scope, if you like to send a variable from one template or scripting file to another you can for example use the Page object:

    <umbraco:macro language="razor" runat="server">
    @{
    Page.yo="Hello World";
    }
    @RenderPage("~/macroscripts/test.cshtml")
    </umbraco:macro>

    and in test.cshtml:

    @Page.yo

    As an alternative you can add a parameter to RenderPage.

    I have no idea if this answers any question here :)

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Mar 30, 2011 @ 11:15
    Sebastiaan Janssen
    0

    Awesome Jonas, thanks for the clarification!

  • Yannick Smits 321 posts 718 karma points
    Mar 30, 2011 @ 11:23
    Yannick Smits
    0

    Seb and Jonas, I was looking to it from a package builder kind of perspective. Now if somebody decides it wants to use macro paramaters in their razor script we are unable to use it in a RenderPage kind of syntax. Not a biggy but would have been nice.

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Mar 30, 2011 @ 18:21
    Sebastiaan Janssen
    0

    In the .cshtml you can make some sensible default values in case the page variables are not there (I suppose they will be null when not defined in the Masterpage). Then you just document the variables that can be set and have people add them at will. 

    Of course you can also install a template with the defaults set as well.

    One note (I'm sure you know this, but for others that stumble upon this topic): If you create a package, you could obviously just create a proper macro with params, When you select the macro for inclusion in the package creator, the params are also included. That requires a bit less documentation..

    I do like the @RenderPage method though, as it decouples the .cshtml file from the umbraco database.

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 28, 2012 @ 16:38
    Anthony Candaele
    0

    Hi,

    I'm trying to access a macro parameter info on a macro.

    On my homepage I have 2 macro's, both have a 'title' macro parameter.

     <homepageBox><![CDATA[<?UMBRACO_MACRO  macroalias="HomeBox"  title="this is a test"  />]]></homepageBox>

          <getInTouch><![CDATA[<?UMBRACO_MACRO  macroalias="GetInTouch"  title="Contact"  intro="Piet Bracke"  address="Korte Meer, 5, 9000 Ghent, Belgium"  telephone=" +32 9 264 68 03"  fax=""  email="[email protected]"  />]]></getInTouch>

    According on Razor Feature Walktrough Blog, I should be able to acces this macro parameter with the following syntax:

    var title = @Parameter.title

    So here is the thing, how can I access the 'title' macro parameter on the 'HomeBox' macro ?

    thanks for your help,

    Anthony

     

  • Gareth Evans 142 posts 334 karma points c-trib
    Sep 30, 2012 @ 21:40
    Gareth Evans
    0

    Hi Anthony

    If I understand your question correctly,

    Assuming both are razor macros, and you've placed them in a rich text editor field in the backoffice, 

    When the homebox macro is running, the title property for that one will be availabel in @Parameter.title

    When the GetInTouch macro is running, the title property will also be in @Parameter.title

     

    You won't be able to access them both at the same time

    Gareth

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 06, 2012 @ 17:56
    Anthony Candaele
    0

    Hi Gareth,

    I see. I'm not using the macro's in the Richt Text Editor. I'm just accessing them from a .cshtml file.

    The problem I have a the moment though is that values returned are empty.

    My code looks like this:

    @if (Model.HasValue("targetSolution"))

    {

        var title = Parameter.targetSolutionTitle;

        var summary = Parameter.targetSolutionSummary;

        var image = Parameter.targetSolutionImage;

        var link = Parameter.targetSolutionLink;

    }

    The value stored in Xml look like this:

    <targetSolution><![CDATA[<?UMBRACO_MACRO  macroalias="TargetSolution"  targetSolutionTitle="Solar Roofing"  targetSolutionSummary="The main parameters to look at when selecting a crystalline solar energy panel include the efficiency of the panel, it's sensitivity to heat and it's warranty"  targetSolutionImage="1127"  targetLink="1219"  />]]></targetSolution>

    I recall on another project I had the same issue, and then all of a sudden the value's returned wheren't empty anymore. Could this be a chaching issue or something like that?

    Thanks for your help,

    Anthony

  • Greg McMullen 49 posts 195 karma points
    Jan 18, 2013 @ 14:53
    Greg McMullen
    0

    I know this thread is older, but I'm having an issue getting an image value out of a Parameter. The vars being called in the template are selected via a Media Picker. We have a working XSLT file, but I'm new to umbraco and trying to rewrite in Razor. If you would like to see that code it's here: http://our.umbraco.org/forum/developers/razor/37621-Converting-Media-Picker-XSLT-to-Razor

    Razor Macro:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    
        @{
            @* Grab mediaId with @Parameter.paramaterName *@
            var image = @Parameter.mediaId;
            @image
        }

    Macro Call:

    <umbraco:Macro Alias="MediaImage" runat="server" mediaId="[#ad1Image]" class="cms"></umbraco:Macro>
    <umbraco:Macro Alias="MediaImage" runat="server" mediaId="[#ad2Image]" class="cms"></umbraco:Macro>
    <umbraco:Macro Alias="MediaImage" runat="server" mediaId="[#ad3Image]" class="cms"></umbraco:Macro> 

    Outputs:

    1645 / 1209 / 1210 - Which I assume are the media IDs for the respective images being picked.

  • Greg McMullen 49 posts 195 karma points
    Jan 18, 2013 @ 15:02
    Greg McMullen
    0

    Damiaan's post helped solve the problem.

    Updated code:

    @{ var media = new umbraco.cms.businesslogic.media.Media( Convert.ToInt32(@Parameter.mediaId) ); string imageUrl = media.getProperty("umbracoFile").Value.ToString(); <img src="@imageUrl" /> }
  • Lewis 36 posts 116 karma points
    Feb 05, 2013 @ 09:44
    Lewis
    0

    Does CurrentModel have support for macro parameters? Or is using Parameter and the dot notation syntax the only way? 

    I can't seem to find anything in CurrentModel 

  • Lars Ljungqvist 14 posts 34 karma points
    Sep 09, 2013 @ 15:11
    Lars Ljungqvist
    0

    Just don't forget to add the parameter outside the razor code as well. You need to add it on the second tab on the macro where the razor file is pointing to. That might have saved me 1 hour of work :-)

  • Ross Stewart 3 posts 23 karma points
    Jun 13, 2014 @ 12:07
    Ross Stewart
    0

    Hi guys

    I know this is an old thread, but was hoping someone could help.

    I'm new to Umbraco. I've created a macro with parameter name 'myTest'. In my scripting file, I'm trying to call it via:

    @Parameter.myTest

    However, nothing is printed on the page. Any ideas what I'm doing wrong?

    Cheers

    Ross

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jun 13, 2014 @ 22:16
    Dennis Aaen
    5

    Hi Ross and welcome to our,

    If you are using Umbraco 7, the way that you get the value of a parameter MVC like this:

    @Model.MacroParameters[“paramAlias”]

    The code you have is for the old legacy razor code called DynamicNode razor thefore if you don´t use Partial View Macro Files or Partial Views I would recommed you to do that, in there you can use dynamic razor or strongly typed razor.

    Just to be clear if you´re using version 7 you should be able to get the value of the paramter like this.

    @Model.MacroParameters[“paramAlias”]

    Here are some good razor cheatsheets, for both the dynamic razor and the strongly typed. I know it´s say for version 6, but you can also use this for version 7.

    http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    The dynamic razor cheatsheet is the one called v6Razorcheatsheet.pdf and the strongly typed cheatsheet is the one caled v6strongcheatsheet.pdf

    Hope this helps,

    /Dennis

  • Ross Stewart 3 posts 23 karma points
    Jun 16, 2014 @ 11:17
    Ross Stewart
    0

    Thanks Dennis, you're a great help!

Please Sign in or register to post replies

Write your reply to:

Draft