We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36926
    • 701 Posts
    Is it possible to use different templates for each resources. The template would be defined either in the resources template field or a TV.

    I'm guessing output filters are the only way, but wondering if there's any alternative solutions.

    Thanks

    Ben
      • 3749
      • 24,544 Posts
      It's definitely possible, but it's hard to imagine a case where it would be a good idea. wink It kind of defeats the whole idea of templates, which are meant to hold reusable content.

      If you can give some more information about what you're trying to do, there might be a better way.



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36926
        • 701 Posts
        Hey Bob,

        Thanks for the reply.

        I need a page thats going to list a number of posts/articles that will be contained within a parent resource. There will be a choice of 3 different layouts for the posts, there's no logic (as in odd/even or x number) to what template will be used. The template will be down to the user to specify either via the template field itself or a TV.

        So on the page that renders out the posts you could have a random order of templates like:

        post 1 (using template c)
        post 2 (using template a)
        post 3 (using template a)
        post 4 (using template b)
        post 5 (using template a)
        post 6 (using template c)
        and so on

        Hopefully that makes sense

        Only way i can think is to use if output filters in the tpl chunk for getresource.

        Thanks

        Ben
          • 3749
          • 24,544 Posts
          Ben, first, let's make sure we're using the word 'template' in the same way. Each Resource has only one template, which is stored in the DB in the Resource's 'template' field. So, the only things in MODX that you call templates are in the Template section of the Elements tree in the Manager and only one template will be involved in the display of your page -- the template of that page (not the templates of the posts you're aggregating)..

          It sounds like you're talking about Tpl chunks, or something similar, if every resource rendered by, say, getResources might have a different format.

          I think I understand what you want for output, but how are the Tpl preferences for each resource set? Can different users have different formats for the same resource?

          Using getResources might be possible, but how will you know the Tpl chunk to use for each resource *before* getResources retrieves them?


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 36926
            • 701 Posts
            Hi Bob,

            Yeah sorry for the confusion there. It's the Tpl chunks i'm referring to. So similar to how getResource can use a different Tpl for odd and even resources.

            The resource will only have on Tpl so once set it will render out that look. There will only be 3 or 4 different Tpl chunks that can be used. I thought the best to set what Tpl chunk to use by having a drop down TV with the possible template options.

            Thanks

            Ben
            • Some phx conditionals should do the trick, but it will get really ugly if you are going to have a lot of options.
                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
                • 36926
                • 701 Posts
                Hey Susan,

                Do you mean some output/input filters for revo as phx is only Evo right?

                There's actually only going to be 3/4 templates so not to many, but there could end up being a number of posts/articles so was wondering it that was going to be the most efficient way.

                Thanks

                Ben
                • In the latest release of getResources (1.5.0-pl) there is a new feature for conditional tpls:

                  [[getResources?
                    &tplCondition=`template`
                    &conditionalTpls=`{"1":"tplA","2":"tplB","3":"tplC"}`
                    &tpl=`defaultTpl`
                  ]]


                  Just define the resource field for the tplCondition and a JSON object as conditionalTpls with keys representing the condition value which determines the tpl to use. [ed. note: opengeek last edited this post 11 years, 11 months ago.]
                    • 36926
                    • 701 Posts
                    Thanks opengeek this is great and works perfectly.

                    Ben
                      • 3749
                      • 24,544 Posts
                      What a great feature!


                      ------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using.
                      MODX info for everyone: http://bobsguides.com/modx.html
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting