Umbraco - showing nodes when not all medias are picked

Go To StackoverFlow.com

0

My intention is to create a list of news, and there will be attached a thumbnail (news_teaserimage) to some of the news. The problem is that if I only pick medias in some of the nodes, I get an xslt-error, and no code will be generated. If i pick medias in all nodes, then it works.. The intention is that the code will gererate the nodes no matter if there are an image or not. If there is no image picked then it won't be displayed.

What am I doing wrong?

<ul>

<xsl:for-each select="umbraco.library:GetXmlNodeById($source)/* [@isDoc and string(umbracoNaviHide) != '1']">
<xsl:variable name="media" select="umbraco.library:GetMedia(news_teaserimage, 0)"/>

  <li>
    <h2><xsl:value-of select="@nodeName"/></h2>
    <h5><xsl:value-of select="@createDate"/></h5>

<xsl:if test="news_teaserimage">
<img src="{$media/umbracoFile}" width="70" height="70" style="float: left; padding-right: 10px; padding-bottom: 10px;" />
</xsl:if>

      <xsl:value-of select="news_shorttext" disable-output-escaping="yes"/>
    <xsl:if test="news_largetext">
      <br />
<a style="float: left; clear: both; margin-top: -10px;" href="{umbraco.library:NiceUrl(@id)}">
      Read more
    </a>

</xsl:if>


  </li>
</xsl:for-each>
</ul>
2012-04-04 19:12
by Cfrim
Can you include the exact error message in your question, too. Is it "Value was either too large or too small for an Int32." - Goran Mottram 2012-04-05 00:50
Move the "umbraco.library:GetMedia(news_teaserimage, 0)" from the xsl:variable tag into the xsl:if tag, and wrap that xsl:if around all the rest of the code in the l - Paul Sweatte 2012-07-30 21:37


0

Calling GetMedia when there is nothing in the variable that you're calling it on (news_teaserimage in your case) will cause an XSLT error, as there's no media to get if the a media node hasn't been picked. You just need to re-jig your code slightly to get it to work. Move the line of code where you assign the "media" variable into the if statement where you check if the news_teaserimage element is there. That way it will only get called if there is actually a value present.

You may also need to change the if test to something like: string-length(news_teaserimage) > 0, as it may fire the if if the element is present but empty.

2012-04-10 09:57
by Tim


0

Move the umbraco.library:GetMedia(news_teaserimage, 0) call from the xsl:variable tag into the xsl:if tag, and wrap that xsl:if around all the rest of the code in the li:

<ul>
<xsl:for-each select="umbraco.library:GetXmlNodeById($source)/* [@isDoc and string(umbracoNaviHide) != '1']">

  <li>
  <xsl:if test="umbraco.library:GetMedia(news_teaserimage, 0)">
    <h2><xsl:value-of select="@nodeName"/></h2>
    <h5><xsl:value-of select="@createDate"/></h5>

    <img src="{$media/umbracoFile}" width="70" height="70" style="float: left; padding-right: 10px; padding-bottom: 10px;" />

    <xsl:value-of select="news_shorttext" disable-output-escaping="yes"/>

    <xsl:if test="news_largetext">
      <br />
      <a style="float: left; clear: both; margin-top: -10px;" href="{umbraco.library:NiceUrl(@id)}">
      Read more
      </a>
  </xsl:if>
  </li>
</xsl:for-each>
</ul>
2013-11-25 22:58
by Paul Sweatte
Ads