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:
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.
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.
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?
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
}
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 :-)
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.
Get macro parameter value?
What's the syntax for using macro parameters within Razor?
@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
Should be this: Added support for Macro Parameters in Razor Macros (@Model.Parameters.AnimalName) where AnimalName is defined on the Macro as a Parameter
Seb: that was my original implementation but it was replaced with Elijah's , so it's @Parameter.nameOfParameter now
Gareth
Oh, nice, that makes a lot of sense. We were posting that at the same time haha!
you should add this to the wiki
Could do, or if you have a moment, the wiki is open to edit by anyone! :-)
Following works fine
or
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>
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?
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 :)
Awesome Jonas, thanks for the clarification!
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.
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.
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
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
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
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:
Macro Call:
Outputs:
1645 / 1209 / 1210 - Which I assume are the media IDs for the respective images being picked.
Damiaan's post helped solve the problem.
Updated code:
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
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 :-)
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
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:
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.
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
Thanks Dennis, you're a great help!
is working on a reply...