Copied to clipboard

Flag this post as spam?

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


  • Laura Holland 82 posts 103 karma points
    Aug 26, 2011 @ 19:38
    Laura Holland
    0

    Render first child node on parent page

    I have a "news" section, set up as follows:

    What's New (parent node):

    • most recent news
    • second most recent news
    • third most recent news
    • etc.

    I would like the "What's New" page to have the same content as the "most recent news" node, but without redirecting. I'm also trying to have a "next" button on the "What's New" page that will skip directly to the "second most recent news" (so that there won't be any duplicates for the user).

    I can get everything to work fine and page back and forth on the child nodes, but I'm having trouble with the "What's New" parent node.

    Any suggestions are greatly appreciated.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Aug 26, 2011 @ 19:50
    Tom Fulton
    1

    Hi Laura,

    You might want to post your XSLT so we can get a better idea of your problem, but to get a specific news item I believe you can add [#] to the end of your XPath filter.  Something like this might work:

    <!-- Link to the second news item -->
    <xsl:variable name="secondNewsItem" select="$currentPage/NewsItem [2]"/>
    <a href="{umbraco.library:NiceUrl($secondNewsItem/@id)}">Next (<xsl:value-of select="$secondNewsItem/@nodeName"/>)</a>
    <!-- Show the first news item -->
    <xsl:variable name="firstNewsItem" select="$currentPage/NewsItem[1]"/>
    <xsl:value-of select="$firstNewsItem/@nodeName"/>

    Of course this assumes they are already in the order you want...are you wanting to sort them by date and get the most recent, or are they already ordered correctly in the node tree?

    -Tom

  • Laura Holland 82 posts 103 karma points
    Sep 12, 2011 @ 18:45
    Laura Holland
    0

    After digging around, it seems like the renderTemplate feature would be ideal for my needs, but I can't get it to work. This is what I have, but it's not working (not picking up the first child node's ID and template and passing it on to the current page to display). I hard-coded the node ID and template ID into the renderTemplate macro to test it, and it works fine that way, but of course that won't work long term because the first child node will be different every time a newws story is added.

    <xsl:param name="currentPage"/>
      <xsl:param name="renderFirstSubnode" select="/macro/renderFirstSubnode"/>

      <xsl:variable name="PageId">
        <xsl:choose>
          <xsl:when test="$renderFirstSubnode='true'">
            <xsl:value-of select="$currentPage/node/@id"/>
          </xsl:when>
          <xsl:when test="$currentPage/data [@alias = 'RenderTemplate']!=''">
            <xsl:value-of select="$currentPage/data [@alias = 'RenderTemplate']"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="/macro/pageId"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="TemplateId">
        <xsl:choose>
          <xsl:when test="/macro/templateId != ''">
            <xsl:value-of select="/macro/templateId"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="umbraco.library:GetXmlNodeById($PageId)/./@template"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:template match="/">
        <xsl:choose>
          <xsl:when test="string($PageId)!='' and string($TemplateId)!=''">
            <xsl:value-of select="umbraco.library:RenderTemplate($PageId, $TemplateId)" disable-output-escaping="yes"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>An error has occured.</xsl:text>
            <br />
            <xsl:text>Please contact webmaster to resolve this error.</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
  • wolulcmit 357 posts 693 karma points
    Sep 13, 2011 @ 05:09
    wolulcmit
    0

    Would this work? it sounds to me as though your renderFirstSubnode isn't returning anything

    <xsl:variable name="renderFirstSubnode"select="$currentPage/node[position()=1]"/>

    take a look at this post it may also help
    http://our.umbraco.org/forum/developers/xslt/2834-Redirect-to-first-child-node-in-xslt

  • Rich Green 2246 posts 4008 karma points
    Sep 13, 2011 @ 09:12
    Rich Green
    1

    I would add a template named something like "Redirect to first child"

    Then in that template add this code

     

    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)

    {

    var current = umbraco.NodeFactory.Node.GetCurrent();

    if (current != null)

    {

    var child = current.ChildrenAsList.FirstOrDefault();

    if (child != null)

    {

    var url = child.Url;

    if (!string.IsNullOrEmpty(url))

    {

    Response.Redirect(url, true);

    }

    }

    }

    }

    </script>

     

    Then allow the parent node to use this template, select the template for 'What's new', then you should be done.

    Rich

     

  • Laura Holland 82 posts 103 karma points
    Sep 15, 2011 @ 02:28
    Laura Holland
    0

    Thanks Rich! That worked well.. not exactly what I was planning on doing (wasn't planning on actually redirecting the page), but works just fine anyway.

Please Sign in or register to post replies

Write your reply to:

Draft