We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49300
    • 19 Posts
    Hi

    I'm coming from a WordPress background and on WordPress, you can put the class in the body to indicate that you are in the home page, a single page, a post page, an archive page, etc...

    <body <?php body_class( $class ); ?>>


    Is there a similar way in Modx to do this? This is to style the pages and posts.

    thanks!

    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      I think the closest thing to that would be to create a Template Variable (TV) and put the "class" name (or whatever you want) in the TV value for each page. If you name the TV 'Class', you could put this tag in the template or text where you want the class name name to appear:

      [[*Class]]


      You can also do this to control the style of the page:

      <div id="[[*Class]]">
      
      </div>

        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
        • 49300
        • 19 Posts
        Hi BobRay

        I want to do something like that but my header is separate. So I can't just put different class names on header (if that's what you mean).

        What I can do though is when I call the header, for example in my single news page, I'd do something like this:

        [[$header var=class="single-news"]]

        In the header chunk I'd say:

        <body class="[[$var]]">

        But I'm sure this is not the correct syntax. Would you know? I'm sorry, I'm really just starting with Modx.

          • 49300
          • 19 Posts
          Quote from: BobRay at Nov 20, 2014, 04:25 PM
          I think the closest thing to that would be to create a Template Variable (TV) and put the "class" name (or whatever you want) in the TV value for each page. If you name the TV 'Class', you could put this tag in the template or text where you want the class name name to appear:

          [[*Class]]


          You can also do this to control the style of the page:

          <div id="[[*Class]]">
          
          </div>


          Hey Bobray, I think I now get what you mean here. Let me try it.
            • 49300
            • 19 Posts
            Ok the problem with adding a TV is that you need to update all of your pages to set the TV. I already have a lot of pages, news items, publications and need to go through all that to set the tv.

            How if I just output the template that's been used in the page? Like for example the home page uses home, the news item uses news-single.

            If I can just output that in the body of the page, then that will be much easier because all of the pages use a template anyway. For example:

            <body class="[[$template_use]]">
            [ed. note: vanduzled last edited this post 9 years, 5 months ago.]
              • 3749
              • 24,544 Posts
              Unfortunately, the template used is only available as a number if you use a tag. You can cheat, though and name chunks with the template ID in the name (in parentheses next to the template in the tree) and put what you want to display inside the chunk. IOW, the chunks would be named template-1, template-3, template-5, etc. and you'd use this tag to set the class:

              <body class="[[$template-[[*template]]]]">


              The inner tag will resolve to the Template's ID, then the outer tag will be replaced with the contents of the specified chunk.
                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
                • 9995
                • 1,613 Posts
                I sometimes do it by using;
                <body class="page-[*id*] [*parent:is=`4`:then=`news`:else=``*] [*id:is=`4`:then=`newsoverview`:else=``*]">

                (This is for evolution but can be done by revolution aswell with different calls)

                If template can be used that would be better if you use different templates.
                (I always try to reduce the ammount of templates.)
                  Evolution user, I like the back-end speed and simplicity smiley
                  • 24629
                  • 370 Posts
                  I think Bobray answered this one slightly better 3 years ago .
                  see http://forums.modx.com/thread/31514/how-to-get-the-template-name

                  RDG
                  • discuss.answer
                    • 3749
                    • 24,544 Posts
                    Thanks for the kind words. I actually have an even better answer now: smiley


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


                    /* TemplateName snippet */
                    $query = $modx->newQuery('modTemplate', array(
                        'id' => $modx->resource->get('template'),
                    ));
                    $query->select('templatename');
                    return $modx->getValue($query->prepare());


                    It's faster and uses less memory.


                      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
                      • 49300
                      • 19 Posts
                      That one works like a charm Thanks BobRay!