简体   繁体   English

在Umbraco的父节点上显示子节点内容

[英]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. 我想要的是一个FAQ页面,该页面在同一页面上显示所有问题和答案。 It gets the content from the questions and answers from subnode content. 它从子节点内容的问题和答案中获取内容。

So for example my tree looks like this: 例如,我的树看起来像这样:

  • FAQ 常问问题
    • Question1 问题1
    • Question2 问题2
    • Question3 问题3

I want the template on FAQList to list the question and answer data from Question1...2... and 3 on the same page. 我希望FAQList上的模板在同一页面上列出Question1 ... 2 ...和3中的问题和答案数据。

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. 这完全取决于您正在运行的Umbraco的版本。 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. 那里有很多文档都引用了Umbraco的早期版本,因此根本无法在较新的版本上使用。

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: 假设您问题的文档类型别名为“ FaqItem”,并且假设此XSLT在相应的内容节点上运行(即$currentPage是您的FAQ父节点),则可以使用以下命令:

If you're using < Umbraco 4.5.1 如果您使用的是<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 如果您使用> = 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. 如果您熟悉XPath,并且想弄清楚Umbraco如何存储数据,或者帮助调试。 Look for a file called Umbraco.config (typically found in ~/App_Data/ ). 查找名为Umbraco.config的文件(通常在~/App_Data/ )。 This is the cached XML that all XSLTs will read from. 这是所有XSLT将从中读取的缓存XML。 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. 将相关片段从此文件发布到您的[Stack Overflow]问题中,将会提高响应速度和机会,因为XSLT贡献者将不仅可以从Umbraco贡献者那里获得帮助。

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())
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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