I have a site set up using the Creative Website Starter Kit and I'm trying to create a macro that will display the news and event items on the homepage, along with their associated image.
I can display text data such as the news item name and body text but i'm having trouble displaying the image.
Below is a section of my current code (which has gone through what seems like 100 iterations). This gives an error when it tries to parse the XSLT.
System.Xml.Xsl.XslLoadException: The variable or parameter 'source' is either not defined or it is out of scope. An error occurred at C:\Users\pc\Documents\My Web Sites\Umbraco test\xslt\635023252788121614_temp.xslt(29,3). at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)
XSLT to display news item image
I have a site set up using the Creative Website Starter Kit and I'm trying to create a macro that will display the news and event items on the homepage, along with their associated image.
I can display text data such as the news item name and body text but i'm having trouble displaying the image.
Below is a section of my current code (which has gone through what seems like 100 iterations). This gives an error when it tries to parse the XSLT.
<ul class="newshome">
<xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node [string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:sort select="@updateDate" order="descending"/>
<xsl:if test="position() <= $numberOfItems">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<!-- If news item has an image, display that, otherwise display a placeholder -->
<xsl:choose>
<xsl:when test="current()/articlePhoto != ''">
<xsl:variable name="articlePhoto" select="umbraco.library:GetMedia(current()/articlePhoto, 'false')" />
<img src="{$articlePhoto/umbracoFile}" />
</xsl:when>
<xsl:otherwise>
<img src="/media/Assets/Placeholders/photo_placeholder.gif" alt="{@nodeName}" style="width:297px; height:145px;" />
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="@nodeName"/>
</a>
<!-- Used to show a sample of the news item -->
<br />
<xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(data [@alias = 'bodyText']), 150, '...')" />
</li>
</xsl:if>
</xsl:for-each>
</ul>
dunc85,
what version of umbraco are you using? It seems you're combining old and new xml syntax in your xslt. Also, what's the error message?
Old syntax from your code snippet
New syntax in your code snippet
Looking forward to your feedback.
Cheers,
/Dirk
Hi,
I'm on 4.0.3. The code is cobbled together from stuff I've found on the forum. I had originally used $currentPage.
The most detailed error message I can get is: Error parsing XSLT file: \xslt\ListNewsOnHomepage.xslt
Is there a way to get something more detailed?
A detailed err msg can be seen when appending ?umbdebugshowtrace=true to the url of the page, err msg will be in red.
If you're using a v4.0.3 version, you should NOT use the new xml schema which has only been introduced as from v4.5 onwards.
<ul class="newshome">
<xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node [string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:sort select="@updateDate" order="descending"/>
<xsl:if test="position() <= $numberOfItems">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<!-- If news item has an image, display that, otherwise display a placeholder -->
<xsl:choose>
<xsl:when test="./data [@alias = 'articlePhoto'] != ''">
<xsl:variable name="articlePhoto" select="umbraco.library:GetMedia(./data [@alias = 'articlePhoto'], 'false')" />
<img src="{$articlePhoto/umbracoFile}" />
</xsl:when>
<xsl:otherwise>
<img src="/media/Assets/Placeholders/photo_placeholder.gif" alt="{@nodeName}" style="width:297px; height:145px;" />
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="@nodeName"/>
</a>
<!-- Used to show a sample of the news item -->
<br />
<xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(./data [@alias = 'bodyText']), 150, '...')" />
</li>
</xsl:if>
</xsl:for-each>
</ul>
Above snippet will get you closer to the solution (haven't verified the xslt...)
Cheers,
/Dirk
Hi,
thanks for your help so far. I already knew about the debug trace but for some reason it was not showing the detailed error.
The code you provided seems closer, but the src is blank, any ideas?
yup, should be
<img src="{$articlePhoto/data [@alias = 'umbracoFile']}" />
(I'm also assuming this is a mediapicker property rather than an upload)
Hope this helps.
Regards,
/Dirk
Brilliant, it works great now. Thanks so much.
Hello
I tried to use this code in Umbraco 6
For example:
<ul class="newshome">
<xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node [string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:sort select="@updateDate" order="descending"/>
<xsl:if test="position() <= $numberOfItems">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<!-- If news item has an image, display that, otherwise display a placeholder -->
<xsl:choose>
<xsl:when test="./data [@alias = 'articlePhoto'] != ''">
<xsl:variable name="articlePhoto" select="umbraco.library:GetMedia(./data [@alias = 'articlePhoto'], 'false')" />
<img src="{$articlePhoto/data [@alias = 'umbracoFile']}" />
</xsl:when>
<xsl:otherwise>
<img src="/media/Assets/test.jpg" alt="{@nodeName}" style="width:429px; height:321px;" />
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="@nodeName"/>
</a>
<!-- Used to show a sample of the news item -->
<br />
<xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(./data [@alias = 'bodyText']), 150, '...')" />
</li>
</xsl:if>
</xsl:for-each>
</ul>
But I have error
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Error occured
System.Xml.Xsl.XslLoadException: The variable or parameter 'source' is either not defined or it is out of scope. An error occurred at C:\Users\pc\Documents\My Web Sites\Umbraco test\xslt\635023252788121614_temp.xslt(29,3).
at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Question is. How display the image of the news while displaying a list of news with Introduction in Umbraco 6?
Could you please help me...
Peter
is working on a reply...