Search

Hi I'm new to Symphony (though loving what it can do so far!) so I apologise if my question seems vague!

I'm trying to setup a website for the family garden centre with a 'plant of the month' segment. I have created a section to hold key information, size, type, flowering month etc. I have created a date source to select the last post this all works fine...

Now on to the difficult bit (for me at least!) I would like to display the flowering month as a list of buttons representing months, with the intention on changing the colour of the flowering months. (the next stage would be to link the buttons to a new page that shows other plants flowing in that month)

What i think i need to do is for each button: when parameter 'flowering' = 'month' turn green otherwise stay grey

I've tried various things like this but can't seem to get it to work...

http://pastie.org/5111607

Am I barking up the wrong tree or just getting it wrong? Can anyone point me in the right direction?

Many thanks, Pete

Hi PLay,

If you show us your page's XML, I'm sure we'll be able to help. Append ?debug to the URL when viewing the page when logged into Symphony to get to this.

Thanks DavidOliver,

Pastie link here:

http://pastie.org/5114211

but not sure its copied in right? I've included the xml as an attachment just in case.

Thanks!

Pete

Attachments:
lotl xml.xml

Hi PLay,

I'm not sure, but I think that you should change the test to be something like this:

<xsl:when test="plant-of-the-month/entry/flowers/item/@handle = $ds-plant-of-the-month.flowers/item/@handle">

Thank you David,

I've had a go at entering the code above but I get the following xslt prosessing error:

XSLTProcessor::transformToXml():
    Invalid type
    xmlXPathCompiledEval: 2 objects left on the stack.

I've tried it as above and as below,

<xsl:when test="plant-of-the-month/entry/flowers/item/@handle = $ds-plant-of-the-month.flowers/item/@october">

Am I doing it wrong?

Thanks for your help!

Pete

Hi Pete,

I'm afraid I still don't quite follow what it is that you're trying to achieve, but I'm not nearly as insightful as most people who tread these threads.

My point about the test above was that you want to check whether two things are equal ('flowering' and 'month').

In your XML, your plant-of-the-month/entry/flowers/item/@handle is "october" and the datasource output parameter ds-plant-of-the-month.flowers/item/@handle is also "october", so a test of whether or not they are equal comes out true and your <td> should get the green bgcolor.

I meant for the test to be as I wrote it, i.e. the @handles should remain; if you have a look in your XML, you'll see that elements can have a 'handle' attribute – these are often the same as the content of the element itself, but with any spaces replaced with hyphens, so that e.g., they could be used in a URL. In your case, because you would only be dealing with names of individual months, you wouldn't necessarily have to have the /@handle after the item (because each month would only be one word, no spaces), but I think it's probably good practice to work with them.

As for why you get the XSLT processing error you describe… I've got nothing; you'll need someone who actually knows what they're talking about. Sorry.

D

I haven't done much date-handling in Symphony yet. There is probably a more elegant way to loop through months of the year in XSLT, but you could create a section and data source for Months that contains 12 entries (one for each month) and then do something like (all untested and only intended to give an idea):

XSL:

<xsl:template match="/data/plant-of-the-month/entry">
    <!-- other plant of the month stuff here -->
    <ul class="flowers">
        <xsl:apply-templates select="/data/months/entry"/>
    </ul>
    <!-- other plant of the month stuff here -->
</xsl:template>

<xsl:template match="/data/months/entry">

    <!-- output each month as an li -->
    <li>

        <!-- link month anchor to another page -->
        <a href="...">

            <!-- if month appears in plant of the month's 'flowers' items value...s -->
            <xsl:if test="@handle = /data/plant-of-the-month/entry[1]/flowers/item/@handle">

                <!-- ...add a class to the anchor tag -->
                <xsl:attribute name="class">this-month</xsl:attribute>

            </xsl:if>

            <xsl:value-of select="name"/> <!-- name of month -->

        </a>

    </li>

</xsl:template>

CSS

.this-month {
    background-color: green;
}

Thanks for your help guys, I'm feeling very out of my depth but I'm having a play and trying to get it to work, I'll report back!...

Pete

So... Sorry it's been a while since I said I would report back, suffice to say I've been struggling!

I couldn't get David Mc's solution to work and I still don't understand entirely how it should work, but thank you for your explanation!

DavidOliver your method made a lot of sense to me but I still couldn't get it to work. I setup a new section and data source for months and linked it via a select box link, I included the flowering month as an output parameter and the month list, XML & Params came through fine, I just could not make the if test work at all. I tried many ways of matching entry's to Params, entry's to entry's as ID's and handles.

