We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5752
    • 10 Posts
    Don’t know if this is the best way or not, but I’m thinking of declare the current template name as an id of the body tag to let JavaScript know which template is loaded and execute specific code related to that template.

    Well, the template id is present in the resource object, but how do I get the template name?

      • 33968
      • 863 Posts
      I think a better way would be to put the template-related JS in a chunk, and only include that chunk in the template where you want it to appear.

      I guess you could just hard-code the id into the template too if you wanted to continue that way.

      And... if you still want to get the template name you’ll need to write a small snippet and use something like this:
      $modx->getObject('modTemplate',$templateName);
      
        • 3749
        • 24,544 Posts
        This should do it, using your original plan:

        <body id="[[!TemplateName]]">



        <?php
        /* TemplateName snippet */
        
        $templateObj = $modx->resource->getOne('Template');
        
        return $templateObj->get('templatename');
          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
        • Mark Hamstra Reply #4, 13 years ago
          Template shouldn’t change before the cache changes, so you could just call that cached right?
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 3749
            • 24,544 Posts
            Quote from: Mark at Apr 27, 2011, 09:45 PM

            Template shouldn’t change before the cache changes, so you could just call that cached right?

            Good point. This should work fine:

            <body id="[[TemplateName]]">


            I just finished driving from New Orleans to MN, mostly in the rain, so I’m a little punchy right now. I was actually thinking that the tag might be used for different templates (without realizing that it’s *in* the template). tongue
              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
              • 37914
              • 89 Posts
              Thank you BobRay!
              I needed this to apply a bodyclass and was looking into solving this problem for an hour already.

              BTW: I ordered your book "MODX: The Official Guide" and am looking forward to receiving it smiley
              • I don't understand the need for all of this. Why not just hardcode the template name as the ID? It's not like the template's name is dynamic, and will change from page to page. That's what snippets and TVs are for.

                I tend to work from the other end, using [[*alias]] for the ID, so that I can style pages differently as necessary. For example, if I want a page with a big form to have a single column, I can do this:
                #formpage #leftside{display:none;}
                #formpage #rightside{display:none;}
                #formpage #middle{width:80%;margin:auto;}
                  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
                  • 37914
                  • 89 Posts
                  Quote from: sottwell at Jan 20, 2013, 08:20 PM
                  I don't understand the need for all of this. Why not just hardcode the template name as the ID? It's not like the template's name is dynamic, and will change from page to page. That's what snippets and TVs are for.

                  I tend to work from the other end, using [[*alias]] for the ID, so that I can style pages differently as necessary. For example, if I want a page with a big form to have a single column, I can do this:
                  #formpage #leftside{display:none;}
                  #formpage #rightside{display:none;}
                  #formpage #middle{width:80%;margin:auto;}

                  Because I use a header chunk that I call from every template.

                  Header chunk:
                  <!DOCTYPE html>
                  <html lang="en">
                  <head>
                  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                  <title>pagetitle</title>
                  </head>
                  <body id="homepage">
                  <div id="header">header contents<div>

                  Content chunk:
                  <div id="content">content contents<div>

                  Footer chunk:
                  <div id="footer">footer contents<div>
                  </body>
                  </html>