Passing an umbraco Node as a parameter to a helper function
Hi,
I'm new to Razor and was wondering how I can pass an umbraco node to a helper function. Declaring the parameter as a Node does not work. I've got the following sample listed below.
@using umbraco.MacroEngines; @helper PrintBlock(DynamicNode node) { if (node!= null) { <div class="pnl"> if (!String.IsNullOrEmpty(node.title)) { <h2>@node.title</h2> } <p>@node.content</p> if (!String.IsNullOrEmpty(node.referral)) { var referral= Model.GetNodeById(node.referral); if (referral != null) { <a href="@referral.Url" class="btn">@node.referralText</a> } } </div> } }
I'm still learning myself. I, basically, moved away from dynamic properties in favor of method GetPropertyValue. (e.g. node.GetPropertyValue("referralText")). Instead of Model.GetNodeById, you could go Umbraco.TypedContent(Model.GetPropertyValue("info1")) then you are passing a typed object to the helper (have to update the helper, of course).
Passing an umbraco Node as a parameter to a helper function
Hi,
I'm new to Razor and was wondering how I can pass an umbraco node to a helper function. Declaring the parameter as a Node does not work. I've got the following sample listed below.
@using umbraco.MacroEngines;
@helper PrintBlock(DynamicNode node)
{
if (node!= null)
{
<div class="pnl">
if (!String.IsNullOrEmpty(node.title))
{
<h2>@node.title</h2>
}
<p>@node.content</p>
if (!String.IsNullOrEmpty(node.referral))
{
var referral= Model.GetNodeById(node.referral);
if (referral != null)
{
<a href="@referral.Url" class="btn">@node.referralText</a>
}
}
</div>
}
}
@PrintBlock(Model.GetNodeById(Model.info1));
@PrintBlock(Model.GetNodeById(Model.info2));
@PrintBlock(Model.GetNodeById(Model.info3));
Info1, 2, and 3 are ContentPickers on the current node. Same with referral, this is a ContentPicker on the node referred to bij info1 (or 2 or 3).
I get the exception that 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'title' and no extension method 'title' accepting a first argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?). How can I pass the resulting node from Model.GetNodeById to a helper function?
Any help appreciated.
Regards,
Erik.
I'm still learning myself. I, basically, moved away from dynamic properties in favor of method GetPropertyValue. (e.g. node.GetPropertyValue("referralText")). Instead of Model.GetNodeById, you could go Umbraco.TypedContent(Model.GetPropertyValue("info1")) then you are passing a typed object to the helper (have to update the helper, of course).
Here is some code that works for me
is working on a reply...