I did a bit of research on the subject I have found bits of information on loading two XML files, it seems very complicated, and a departure from DavidOlivers advice above, am I getting the wrong end of the stick?

Comparing two xml files?:

http://www.dpawson.co.uk/xsl/sect2/N1777.html

I have however found a way to achieve what I wanted to do, I don't think it's a very elegant solution and there must be away of making DavidOlivers solution work?

With my working code below does it shine any light on what wasn't working before perhaps? Or give anyone any ideas on a better way of achiving the same result? In this version I have used check boxes for each of the months but I think now I have this working I could use a select box link with a when test flowers='jan' instead of when test flowers-jan='Yes'? So that will neaten it up a little...

XML

<plant-of-the-month>
    <section id="20" handle="plant-of-the-month">plant of the month</section>
    <entry id="99">
        <genus handle="fgfg">fgfg</genus>
        <common-name handle="ggfdgb">ggfdgb</common-name>
        <latin-name handle="dghgh">dghgh</latin-name>
        <flowers-jan>Yes</flowers-jan>
        <flowers-feb>Yes</flowers-feb>
        <flowers-mar>No</flowers-mar>
        <flowers-apr>No</flowers-apr>
        ...
    </entry>
</plant-of-the-month>

XSLT

<xsl:template match="data/plant-of-the-month/entry">
    <h3>Plant of the Month</h3>
    <!-- flowers -->
    <!--jan-->
    <xsl:choose>
        <xsl:when test="flowers-jan='Yes'">
            <a class="btn btn-success" href="{$root}/plants/jan/">  Jan  </a>
        </xsl:when>
        <xsl:otherwise><a class="btn" href="{$root}/plants/jan/"> Jan </a></xsl:otherwise>
    </xsl:choose>
    <!--feb-->
    <xsl:choose>  
        <xsl:when test="flowers-feb='Yes'"><a class="btn btn-success" href="{$root}/plants/feb/"> Feb </a>
        </xsl:when>
        <xsl:otherwise><a class="btn" href="{$root}/plants/feb/"> Feb </a></xsl:otherwise>
    </xsl:choose>
</xsl:template>

Thanks!

Pete

Apologies for copying my code into the comment and making a mess please find a link to XPathr: http://xpathr.com/view/4024791/

Pete

@Pete: I've reformatted your code blocks so it's a little easier to follow for others.

I haven't been following the thread, but while I was cleaning up your code, I thought I should offer a slightly different way to achieve what you're looking for:

<xsl:template match="data/plant-of-the-month/entry">
    <h3>Plant of the Month</h3>
    <!-- flowers -->
    <!--jan-->
    <a class="btn" href="{$root}/plants/jan/">
      <xsl:if test="flowers-jan='Yes'">
        <xsl:attribute name="class">btn btn-success</xsl:attribute>
      </xsl:if>
      <xsl:text>Jan</xsl:text>
    </a>
    <!--feb-->
    <a class="btn" href="{$root}/plants/feb/">
      <xsl:if test="flowers-feb='Yes'">
        <xsl:attribute name="class">btn btn-success</xsl:attribute>
      </xsl:if>
      <xsl:text>Feb</xsl:text>        
    </a>
</xsl:template>

Essentially the logic reads, "if flowers-{month} equals "yes", then change the @class attribute to btn btn-success.

Now this is only changing your code slightly to achieve the same result. There is a cooler way of doing this that involves wildcard element matching. When I get more time, I may show you how that can be done. Unless someone beats me to the punch.

Well, I've gone ahead and done a more fanciful version for you to see. Bear in mind that the code can be abstracted further by removing the for-each loop and replace it with template match. But for the sake of illustrating what the code block does, I think it's better to show it in a for-each loop:

http://xpathr.com/view/4031078/

The code essentially eliminates having to write out each month by hand. Instead it iterates over each element that has the format of <flower-{month}>.

Note that for the sake of completeness, I added bits of logic in there that doesn't directly contribute to the point my code is making. For example, I added a translation method to capitalise the month for output. I also added a root template match block just so that XPathr would transform the result properly. Normally you would have your own root template match block, most likely in your master.xsl file. I hope those additional lines of code doesn't detract from the point at hand.

@Pete if you are unclear about any of the parts of the code, let me know. Typically I am hesitant on showing weird and advanced coding methods to people new to XSLT. I hope my example is easy enough to follow and understand.

@Allen that's amazing, you really are my hero! I've not tried to implement any of this yet but the first method seems fairly simple to understand, and with a little bit of w3schools help I think I know what's going on in the second. Starting to really enjoy working with symphony now it's all starting to come together in my head! Thank you for your help on this one it's very much appreciated, and I hope I didn't cause too many problems with my code above! Pete

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