Search

Howdy. Sorry for a full on noob question but i’ve been hunting around for ages and can’t seem to find the answer (and it’s dead simple!).

Basically I have set up a section for a navigation menu and each entry has a navigation-title and a link (ie text that displays and the link it links to).

It was working fine until I wanted to add a the class ‘current’ to the navigation link that for the page being viewed.

This is what I have. I’m sure it’s just a syntax error type thing but no matter what I try I can’t get it to work!

    <xsl:template match="nav/entry">
      <xsl:choose>
        <xsl:when test="{link}=$current-url">
        <li class="navi current"><a href="{link}"><xsl:value-of select="navigation-title"/></a></li>
        </xsl:when>
        <xsl:otherwise>
      <li class="navi"><a href="{link}"><xsl:value-of select="navigation-title"/></a></li>
        </xsl:otherwise>
      </xsl:choose>
</xsl:template>

Any help much appreciated and apologies for a very noobish question. I’m only just getting started with XSLT (first site with Symphony).

Thanks,

Tim

Hi Tim,

try

<xsl:template match="nav/entry">
    <li>
        <xsl:attribute name="class">
            <xsl:text>navi</xsl:text>
            <xsl:if test="link = $current-url">
                <xsl:text> current</xsl:text>
            </xsl:if>
        </xsl:attribute>
        <a href="{link}"><xsl:value-of select="navigation-title"/></a>
    </li>
</xsl:template>
  • The curly braces syntax is to interpret XPath inside attribute values of your actual XML/(X)HTML output elements.
  • You can dynamically set attribute values, so there is no need for repeating markup.

There are some nice XSLT utilities in the Downloads section to harvest and learn from. And the default workspace (included in the download package) is a good resource as well.

I think your code will work if you just take the curly braces off “link” in your when statement. klaftertief’s code is more exotic but it takes one more line than does yours.

Thanks guys.

It’s great to see another way of doing things and seeing that you can dynamically set attributes will be really handy! Will try that out too.

Just a question, if I wanted to add in a second ‘OR’ type condition to the test (something like ‘OR $root=$current-url’) can you chain tests together like in JQuery or do I need to open another ‘When’ to add another test at the same point?

Also this has finally got into my head when and when not to use curly braces. I was blindly going with trial and error but now I get that when it’s inside an xsl block you don’t. When your outside that (in XHTML/XML) you use the curly braces to get it evaluated. Can’t believe I didn’t get it till now!

So helpful thanks again!

You’re welcome!

There are standard operators you can use in XPath expressions and or is one of them. So yes, you can just add or $root = $current-url to your expression. And you can use some predefined (depending on your processor) functions as well.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details