We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35176
    • 193 Posts
    Hi, how does one create sub-templates?

    For example, my Parent template contains my header, menu, sidebar, and footer.

    Now I want a template for my Tour pages, which would go into the content region of the Parent template. Like the parent template, it would contain template variables, which would be filled in by the resource pages.

    I have 2 or 3 different kinds of pages: Tours, Gallery, Assets. Each kind of page would have common layout (all Tour pages would have identical layout, all Gallery pages would share an identical layout which is different from the Tour pages).

    But all kinds (Tours, Gallery, Assets) would share the Parent template.

    I notice the Tags help page does not list a tag for embedding a Template. I’d be pretty shocked if this is not part of ModX! But there must be a workaround (chunks? can a chunk have variables? Or, can i nest a template inside another template?)

      jealousy<>love
      • 32316
      • 387 Posts
      It all comes down to semantics

      When you talk about "a template"
      Now I want a template for my Tour pages, which would go into the content region of the Parent template.
      you are not talking about the same thing as a "modx template"

      I notice the Tags help page does not list a tag for embedding a Template. I’d be pretty shocked if this is not part of ModX! But there must be a workaround (chunks? can a chunk have variables? Or, can i nest a template inside another template?)
      A MODX template is a html code, normal complete html code for a page with the addition of special tags for inserting the content for different pages - if you "embedded" one inside the other you would end up with multiple html, body and head tags and possibly headers all of which would make the page invalid html. Chunks are the way you include extra html into the template - this is not a workaround, it is the way it works.

      So for example you could write a chunk with all your footer code in it and then include it (with the appropriate tag) into one or more templates. Need to change the footer - edit the chunk and it will change everywhere it is used.

      Oh and ...
      (chunks? can a chunk have variables?
      what do you mean by a variable?
      Or, can i nest a template inside another template?)
      I think this is possible - but the one nested inside should not have doc type, head, body tags - in other words an empty template - in your example though I would be using chunks - empty templates are useful for other things - a resource has a template, and if for example one is going to use it with ajax it does not need to contain all the other stuff
        • 35176
        • 193 Posts
        as i see it, a subtemplate could be picked from the Document editor, same as the template is picked in the Document editor. Only then would it be active.

        further, it would only display it’s TV’s in the Document editor when it’s active. for example, say i have a StandardPage template, a Tour sub-template with it’s own TVs, and a Gallery sub-template with it’s own TV’s. Then in the Document editor, the manager picks the StandardPage template and the Tour subtemplate. Then, the Document editor should only display the TVs for the Tour subtemplate.

        a robust sub-templating system would allow multiple levels-- templates within templates within templates. Again, the Document editor would allow manager to pick which subtemplates and sub-subtemplates to use.

        a robust subtemplating system would be able to specify to which subtemplates Templates have access. So that, in the Document editor, when the manager picks a given Template, she would then only be able to pick the subtemplates it has access to.

        i’d be surprised if this is not even on the roadmap for modx. It seems directly in-line with the modx philosophy.

        you say it’s possible to nest a template inside another template. how?
          jealousy<>love
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          The template system is very simple. What you are describing becomes a complex system of templates and subtemplates, that is not how MODx works. TVs can be created to allow selection of chunk via checkboxes or radio buttons, and these chunks contain the desired HTML and further MODx tags for generating the content. There are mechanisms in place for hiding TVs under certain circumstances. Pages that use significantly different structures can be assigned different templates, with content (such as menu structures, logos and footers) can be in chunks used by all templates.

          Another way to control different structure is to create a template with every possible structure, use a TV to specify a class or id for the <body> tag, and use CSS to show or hide and position the various HTML container elements. You can use a TV in the template’s head to specify which .css file to use; again a radio button type TV would be useful here. The @INHERIT TV binding is good for the default value, then you can have whole sections of the site using a given template or .css file and you only need to edit the TV for the parent resource.
            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
            • 35176
            • 193 Posts
            Quote from: sottwell at Aug 19, 2011, 11:42 PM
            system of templates and subtemplates, that is not how MODx works.
            And you don’t think that would be 100% consistent with everything else modx does?
              jealousy<>love
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Chunks and TVs fill your requirements if they are utilized with a bit of imagination. Templates are too basic to the generation of the page to be fragmented.
                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
                • 35176
                • 193 Posts
                Not just imagination, but some relatively advanced modx gymnastics to really achieve all that I describe. The muliple levels seems particularly challenging.

                But you didn't reply to my question about it being consistent with MODx philosophy.

                Fragmented how? I don't see that. My parent template would be a complete page, with all the doc declarations and head section the full page needs, plus a subtemplate tag in the body, similar to a content tag. [ed. note: johnywhy last edited this post 15 years, 1 month ago.]
                  jealousy<>love
                  • 33968
                  • 863 Posts

                  My parent template would be a complete page, with all the doc declarations and head section the full page needs, plus a subtemplate tag in the body, similar to a content tag.
                  I think your understanding of chunks and templates is back-to-front. Rather than having a ’parent template’ with standard doc declarations and head section, etc, you put those in a [[$header]] chunk instead and then ’include’ them in your templates.


                  i have a StandardPage template, a Tour sub-template with it’s own TVs, and a Gallery sub-template with it’s own TV’s. Then in the Document editor, the manager picks the StandardPage template and the Tour subtemplate. Then, the Document editor should only display the TVs for the Tour subtemplate.
                  To accomplish this, why not just create three templates? One for StandardPage (with it’s own TVs), one for Gallery (with it’s own TVs) and one for Tour... with it’s own TVs. All three could contain completely different markup, snippets and chunks, but would include a standard header and footer by using chunks.

                  Also, and I think this is what you meant by ’chunk variables’, it’s possible to include different chunks within a template depending on a resource’s TV value. For example you could have a Dropdown List TV to allow you to choose between different sidebars. In your Input Options you would have something like this:
                  Sidebar One==[[$sidebar1]]||Sidebar Two==[[$sidebar2]]||Sidebar Three==[[$sidebar3]]
                  
                    • 35176
                    • 193 Posts
                    actually, no.

                    your solution still requires me to repeat the skeletal structure of the page, and the Header/Footer tags, 3 times. If every page has a header and footer and sidebar, sure i can put the header,footer, and sidebar html meat into chunks, but i still have to repeat the page skeleton template in which their tags are embedded, right?

                    For example:
                    [[$DeclarationsAndHead]]
                    <table>
                    <tr>[[$MyHeader]]</tr>
                    <tr>[[$SubtemplateChunk1]]</tr>
                    <tr>[[$MyFooter]]</tr>
                    </table>
                    </body>
                    </html>
                    

                    [[$DeclarationsAndHead]]
                    <table>
                    <tr>[[$MyHeader]]</tr>
                    <tr>[[$SubtemplateChunk2]]</tr>
                    <tr>[[$MyFooter]]</tr>
                    </table>
                    </body>
                    </html>
                    

                    [[$DeclarationsAndHead]]
                    <table>
                    <tr>[[$MyHeader]]</tr>
                    <tr>[[$SubtemplateChunk3]]</tr>
                    <tr>[[$MyFooter]]</tr>
                    </table>
                    </body>
                    </html>
                    


                    So your solution still requires me to repeat that skeleton of the page in each template. That does nothing to achieve real subtemplating. And Isn't that redundancy contrary to the whole point of MODx?

                    also, the chunks will have TVs. the entry-widgets for those TVs should only be visible when the chunk is activated.

                    i found that i can put TVs in a chunk, if the chunk is embedded in the template, and the Template has access to those TVs.

                    but if the chunk is not active, then i don't want it's TV widgets visible in the Document editor. so, is there a way to conditionally display TV widgets in the Document editor?

                    thanks! [ed. note: johnywhy last edited this post 15 years, 1 month ago.]
                      jealousy<>love
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      With Evo's ManagerManger you can use PHP conditionals to hide TVs (and almost all other resource editing fields); the basic hideField function has parameters for hiding based on template and on user role.

                      if($id == 1) {
                          hideField('tvName');
                      }
                        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