We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19787
    • 28 Posts
    Is it possible to use alternative templates (views) on the same content?

    For example i have a number of different sections - Football, Cricket, Tennis etc.

    Each section uses a template for each different view - main view (latest news), popular stories view, videos view

    My aim is to avoid creating lots of different documents and also to call these views via the url in a nice clean readable way like :

    football/news
    football/popular
    football/videos

    cricket/news
    cricket/popular
    cricket/videos


    Is there a way to call a different template in each section via the url? eg. /football?template=alternativeTemplate

    Or
    is there better way to do this without creating seperate documents for each section?

    Or
    would it be better to do it all in a snippet which looks for a variable in the URL and switches the view/snippet?

    Any tips or pointers to examples would be helpful

    Thanks,
    Michael
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Michael-

      I think you’re getting to the root of the whole "template model" problem... don’t know exactly what it’s called (and it’s difficult to talk about), but essentially the problem can be solved by having the templates PULL data into them or have the content PUSH data to the templates. MODx uses the push model where the page data pushes itself to the chosen template. I know Expression Engine uses a pull approach (they don’t call it that), where you create a template and then you put logic in that template which pulls data from the content (which lives in a weblog somewhere). Personally, I find MODx’s push method to make more sense, but after getting vehemently flamed in the Expression Engine forums, I realized that this may just be a matter of personal taste.

      The solution that requires the least amount of work is to adapt your site (and probably your thinking) to the "push" model... but I know how much that just chaiffed my brain when I had to cross-over.

      Maybe you can clarify: are you using the EXACT SAME content on more than 1 page? Based off or your URLs, I’d say that you aren’t... you’d have cricket content on the cricket page, football stuff on the football page, etc.

      Here’s one solution that may not actually work for you, but hear me out. Let’s say your football template has image of a football, the tennis template has a tennis ball, etc. Why not just create a single generic template and then add a template variable where the image path can be stored. In your template you’d have something like
      <img src="[*specialized_ball_image*]" />

      and then on your page, you’d specify the path to the image. You can even utilize the inherit properties so that all football pages inherit the football image used by their parent (as one example).

      You can take this concept even further by creating custom chunks for each sport, and then use a template variable to reference those chunks.

      Have you worked with template variables before? Does this solution make sense to you, or is it completely off the mark?


        • 28042 ☆ A M B ☆
        • 24,524 Posts
        I’m not sure I understand your "push" and "pull" thinking. MODx first determines which document has been requested, then gets the template that document is using, then parses the template and processes any MODx tags in that template. So it seems to me that the template "pulls" any content that it requires to have the requested document generated. You can even have a template with no MODx tags at all if you really want to for some reason.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 29774
          • 386 Posts

          Is there a way to call a different template in each section via the url? eg. /football?template=alternativeTemplate

          This is simple - you can use a snippet to determine the requested view (by registering the value of $_GET[’template’] and using switch/case logic for example) and return the appropriate template (chunk) populated with whatever data you like.
            Snippets: GoogleMap | FileDetails | Related Plugin: SSL
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            I'm not sure I understand your "push" and "pull" thinking. 


            LOL. Yeah... the more people I tell about it the more funny looks I get. I gotta come up with a better analogy, but the way that MODx handles it makes perfect sense to me, whereas the Expression Engine way just struck me as backwards.
              • 19787
              • 28 Posts
              Thanks to everyone for trying to help me.

              @Everet I understand your analogy of push and pull. It’s a difficult concept to get your head around, but its a little clearer now.

              Here is a better example of what i am trying to accomplish

              Each section has a number of documents underneath

              Football
              - Doc 1
              - Doc 2
              - Doc 3

              Cricket
              - Doc 1
              - Doc 2
              - Doc 3

              The default view when you visit Football is a to list all the child documents
              football/

              I then have another template which lists the most popular pages
              football/popular

              I then might have another template which provides an RSS of latest documents
              football/rss

              I thought there might have been a function to easily allow alternative templates on the same content This would have allowed me to apply the rss and popular templates to the cricket and tennis sections, without creating another document.

              However, as suggested by @therebechips I will go with a snippet that looks for a value in the URL and presents the correct template using some switch / case logic. I will probably then use an Apache RewriteRule to get my clean URL. Something like this :

              url = football/?template=rss

              snippet ->
              $template=$_GET[’template’]

              switch ($template) {
              case "rss":
              {{rss-snippet}}
              break;
              case "popular":
              {{popular-snippet}}
              break;
              default:
              {{default-snippet}}
              break;
              }




                • 29774
                • 386 Posts

                However, as suggested by @therebechips I will go with a snippet that looks for a value in the URL and presents the correct template using some switch / case logic. I will probably then use an Apache RewriteRule to get my clean URL.

                Interesting. I’ve often considered implementing some kind of URI routing scheme for dynamic views using Apache Rewrite; something like Codignitor’s /class/function/id/

                Maybe /template/snippet/id/

                Anyone done this?
                  Snippets: GoogleMap | FileDetails | Related Plugin: SSL
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: therebechips at May 27, 2009, 11:00 AM

                  Interesting. I’ve often considered implementing some kind of URI routing scheme for dynamic views using Apache Rewrite; something like Codignitor’s /class/function/id/

                  Maybe /template/snippet/id/

                  Anyone done this?
                  You can do this by simply providing a custom function to generate or rewrite the links in the content you produce, as well as providing either a) rewrite rules to route your URL/GET parameters to that particluar view, or b) a plugin OnPageNotFound to do the same with PHP.
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    Do you really need a separate TEMPLATE to display the popular pages or the rss feed? Couldn’t you just put this stuff in a new page using the same template as the other pages? Well, the RSS feed would use a blank template, but the popular pages are easily listed using a Snippet. Is there a need for the whole page to look differently?
                      • 29774
                      • 386 Posts

                      You can do this by simply providing a custom function to generate or rewrite the links in the content you produce, as well as providing either a) rewrite rules to route your URL/GET parameters to that particluar view, or b) a plugin OnPageNotFound to do the same with PHP.

                      Cool, I like the later approach since it gives you a place to attach a front controller to delegate requests. I’m thinking along the lines of parsing the uri so that the document alias is the name for an action controller (a snippet, since we want access to the $modx object) and the document’s template becomes the view; eg:

                      /blog/showpost/post_title/

                      ...where blog is the alias of a modx document AND the name of the snippet which defines a controller class, showpost maps to a method of this class, and post_title is the argument passed the method to retrieve the requested data.

                      For flexibility the controller could pass data to the document template (view) by adding output to a custom placeholder array (modx placeholders get cached which we don’t want in a dynamic view). Custom placeholders (eg (+my_placeholder+) ) would be used within the document template and we’d need to use another plugin to parse the $modx->documentOutput on OnWebPagePrerender to replace them with the matching key value in the custom placeholder array.

                      So the end result would hopefully be a system that implements a simple front controller pattern to provide friendly urls for dynamic views on data, with the flexibility to be incorporated anywhere within an existing modx document tree , and the ability to use modx templates and chunks by injecting output into placeholders.
                        Snippets: GoogleMap | FileDetails | Related Plugin: SSL