Question

I have been looking into Symphony quite a bit, and although I am a very slow learner, I have created a few basic websites. One thing I am struggling with is, I want my main page template (home.xsl) to show one template if there is a url parameter, and if the parameter is empty then just show another template.

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output method="xml"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    omit-xml-declaration="yes"
    encoding="UTF-8"
    indent="yes" />

 <xsl:template match="/">
    <html>
        <head>
            <title>Homepage</title>
        </head>
        <body>
            <h2>Videos</h2>
            <ul>
                <xsl:apply-templates select="/data/videos/entry"/>
            </ul>
        </body>
    </html>
 </xsl:template>

 <xsl:template match="videos/entry/single">
    <div class="video"><xsl:value-of select="greeting-text"/></div>
 </xsl:template>

 <xsl:template match="videos/entry">
    <li><xsl:value-of select="greeting-text"/></li>
 </xsl:template>

 </xsl:stylesheet>

For example, in the above code (adapted from the 'Hello World!' Symphony tutorial), there is a template match="videos/entry/single" and template match="videos/entry". I would like the first template to show up if there is a URL parameter defined (e.g. I am loading up website.com/parameter), and it would show the 'parameter' video, and if there is no parameter defined it will show all videos, i.e. the second template.

I have a real problem explaining things, especially when I don't fully know the technology, so excuse any idiocies in my writing, and I would be happy to explain more if necessary.

Was it helpful?

Solution

Have you added a URL parameter to the Page your template is for? Have you added a Data Source for the single video and one for multiple videos? You need to then use the URL parameter on your Page to determine which code block you want to use.

I know it's a lot to get used to but you will get the hang of it with a little experimentation and there is lots of great help available on the forum: http://www.getsymphony.com/discuss/

This tutorial is doing something similar and has lots of explanation as it goes: http://designprojectx.com/tutorials/master-detail-views-in-symphony/

Essentially you need:

  1. A Page with a URL Parameter (say, 'id')
  2. A Data Source called 'videos' which fetches all of your videos for the home page
  3. A Data Source called 'single-video' which fetches one video by its 'id' (would look like {$id} in the Data Source filter box). This will return no results if 'id' is not set.
  4. You need to include both Data Sources in your Page
  5. Then, in your Page Template, you need to check for the URL Parameter to determine which layout you are going to use. This would look something like:

    <xsl:template match="/">
        <html>
            <head>
                <title>Homepage</title>
            </head>
            <body>
                <!-- this is the XSL version of an if/else
                     (basically check if URL Parameter 'id' is nothing,
                     display list, otherwise, display video by the 'id' provided) --> 
                <xsl:when test="not($id)">
                    <h2>Videos</h2>
                    <ul>
                        <xsl:apply-templates select="/data/videos/entry"/>
                    </ul>
                </xsl:when>
                <!-- here is where we tell it which template to use if 
                     we do have a video id in the URL. We reference the second
                     datasource (single-video) in this case -->
                <xsl:otherwise>
                    <xsl:apply-templates select="/data/single-video/entry"/>
                </xsl:otherwise>
            </body>
        </html>
     </xsl:template>
    
     <!-- single video layout -->
     <xsl:template match="single-video/entry">
         <div class="video"><xsl:value-of select="greeting-text"/></div>
     </xsl:template>
    
     <!-- all videos layout -->
     <xsl:template match="videos/entry">
         <li><xsl:value-of select="greeting-text"/></li>
     </xsl:template>
    

I hope this helps. Symphony takes a little getting used to but it really makes sense once you start seeing how all the pieces fit together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top