5 users online. Create an account or sign in to join them.Users

Search

I’m trying to get my head around Form Controls, as I’ve been avoiding it for awhile.

So far seems pretty easy and lots of benefits. But I’m having trouble with a Select Box Link. Can I achieve a Select box that has options with a different “value” and “label”? I’ll show what I mean below.

I’m trying to achieve some HTML like this:

<select name="fields[categories]" id="fields-categories" title="" class="">
    <option value="2">Announcement</option>
    <option value="1">Symphony</option>
</select>

And here’s the XSL:

<xsl:call-template name="form:select">
    <xsl:with-param name="handle" select="'categories'"/>
    <xsl:with-param name="options" select="/data/categories/entry/title"/>
</xsl:call-template>

And here’s the XML for the categories I’m trying to list (basically a default Select Box data source)

<categories>
    <section id="2" handle="categories">Categories</section>
    <entry id="2">
        <title handle="announcement">Announcement</title>
    </entry>
    <entry id="1">
        <title handle="symphony">Symphony</title>
    </entry>
</categories>

Thanks for your help.

Hi Jesse, great to see you getting stuck in!

You’re right, Form Controls doesn’t handle this situation perfectly. With options like this, as you have:

<xsl:with-param name="options" select="/data/categories/entry/title"/>

You’ll get a set of options with the title as the label and the handle as the value. This is because Form Controls looks for a set of known attributes to use. But because you want the entry ID, you’ll need a different approach. There are two ways:

One is to use Section Schemas. It will give you the <option> elements as you see the Select Box Link in the backend.

The second, seeing as you’ve already got a data source providing the related entries, is to manually create the options yourself. The $options parameter accepts either an XPath expression (as above) or XML itself:

<xsl:with-param name="options">
    <xsl:for-each select="/data/categories/entry">
        <option>
            <xsl:attribute name="value">
                <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:value-of select="title"/>
        </option>
    </xsl:for-each>
</xsl:with-param>

Here you’re looping over category entries and building the options yourself. The XML you create doesn’t need to be <option> elements, I’m just showing that for convenience. The element can be named anything you like, <category> for example. If you want the final value attribute to be different to the label, then you need to add an attribute named handle or id, link-id, link-handle or value. So technically the above generated XML could be:

<category id="123">My Category</category>

But that’s by the by.

Hope this helps!

Nice! Thanks a bunch, its good to understand the flexibility that this can give you.

BTW, I think I figured it out, but for anybody else using this as a reference, you might want to correct your XSL to use with-params, not just a param?

Good spot, I’ve updated my post. Sorry if that tripped you up.

Here’s an interesting example from the readme showing that you can combine static XML with a copy-of; similar to what we’ve been discussing.

<xsl:with-param name="options">
    <option value="">Select a country:</option>
    <country>Australia</country>
    <xsl:copy-of select="/data/countries/country"/>
    <country>Zimbabwe</country>
</xsl:with-param>

Yeah, so I was reading through your docs and I must admit that example above confused me a lot. isn’t exactly a valid HTML element, does it get transformed to option elements somehow? It was strange that you were mixing an option together with those cuntry elements. And are you just adding in 2 additional countries that weren’t already a part of the datasource with Australia and Zimbabwe? Sorry for being dense sometimes… I love symphony but I admit that xsl is sometimes black magic to me still! =)

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.2 or above
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.0 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details