Is it possible to hide an <li> if link not populated
Hi,
I'm currently using the following code to display some links...
<ul class="downlinks"> <li><a href='<umbraco:Item field="umbracoFile" nodeid="[#roomset]" runat="server"></umbraco:Item>' rel="lightbox">See this product in a roomset</a></li> <li><a href='<umbraco:Item field="umbracoFile" nodeid="[#fullSpecLink]" runat="server" target="_blank"></umbraco:Item>'>Download full specifications</a></li> <li><a href='<umbraco:Item field="umbracoFile" nodeid="[#userManual]" runat="server" target="_blank"></umbraco:Item>'>Download User Manual</a></li> <li><a href='<umbraco:Item field="umbracoFile" nodeid="[#addManual]" runat="server" target="_blank"></umbraco:Item>'>Additional Manual</a></li> </ul>
It's possible that not all of these links will be populated, so my question is...is it possible to hide hide the list item if the link has nothing attached to it?
you have a number of attributes on the umbraco:item elements (link), but I'm not sure you will be able to remove the <li> elements.
Alternatively you should create an xslt macro for either the full list or individual list elements taking the nodeid as argument, and the testing for empty links within the macro.
<umbraco:Item field="umbracoFile" nodeId="[#roomset]" insertTextBefore="<li><a href="" insertTextAfter="" rel="lightbox">See this product in a roomset</a></li>" runat="server"></umbraco:Item>
Haven't tried this, but worth a shot.... Otherwise, I'd suggest to move this piece of logic to an xslt file..
What's happening here? We're checking if your current page has a property value for 'userManual', then using that nodeId/value to get the node data - and then checking if the 'umbracoFile' has a value ... if so, then display the <li> tag.
I haven't tested this XSLT ... you'll need to tweak it to suit your needs.
Dirk, I tried changing the syntax and it removed the link but it did so regardless of whether there is an attachment or not!
So, I moved on to the xslt route, unfortunately my xslt knowledge is very limited at the moment so by best hope was to copy and paste the code that Lee provided - this also removed the link regardless of whether there is an attachment or not!
I appreciate you don't have time to write my code for me but if anyone has any tweaking suggestions on the above code it would be much appreciated.
Hi Rachel, Lee's snippet assumes that you have the fields userManual,... as nodes on your current page. If these fields are media picker fields instead of content pickera, you would normally use GetMedia to fetch the image nodes. So you could try substituting GetXmlNodeById by GetMedia.
If you could provide some details about your content structure - as in which properties you use, etc. or copy-n-paste a sample from your /data/umbraco.config file (it's the XML cache of your site). Then we should be able to point you in the right direction.
You don't need to copy everything from umbraco.config - just a snippet of relevant content/nodes.
Ok, I am using a media picker and so i substituted GetXmlNodeById for GetMedia and I know that this line returns the correct information (ie. link to my pdf download)
it displays the list item if userManual is not blank or doesn't display it if it is - this is exactly what I want but I need the link, if displayed, to link to userManual.
For some reason, the second if doesn't appear to be working in the full code:
Your 2nd code snippet is using "current()" - as in the current node - and your 3rd snippet is using $currentPage - which is the node of the current page. So there are different contexts. Try replacing the "$currentPage" with "current()" - and see what happens.
Sorry Rachel, I re-read your code snippet ... I got it around the wrong way - swap the "current()" with "$currentPage" ... sorry about that - getting cross-eyed.
Is it possible to hide an <li> if link not populated
Hi,
I'm currently using the following code to display some links...
It's possible that not all of these links will be populated, so my question is...is it possible to hide hide the list item if the link has nothing attached to it?
Many thanks,
Rachel
Hi Rachel,
you have a number of attributes on the umbraco:item elements (link), but I'm not sure you will be able to remove the <li> elements.
Alternatively you should create an xslt macro for either the full list or individual list elements taking the nodeid as argument, and the testing for empty links within the macro.
>Tommy
Me thinks you can change the syntax to:
Haven't tried this, but worth a shot.... Otherwise, I'd suggest to move this piece of logic to an xslt file..
Hope this helps.
Regards,
/Dirk
Hi Rachel,
You can do this in XSLT, by wrapping IF conditions around each of the <li> tags:
<xsl:if test="string($currentPage/data[@alias='userManual']) != ''"> <xsl:variable name="file" select="umbraco.library:GetXmlNodeById($currentPage/data[@alias='userManual'])" /> <xsl:if test="string($file/data[@alias='umbracoFile']) != ''"> <li> <a href="{$file/data[@alias='umbracoFile']}">Download User Manual</a> </li> </xsl:if> </xsl:if>What's happening here? We're checking if your current page has a property value for 'userManual', then using that nodeId/value to get the node data - and then checking if the 'umbracoFile' has a value ... if so, then display the <li> tag.
I haven't tested this XSLT ... you'll need to tweak it to suit your needs.
Good luck, Lee.
Hi guys,
Thanks for your quick responses.
Dirk, I tried changing the syntax and it removed the link but it did so regardless of whether there is an attachment or not!
So, I moved on to the xslt route, unfortunately my xslt knowledge is very limited at the moment so by best hope was to copy and paste the code that Lee provided - this also removed the link regardless of whether there is an attachment or not!
I appreciate you don't have time to write my code for me but if anyone has any tweaking suggestions on the above code it would be much appreciated.
Thanks,
Rachel
Hi Rachel, Lee's snippet assumes that you have the fields userManual,... as nodes on your current page. If these fields are media picker fields instead of content pickera, you would normally use GetMedia to fetch the image nodes. So you could try substituting GetXmlNodeById by GetMedia.
Otherwise check if you go into the first IF.
>Tommy
Hi Rachel,
If you could provide some details about your content structure - as in which properties you use, etc. or copy-n-paste a sample from your /data/umbraco.config file (it's the XML cache of your site). Then we should be able to point you in the right direction.
You don't need to copy everything from umbraco.config - just a snippet of relevant content/nodes.
Thanks, Lee.
Hi,
Ok, I am using a media picker and so i substituted GetXmlNodeById for GetMedia and I know that this line returns the correct information (ie. link to my pdf download)
I know the first if statement works because whe I try the following:
it displays the list item if userManual is not blank or doesn't display it if it is - this is exactly what I want but I need the link, if displayed, to link to userManual.
For some reason, the second if doesn't appear to be working in the full code:
I hope this is a bit more helpful. I'm sorry but I'm not sure how to access the umbraco.config file??
Many thanks,
Rachel
Hi Rachel,
Your 2nd code snippet is using "current()" - as in the current node - and your 3rd snippet is using $currentPage - which is the node of the current page. So there are different contexts. Try replacing the "$currentPage" with "current()" - and see what happens.
Cheers, Lee.
Sorry, I just realised that and was coming back to amend my post!
My code now looks like this:
but still no joy I'm afraid.
Thanks, Rachel
Sorry Rachel, I re-read your code snippet ... I got it around the wrong way - swap the "current()" with "$currentPage" ... sorry about that - getting cross-eyed.
I think you should use currentPage instead of current():
Peter
Sorry Lee, still no joy :(
And thank you for your input Peter but as I said above, this still doesn't want to work!
ah....now I see it. You allready have the umbracoFile in the variable. By calling it twice, it doesn't work.
Try:
Peter
Peter's way should work, but I'd do it the other way (leave the 'umbracoFile' select until later):
<xsl:if test="string($currentPage/data[@alias='userManual']) != ''"> <xsl:variable name="file" select="umbraco.library:GetMedia($currentPage/data[@alias='userManual'], 0)"/> <xsl:if test="string($file/data[@alias='umbracoFile']) != ''"> <li> <a href="{$file/data[@alias='umbracoFile']}">Download User Manual</a> </li> </xsl:if> </xsl:if>I wrote a blog post a while back about how to safely handle GetMedia nodes:
http://blog.leekelleher.com/2009/11/30/how-to-use-umbraco-library-getmedia-in-xslt/
Cheers, Lee.
You guys are awesome - both methods work just as I need them to. I'll be sure to read your blog Lee - thanks very much.
One of the things I love about Umbraco is all the help & support. Hopefully I'll be able to offer my own someday!
Thanks,
Rachel
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.