Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Oct 24, 2012 @ 09:52
    Anthony Candaele
    0

    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

  • Carsten Fallesen 35 posts 154 karma points
    Oct 24, 2012 @ 10:01
    Carsten Fallesen
    1

    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

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 24, 2012 @ 10:06
    Anthony Candaele
    0

    Hi Carsten,

    Thanks for your help, this works:

    var siteEmail = Model.AncestorsOrSelf().Where("NodeTypeAlias == \"SiteSettings\"").First().siteEmail;

    greetings,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 24, 2012 @ 10:35
    Anthony Candaele
    0

    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 &nbsp; | &nbsp; Fax.: @siteFax</p>

             

          </div>    

          <div id="copyright">

          <p>Ghent University &copy; @year   &nbsp; | &nbsp;<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

Please Sign in or register to post replies

Write your reply to:

Draft