Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi
I have made a macro with Parameter "propNameRazor"
How can I insert "propNameRazor" instead of "link3"?
@{string target = Model.link3.NewWindow ? " target=\"_blank\"" : "";<a href="@Model.link3.Url" title="@Model.link3.Title" target="@target">@Model.link3.Title</a>}
Thanks in advance
Hi Kate,
"link3" must be the alias of your property in your document type, so you would need to update the property in the document type?
Is this property using a uComponents URLPicker property editor?
Jeavon
Hi Jeavon
"link3" is the alias of my property in my document type :-)
And yes, im using the uComponents URLPicker :-)
Ok, so if you want your code to be:
@{ string target = Model.propNameRazor.NewWindow ? " target=\"_blank\"" : ""; <a href="@Model.propNameRazor.Url" title="@Model.propNameRazor.Title" target="@target"> @Model.propNameRazor.Title </a> }
Just change the alias of the property in your document type to "propNameRazor"
You may also need to @Html.Raw your linkTarget.
This is the code sample we use for uComponents Razor Macro (without Model Binding)
@{ if (Model.propNameRazor != null && !String.IsNullOrEmpty(Model.propNameRazor.Url)) { var linkTarget = (Model.propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty; <a href="@Model.propNameRazor.Url" class="someCssClass" @Html.Raw(linkTarget)>@Model.propNameRazor.Title</a> } }
It still won't work.
When I write "propNameRazor" insted of "link3" it does not do anything. So do I have to declare the macro parameter in the script file?
Something like this:
var propNameRazor = Parameter.propNameRazor;
var
propNameRazor = Parameter.
propNameRazor;
in my template i have this:
<umbraco:Macro propNameRazor="link3" Alias="RazorPicker" runat="server"></umbraco:Macro>
In my script file, when I write this:
var propNameRazor = Parameter.propNameRazor; <p>@propNameRazor</p>
it spits out "link3"
So I know that there is a value from the parameter. I just dont know how to conect my variable "propNameRazor" with this line:
string target =Model.link3.NewWindow?" target=\"_blank\"":"";
Ah I see, try this:
@{ if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor)) { dynamic propNameRazor = Model.GetPropertyValue(Parameter.propNameRazor); if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.Url)) { var linkTarget = (propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty; <a href="@propNameRazor.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.Title</a> } } }
it gave my an error
If I just write this
dynamic propNameRazor = Model.GetPropertyValue(Parameter.propNameRazor); <p>@propNameRazor </p>
This is what comes out on the page:
<url-picker mode="Content"><new-window>False</new-window><node-id>1094</node-id><url>/uk/engelske-underside.aspx</url><link-title>Det er den jeg skal bruge</link-title></url-picker>
It seems that it does not like this if-sentenst
if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.Url)) { var linkTarget = (propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty; <a href="@propNameRazor.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.Title</a> }
Ah yes, you will need:
dynamic propNameRazor = new DynamicXml(Model.GetPropertyValue(Parameter.propNameRazor));
and add @using umbraco.MacroEngines at the top
@using umbraco.MacroEngines
Like this:
@inherits umbraco.MacroEngines.DynamicNodeContext@using umbraco.MacroEngines @{ if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor)) { dynamic propNameRazor = new DynamicXml(Model.GetPropertyValue(Parameter.propNameRazor)); if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.Url)) { var linkTarget = (propNameRazor.NewWindow) ? " target=\"_blank\"" : string.Empty; <a href="@propNameRazor.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.Title</a> } }}
Sorry, it didn't work.
can it be something with my Parameter. It's Type is set to "Text". Should that be something else?
Wow, it's more complicated that I remember, this seems to be working:
@using umbraco.MacroEngines @inherits DynamicNodeContext @{ if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor)) { dynamic propNameRazor = new DynamicXml(Model.GetPropertyValue(Parameter.propNameRazor)); if (propNameRazor != null && !String.IsNullOrEmpty(propNameRazor.url)) { var linkTarget = (propNameRazor.BaseElement.Element("new-window").Value == "True") ? " target=\"_blank\"" : string.Empty; <a href="@propNameRazor.url" class="link-1 button-link" @Html.Raw(linkTarget)>@propNameRazor.BaseElement.Element("link-title").Value</a> } } }
Jubii, it works
You're my favorite person of the week :-)
Thanks for your time and patience
/Kate
You're welcome and have a great weekend.
Depending on the version of uComponents you are using, you maybe able to simplify this a lot to:
@using uComponents.DataTypes.UrlPicker.Dto @using umbraco.MacroEngines @inherits DynamicNodeContext @{ if (!string.IsNullOrWhiteSpace(Parameter.propNameRazor)) { var urlPicker = UrlPickerState.Deserialize(Model.GetPropertyValue(Parameter.propNameRazor)); if (urlPicker != null && !String.IsNullOrEmpty(urlPicker.Url)) { var linkTarget = (urlPicker.NewWindow) ? " target=\"_blank\"" : string.Empty; <a href="@urlPicker.Url" class="link-1 button-link" @Html.Raw(linkTarget)>@urlPicker.Title</a> } } }
That works great too and it's easier to read :-)
Have a nice weekend
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Passing a Parameter Variable in razor
Hi
I have made a macro with Parameter "propNameRazor"
How can I insert "propNameRazor" instead of "link3"?
Thanks in advance
Hi Kate,
"link3" must be the alias of your property in your document type, so you would need to update the property in the document type?
Is this property using a uComponents URLPicker property editor?
Jeavon
Hi Jeavon
"link3" is the alias of my property in my document type :-)
And yes, im using the uComponents URLPicker :-)
Ok, so if you want your code to be:
Just change the alias of the property in your document type to "propNameRazor"
Jeavon
You may also need to @Html.Raw your linkTarget.
This is the code sample we use for uComponents Razor Macro (without Model Binding)
It still won't work.
When I write "propNameRazor" insted of "link3" it does not do anything. So do I have to declare the macro parameter in the script file?
Something like this:
in my template i have this:
In my script file, when I write this:
it spits out "link3"
So I know that there is a value from the parameter. I just dont know how to conect my variable "propNameRazor" with this line:
Ah I see, try this:
it gave my an error
If I just write this
This is what comes out on the page:
It seems that it does not like this if-sentenst
Ah yes, you will need:
and add
@using umbraco.MacroEngines
at the topLike this:
Sorry, it didn't work.
can it be something with my Parameter. It's Type is set to "Text". Should that be something else?
Wow, it's more complicated that I remember, this seems to be working:
Jubii, it works
You're my favorite person of the week :-)
Thanks for your time and patience
/Kate
You're welcome and have a great weekend.
Depending on the version of uComponents you are using, you maybe able to simplify this a lot to:
That works great too and it's easier to read :-)
Have a nice weekend
is working on a reply...