0

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.

user2992596
  • 181
  • 2
  • 12

1 Answers1

2

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.

Sarah Kemp
  • 2,670
  • 3
  • 21
  • 29
  • Ah, I think I was going wrong by not having two data sources, I assumed I could grab just 1 video from the source with all of them, but everything makes a lot more sense your way. I'm just travelling on the train so I will update when I get home and try this out :) – user2992596 Nov 27 '13 at 16:58
  • In this simple case I think you could get away with using just one datasource and some XPATH to choose the right video from the list, but my answer is more scaleable and I think explains the way Symphony is structured a little better. Plus this way you can add the Conditionalizer extension down the road to only run the queries for the Data Source you need, rather than pulling video info you don't need then sorting through it in your template. – Sarah Kemp Nov 27 '13 at 17:21
  • But if you check out the XML code you'll see that there are all the entries for both datasources: the single video and a list with ALL the videos, which means your XML file processing will take the same amount of time to load a single video as if it were loading the full list of videos. Am I wrong with this? Any way to solve this issue? – Manuel Ro Aug 12 '15 at 21:50