I read that it's better, for performance reasons, not to use node.nodeTypeAlias to get all descendants, but to use Model.Children.Where("NodeTypeAlias == \"DocumentTypeAlias\")
So I tried to change my code to:
var siteEmail = Model.AncestorOrSelf.Where("NodeTypeAlias == \"SiteSettings\"").siteEmail;
But this doesn't work, I don't get an error, but I don't get the value of the property 'siteEmail' either.
I think there might be two errors in your code. The collection is named AncestorsOrSelf, note the extra s and the Where will return a collection, so you should pick the first:
var siteEmail = Model.AncestorsOrSelf.Where("NodeTypeAlias == \"SiteSettings\"").First().siteEmail;
Razor performance tweeking
Hi,
In my Razor script (footer.cshtml), I'm using this code to get to a property of a parent node:
var siteEmail = Model.AncestorOrSelf().siteEmail;
This works, but in the blogpost of James Dewer: Things learnt whilst using Umbraco (Part 3) - Performance
I read that it's better, for performance reasons, not to use node.nodeTypeAlias to get all descendants, but to use Model.Children.Where("NodeTypeAlias == \"DocumentTypeAlias\")
So I tried to change my code to:
var siteEmail = Model.AncestorOrSelf.Where("NodeTypeAlias == \"SiteSettings\"").siteEmail;
But this doesn't work, I don't get an error, but I don't get the value of the property 'siteEmail' either.
Does anyone has suggestions on this?
Thanks for your help,
Anthony
Hi Anthony,
I think there might be two errors in your code. The collection is named AncestorsOrSelf, note the extra s and the Where will return a collection, so you should pick the first:
var siteEmail = Model.AncestorsOrSelf.Where("NodeTypeAlias == \"SiteSettings\"").First().siteEmail;
I haven't tried this out, but hope it is right.
/Carsten
Hi Carsten,
Thanks for your help, this works:
var siteEmail = Model.AncestorsOrSelf().Where("NodeTypeAlias == \"SiteSettings\"").First().siteEmail;
greetings,
Anthony
I further tweaked my code for performance,
my Razor footer.cshtml script now looks like this:
@using umbraco.MacroEngines
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var parent = Model.AncestorsOrSelf().Where("NodeTypeAlias == \"SiteSettings\"").First();
var nodeid = Convert.ToInt16(parent.GetPropertyValue("siteDisclaimerPage"));
var disclaimerpage = umbraco.library.NiceUrl(nodeid);
var sitemap = Library.NodeById(parent.GetPropertyValue("siteSiteMap"));
var siteName = parent.GetPropertyValue("siteName");
var siteAddress = parent.GetPropertyValue("siteAddress");
var siteEmail = parent.GetPropertyValue("siteEmail");
var siteTelephone = parent.GetPropertyValue("siteTelephone");
var siteFax = parent.GetPropertyValue("siteFax");
var year = DateTime.Today.Year;
}
<div class="main">
<div id="siteaddress">
<p>
@siteName<br />
@siteAddress<br />
Email: <a href="@siteEmail">@siteEmail</a><br />
Tel.:@siteTelephone | Fax.: @siteFax</p>
</div>
<div id="copyright">
<p>Ghent University © @year | <a href="@disclaimerpage">Read our disclaimer</a><br />
<a href="@sitemap.Url">Sitemap</a></p>
</div>
</div>
Hope this can be of any help to someone else tweaking his Razor scripts for performance
greetings,
Anthony
is working on a reply...