How about creating a @helper method, which can be put in App_code folder and be used on any of your Razor files? Or an extension method on the Model that returns a DynamicNodeList, probably taking your propery alias as additional parameter?
BTW: related pages is uComponents MNTP saving in XML format.
@Dirk unfortunately you can't have extension methods on dynamic objects, I tried that.
However I just tried the obvious solution again and this time it works, I don't know If I failed last time or because we updated to latest macroEngine, but here it is, moving it into a static helper class:
using System.Collections.Generic;
using System.Web;
using umbraco.MacroEngines.Library;
namespace Terabyte.Umbraco.Extensions
{
public static class DynamicNodeExtensions
{
public static IEnumerable<dynamic> GetPublishedNodes(dynamic property)
{
foreach (var nodeId in property)
{
var page = (new RazorLibraryCore(null)).NodeById(nodeId.InnerText);
if (page.Id != 0)
{
yield return page;
}
}
}
}
}
In need of Razor syntactic sugar to help with 'is published' checking.
Hi all
Here's a pretty common scenario we face, and I'm wanting a way to make this DRYer.
How can I separate out all the logic above the 'HTML HERE' line into something re-usable?
@inherits umbraco.MacroEngines.DynamicNodeContext @{ if (Model.HasProperty("relatedPages") && Model.HasValue("relatedPages")) { List publishedRelatedPages = new List(); foreach (var nodeId in @Model.RelatedPages) { var page = Model.NodeById(nodeId.InnerText); if (page.Id != 0) { publishedRelatedPages.Add(page); } } if(publishedRelatedPages.Count > 0) { @* ---------- HTML HERE ---------- *@ <h2>Related Pages</h2> <ul> @foreach (var page in publishedRelatedPages) { <li> <a href="http://mce_host/forum/developers/razor/@page.NiceUrl">@page.Position(). @page.Name</a> </li> } </ul> } } }Hi Murray,
How about creating a @helper method, which can be put in App_code folder and be used on any of your Razor files? Or an extension method on the Model that returns a DynamicNodeList, probably taking your propery alias as additional parameter?
Hope this helps.
Regards,
/Dirk
BTW: related pages is uComponents MNTP saving in XML format.
@Dirk unfortunately you can't have extension methods on dynamic objects, I tried that.
However I just tried the obvious solution again and this time it works, I don't know If I failed last time or because we updated to latest macroEngine, but here it is, moving it into a static helper class:
using System.Collections.Generic; using System.Web; using umbraco.MacroEngines.Library; namespace Terabyte.Umbraco.Extensions { public static class DynamicNodeExtensions { public static IEnumerable<dynamic> GetPublishedNodes(dynamic property) { foreach (var nodeId in property) { var page = (new RazorLibraryCore(null)).NodeById(nodeId.InnerText); if (page.Id != 0) { yield return page; } } } } }and the razor:
@inherits umbraco.MacroEngines.DynamicNodeContext @using Terabyte.Umbraco.Extensions; @{ IEnumerable<dynamic> pages = DynamicNodeExtensions.GetPublishedNodes(@Model.RelatedPages); if (pages.Any()) { <h2>Related Pages</h2> <ul> @foreach (var page in pages) { <li> <a href="@page.NiceUrl">@page.Name </li> } </ul> } }Improvements welcome
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.