Copied to clipboard

Flag this post as spam?

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


  • karen 186 posts 461 karma points
    Jun 21, 2013 @ 22:57
    karen
    0

    xslt to razor help

    I am trying to learn razor, so am trying to convert a simple xslt macro to a razor partial, but not having much luck. I've looked at 'Umbraco 4.7 Razor Feature Walkthrough' and checked the forums, google, etc, and not been able to come up with a working solution.  (I am fairly new to razor and paritals and pretty much all things MVC, understand the concepts, just not much working experience)

    My xslt looks like this (basically selects all items of doc type News in the content tree)

    <xsl:param name="currentPage"/>
    <xsl:param name="contentRoot" select="$currentPage/ancestor-or-self::root" />
    
    <xsl:template match="/">
        <xsl:for-each select="$contentRoot/NewsFolder//News">
            <xsl:value-of select="headline"/><br/>
        </xsl:for-each>
    </xsl:template>
    
    

    How do I create partial to do the same thing?  

     

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jun 22, 2013 @ 08:30
    Jeavon Leopold
    0

    Hi Karen,

    In a strongly typed partial it would be like this:

     @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var currentRoot = Model.Content.AncestorOrSelf(1);
    
        var newsCollection = currentRoot.Children.Where(x => x.DocumentTypeAlias == "NewsFolder").First().Children.Where(x => x.DocumentTypeAlias == "News");
        foreach (var newsItem in newsCollection)
        {
            <p>@newsItem.GetPropertyValue("headline")</p>
        }
    }

     Then to render in your View (assuming your partial is callewd News)

     @Html.Partial("News",Model)

     Hope that helps get your started.

    Thanks,

    Jeavon

  • karen 186 posts 461 karma points
    Jun 24, 2013 @ 17:08
    karen
    0

    Hi Jeavon,

    Thanks!

    I tried this, but am getting an error on the following line:

    var newsCollection = currentRoot.Children.Where(x => x.DocumentTypeAlias=="NewsFolder").First().Children.Where(x => x.DocumentTypeAlias=="News");

    Error:

    Exception Details: System.InvalidOperationException: Sequence contains no elements

     

    So an additional queston, is how to avoid the page throwing such an error message?

     

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jun 24, 2013 @ 17:13
    Jeavon Leopold
    0

    Hi Karen,

    Ok, so that is happening because it cannot locate your NewsFolder, can I check that your tree structure is as follows (with each item being a parent node of the next)

    Content (root)
       Homepage
         NewsFolder
           News
           News
         OtherPage 

    Or is NewsFolder a sibling of Homepage?

    Thanks,

    Jeavon

  • karen 186 posts 461 karma points
    Jun 24, 2013 @ 17:35
    karen
    0

    Hi,

    It is:

    Content (root)
    - Homepage
    -- Some Pages
    - NewsFolder
    -- News

    The xslt is working, so trying to base it off that

    Thanks

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jun 24, 2013 @ 17:40
    Jeavon Leopold
    1

    Ah cool, ok so try:

     var newsCollection = currentRoot.Sibling("NewsFolder").Children.Where(x => x.DocumentTypeAlias == "News");

     

  • karen 186 posts 461 karma points
    Jun 24, 2013 @ 17:58
    karen
    0

    OK - that works.

    But is there a way to make it better match the xslt?  That is, get the actual root node of the content tree (vs the 1st level), then get something under it.

    In xslt:

    <xsl:paramname="contentRoot"select="$currentPage/ancestor-or-self::root"/>

    vs this seems to get 1 level under

    var currentRoot = Model.Content.AncestorOrSelf(1);

    So I want something like this (but this doesn't work)

    var currentRoot = Model.Content.AncestorOrSelf();
    var newsFolder = currentRoot.Children.Where(x => x.DocumentTypeAlias == "NewsFolder").First();
    var newsCollection = newsFolder.Children.Where(x => x.DocumentTypeAlias == "News");
  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jun 24, 2013 @ 18:05
    Jeavon Leopold
    0

    Hi Karen,

    No I do not believe that there is a way to specifically select the root node because it is not a real node (it has no properties). You can select all the siblings of your current root node (all the children under the root node). e.g.

     var allRootNodeChildren = Model.Content.AncestorOrSelf(1).Siblings();

    And from there you have access to every node within the Umbraco install.

    Hope that's helpful?

    Thanks,

    Jeavon

  • karen 186 posts 461 karma points
    Jun 24, 2013 @ 18:11
    karen
    0

    OK thank you very much for your help!

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jun 24, 2013 @ 19:25
    Jeavon Leopold
    0

    You're very welcome! Thanks for your "High five!" above, that was my 1000th karma point :-)

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Jul 04, 2013 @ 16:38
    Jeavon Leopold
    0

    Hi Karen,

    I just discovered that in Umbraco v6.1+ there is a new helper method called TypedContentAtXPath that can do exactly what you need:

    var newsCollection = Umbraco.TypedContentAtXPath("//News");

    The method lets you execute a XPath expression against your entire tree.

    Hope that's helpful!

    Jeavon

  • karen 186 posts 461 karma points
    Jul 05, 2013 @ 17:10
    karen
    0

    Awesome!  Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft