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.
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"/>
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:
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.
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.
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
Sorry mate, i should have said, im using version 4.5!
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
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!
Can I ask why you're doing this in c#?
This could be done in about 30 secs using xslt.
Rich
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!
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
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
is working on a reply...