Copied to clipboard

Flag this post as spam?

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


  • dunc85 39 posts 42 karma points
    Sep 14, 2010 @ 14:53
    dunc85
    0

    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() &lt;= $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>

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 14, 2010 @ 15:02
    Dirk De Grave
    0

    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

    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node [string(data [@alias='umbracoNaviHide']) != '1']">

    New syntax in your code snippet

    <xsl:when test="current()/articlePhoto != ''">

    Looking forward to your feedback.

     

    Cheers,

    /Dirk

  • dunc85 39 posts 42 karma points
    Sep 14, 2010 @ 15:17
    dunc85
    0

    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?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 14, 2010 @ 15:22
    Dirk De Grave
    0

    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() &lt;= $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

  • dunc85 39 posts 42 karma points
    Sep 14, 2010 @ 15:29
    dunc85
    0

    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?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 14, 2010 @ 15:36
    Dirk De Grave
    0

    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

  • dunc85 39 posts 42 karma points
    Sep 14, 2010 @ 15:41
    dunc85
    0

    Brilliant, it works great now. Thanks so much.

  • Peter 6 posts 26 karma points
    Apr 23, 2013 @ 15:56
    Peter
    0

    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() &lt;= $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


     

Please Sign in or register to post replies

Write your reply to:

Draft