简体   繁体   中英

Displaying Subnode content on parent node in Umbraco

I've been having a lot of trouble figuring this out.

What I want is a FAQ page that displays all the questions and answers on the same page. It gets the content from the questions and answers from subnode content.

So for example my tree looks like this:

  • FAQ
    • Question1
    • Question2
    • Question3

I want the template on FAQList to list the question and answer data from Question1...2... and 3 on the same page.

Every time I try to find examples of this being done I can only find examples that list the subpages as links. I don't want to link to the subpages. I want to actually print the content from them onto the parent page. Is that possible?

This is my attempt at it:

<xsl:for-each select="$currentPage/node">

    Question: <xsl:value-of select="data [@alias = 'question']"/><br/>

    Answer: <xsl:value-of select="data [@alias = 'answer']"/><br/>

</xsl:for-each>

But I had no results. Help me out here. I'm banging my head on this.

It all depends on the version of Umbraco you're running. There's a lot of documentation out there that refers to a much earlier version of Umbraco and simply won't work on more recent versions.

Assuming the document type alias of your questions is called 'FaqItem' and assuming that this XSLT is run on the respective content node (ie $currentPage is your FAQ parent node), you can use the following:

If you're using < Umbraco 4.5.1

<xsl:for-each select="$currentPage/child::node[@nodeTypeAlias='FaqItem']">
    Question: <xsl:value-of select="./data[@alias='question']"/><br/>
    Answer: <xsl:value-of select="./data[@alias='answer']"/><br/>
</xsl:for-each>

If you're using >= Umbraco 4.5.1

<xsl:for-each select="$currentPage/FaqItem">
    Question: <xsl:value-of select="./question"/><br/>
    Answer: <xsl:value-of select="./answer"/><br/>
</xsl:for-each>

For future reference

If you're familiar with XPath and want to figure out how Umbraco has stored the data, or to help with debugging. Look for a file called Umbraco.config (typically found in ~/App_Data/ ). This is the cached XML that all XSLTs will read from. Posting the relevant snippet from this file into your [Stack Overflow] question will increase the speed and chances of getting a response, as XSLT contributors will be able to help and not just Umbraco contributors.

Have to looked at razor? IMHO it is much easier to read and write.

@using System.Linq
@using System.Xml.Linq
@using umbraco.MacroEngines    

@{
    IEnumerable<DynamicNode> FAQs = new DynamicNode(Model.Id).Descendants("FaqItem").Items;
    List<DynamicNode> faqList = FAQs.ToList();

    @foreach(DynamicNode faq in faqList){
        Question: @(faq.GetProperty("question").ToString())
        Answer: @(faq.GetProperty("answer").ToString())
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM