Media Picker - check if image is chosen Options
mikkelstrack
Posted: Monday, May 05, 2008 5:11:42 PM
Rank: Newbie

Joined: 3/25/2008
Posts: 11
Hi,

I have made a document type where an image can be selected with the Media Picker, but I get an xslt-error on the published page if an image has not been selected.

How do I check if an image has been selected?
mortenbock
Posted: Monday, May 05, 2008 5:24:32 PM

Rank: Addict

Joined: 7/19/2006
Posts: 738
Location: Århus, Denmark
There are a few ways to do this. How do you get the image property in your XSLT? Is it passed via. a macro parameter, or do your get it from the $currentNode parameter?

What you probably want to do is use either an xsl:if or a xsl:choose, depending on what should happen if there is no image chosen.

Could you describe what you need to do if no image has been selected? Should it just not be displayed, or should it inherit a value from the parent node, or should it default to some hardcoded image?

Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

mikkelstrack
Posted: Monday, May 05, 2008 6:01:47 PM
Rank: Newbie

Joined: 3/25/2008
Posts: 11
Hi Morten

I use a macro like this i found here in the forum

<xsl:template match="/">

<img src="{umbraco.library:GetMedia($currentPage/data[@alias='ProfileImage'], 'false')/data [@alias = 'umbracoFile']}"/>

</xsl:template>

In case no image is selected, nothing will be displayed.

Hope this information is enough :)
widescreen
Posted: Monday, May 05, 2008 6:42:46 PM

Rank: Newbie

Joined: 3/29/2008
Posts: 13
Location: NC
Code that I use looks something like this

<xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:choose>
<xsl:when test="string-length(data [@alias='TeaserImage'])">
<div class="TeaserImage">
<img width='150px' src="{umbraco.library:GetMedia(data [@alias = 'TeaserImage'],true)/data [@alias = 'umbracoFile']}" />
</xsl:when>
<xsl:otherwise>
<img width='150px' src="/data/assests/imgnotfound.png" />
</xsl:otherwise>
</xsl:choose>

Hope that helps,

Hal
mortenbock
Posted: Monday, May 05, 2008 8:07:48 PM

Rank: Addict

Joined: 7/19/2006
Posts: 738
Location: Århus, Denmark
Mikkel, in your case, I think you should use something like this:

Code:

  <xsl:template match="/">
    <xsl:if test="$currentPage/data[@alias='ProfileImage'] != ''">
      <img src="{umbraco.library:GetMedia($currentPage/data[@alias='ProfileImage'], 'false')/data [@alias = 'umbracoFile']}"/>
    </xsl:if>
  </xsl:template>


I think that the media picker returns an empty string if nothing is selected, så that should do the job.

It might return a "0", but then you just change your xsl:if to handle that instead.

If you want to see what exactly your mediapicker returns, then use

Code:

<xsl:copy-of select="$currentPage/data[@alias='ProfileImage']"/>


Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

mikkelstrack
Posted: Tuesday, May 06, 2008 11:17:40 AM
Rank: Newbie

Joined: 3/25/2008
Posts: 11
Morten...it worked just fine!

Thx :)
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.