-
- 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?
-
- 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');
Template shouldn’t change before the cache changes, so you could just call that cached right?
-
- 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).
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;}