Copied to clipboard

Flag this post as spam?

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


  • Grant Perkins 11 posts 31 karma points
    May 20, 2011 @ 11:36
    Grant Perkins
    0

    List top level nodes

    Hi,

    I have a structure for all my different languages like this:

    en

    -page 1

    -page 2

    es

    -page 1

    -page 2

    Whats the easiest way to iterate through the top level nodes getting their URL's etc. Ideally i would like to do it in c# rather than xslt, but am open to ideas.

    Thanks in advance.

  • dimi309 245 posts 579 karma points
    May 20, 2011 @ 11:45
    dimi309
    0

    Something like this in razor?

     

     var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);

    var parent = @Model.AncestorOrSelf(level);
       if (parent != null)
       {

    @foreach (var item in parent.Children)
            {

    string link = item.Url;

     

     

    ........................................

    }

    }                                                                      Dimitri

     

     

  • Grant Perkins 11 posts 31 karma points
    May 20, 2011 @ 11:48
    Grant Perkins
    0

    Sorry mate, i should have said, im using version 4.5!

  • dimi309 245 posts 579 karma points
    May 20, 2011 @ 12:07
    dimi309
    0

    Ah ok! My bad...

    It should be something like this using the API:

    using umbraco.BusinessLogic;

    using umbraco.cms.businesslogic.web;

    using umbraco.cms.businesslogic;

     Document parent = new Document(doc.ParentId);

                    foreach (Document child in parent.Children)

                    {

                        child.Path ....

    }

     

    I could be wrong but I think you will lose performance if you traverse them like this though... Maybe it's better to do it in xslt:

    <xsl:for-each select="$currentPage/ancestor-or-self::*[@level=$Level and @isDoc]/child::*[@isDoc]" >

    <xsl:value-of disable-output-escaping="yes" select="umbraco.library:NiceUrl(@id)"/>

    </xsl:for-each>

    Sorry if these are not 100% exact but I hope they help as a starting point...

     

    Dimitri

     

     

  • Grant Perkins 11 posts 31 karma points
    May 20, 2011 @ 12:38
    Grant Perkins
    0

    Hi Dimitri,

     

    Thanks for your reply. I think il give you some more info to make things more clearer.

    For each top level node i have added 2 properties, a Country Name and a Continent (both textString).

    So what i need to do is select all the top level nodes within a certain continent and print out their country names.

    Literally been looking at umbraco for a few days so some of it is quite new to me!

     

  • Rich Green 2246 posts 4008 karma points
    May 20, 2011 @ 12:55
    Rich Green
    0

    Can I ask why you're doing this in c#?

    This could be done in about 30 secs using xslt.

    Rich

  • Grant Perkins 11 posts 31 karma points
    May 20, 2011 @ 12:59
    Grant Perkins
    0

    Hi Rick,

     

    Because c# is what i know, but like i said im open to xslt if its more efficient! Just looked at the xslt and i currently have:

    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]//* [@isDoc and @area ='Europe']">
        <xsl:value-of select="countryName"/>
    </xsl:for-each>

    not sure if thats heading the right way or not, i am having trouble understanding all the tags and breaking it down!

  • Rich Green 2246 posts 4008 karma points
    May 20, 2011 @ 13:22
    Rich Green
    0

    Hey Grant,

    Your code won't really work as when you move around the site the 'currentpage' you are on changes.

    What you want is some xslt that produces the same results regardless of where you are in the site.

    1: Go to Developer section

    2: New XSLT file

    3: Choose 'Navigation Prototype' from the drop down

    4: Type a name and leave 'Create Macro' ticked

    5: Insert this macro into your template

    You should see a level of navigation appear with the nice urls etc.

    If the wrong level of nav is appearing change the variable inside the script 

    <!-- Input the documenttype you want here -->
    <!-- Typically '1' for topnavigtaion and '2' for 2nd level -->
    <!-- Use div elements around this macro combined with css -->
    <!-- for styling the navigation -->
    <xsl:variable name="level" select="1"/>

    Let me know if that helps

    Rich

  • dimi309 245 posts 579 karma points
    May 20, 2011 @ 13:35
    dimi309
    0

    It can be a bit confusing at times it's true... One thing though... the names that are preceded by a @ are xml properties and they would appear like this in the source xslt:

    for example

    <language @id> ..... </language>

    whereas properties you define yourself in the document types are other tags for example:

    <language @id><area><![CDATA[Europe]]></area></language>

    So if you have defined "area" as a field in a document type normally you would write something like area = 'Europe' not @area = 'Europe'

    I am sorry if this does not seem very clear. I am not perfectly comfortable with this myself and I could be making mistakes even in the things I am writing. It takes a bit of practice and I still work a lot through trial an error.

    What I did when I was starting out, which might help if you would like to do the same, was to work on my projects while taking breaks to do the xslt and xpath turorials on w3schools: http://www.w3schools.com/

    Also, a bit of info which I hadn't come accross for a long time: In case you are not aware of it yet, the entire xml structure of your site can be found in your umbraco installation in /App_Data/umbraco.config . Looking at this while you are writing xslt scripts will make you more comfortable with what you are querying against.

     

    Dimitri

Please Sign in or register to post replies

Write your reply to:

Draft