We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11924
    • 2 Posts
    Hi, I was looking for a way to make the children of a containing page use a particular default template (ie not use the parent’s template), and I found this:

    http://modx.com/extras/package/inheritselectedtemplatefixed

    which looks like what I’m looking for. So, I was wondering whether either:

    1. it’s safe to use this plugin in Revolution (and if so, how do I go about doing that?)

    or

    2. there’s a similar extension for Revolution, or a way to do this without needing to get an extension.

    Thanks in advance to anyone who can give me a quick pointer here :~}

      • 33337
      • 3,975 Posts
      Zaigham (aka zi) Reply #2, 13 years ago
      It can’t be used with Revo unless you modify it to make it Revo compatible.

      I don’t think there is something available for Revo like this, but I could be wrong. Lets see if someone chimes in with something.
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 3749
        • 24,544 Posts
        I just created this plugin for Revo and it's untested. Unlike the Evo plugin, it sets the template when a new resource is saved (based on any ancestor's setting for a TV called ChildTemplate). That will make for faster page loads than setting the template every time the resource is viewed.

        It's not necessary, but if you use an @INHERIT for the TV's default value, it should speed up processing for resources further down in the tree.

        <?php
        //remove this line before pasting
        /**
         * ChildTemplate plugin
         *
         * Sets a new doc's template based on the ChildTemplate TV setting of the nearest
         * ancestor. Set the default value of the ChildTemplate TV to @INHERIT
         *
         * Author: Bob Ray
         *
         * Connect to OnDocFormSave (*not* OnBeforeDocFormSave)
         *
         * looks up the tree for an ancestor that has a ChildTemplate TV setting and,
         * if found, sets it as the template. Uses the nearest ancestor with a setting
         * for that TV.
         *
         * Note: can be used with InheritParentTemplate as long as
         * InheritParentTemplate executes first (set the plugin priority on the System
         * Events tab of the plugin.).
         *
         * The TV setting, (if any) will override the InheritParentTemplate setting.
         * The ChildTemplate TV can contain the name or id of the template, but using
         * the ID will be faster.
         *
         *
         */
        
        
        /* Only act on new documents -- remove this line if you want to be
        * able to change the TV value and have the template updated when
        * you edit and save existing resources */
        if ($mode != modSystemEvent::MODE_NEW) return;
        
        /* Get parent ID */
        $parentId = $resource->get('parent');
        
        /* Get the TV object */
        $tvObj = $modx->getObject('modTemplateVar', array('name' => 'ChildTemplate'));
        
        /* Act only if there is a parent ID (i.e., doc is not at the root) and the TV object exists */
        
        if (tvObj) {
         
            /* Get value of TV for parent */
                if ($parentId) {
        
                    /* Get the TV's value for that ancestor */
                    $tv = $tvObj->getValue($parentId);
        
                    if ($tv) {
                        /* $tv is set -- use it for current resource's template value */
                        if (is_numeric($tv)) {
                            /* It's a number -- user it */
                            $resource->set('template', $tv);
                        } else {
                            /* It's a template name -- get the ID */
                            $t = $modx->getObject('modTemplate', array('templateName' => $tv));
                            if ($t) {
                                $resource->set('template', $t->get('id'));
                            }
                        }
                        $resource->save();
                     }
        
                }
        }
        
        
        [ed. note: BobRay last edited this post 12 years, 4 months ago.]
          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
          • 9550
          • 123 Posts
          BobRay: just tried to use this and MODx now hangs on saving a document resource. Nothing in the error logs. MODx 2.0.8-pl.

          Does the ChildTemplate TV have to be assigned to all templates? I only assigned the TV to the parent and child templates for the container and sub-pages where I tested it.
          • Honestly the behavior of fresh installs of Evo 1.0.5 is ideal: it inherits the same template as any sibling docs it finds at any level. You’d need to craft a plugin to do the other bits.
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 9550
              • 123 Posts
              rthrash: but revolution seems to inherit the parent template when creating a new resource, or use the default template if creating off a root context...
                • 3749
                • 24,544 Posts
                I made a change to the code. See if it works now (be sure not to paste the <?php line).
                  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
                  • 9550
                  • 123 Posts
                  no longer freezes on saving, but doesn’t seem to work.

                  got a TV called ChildTemplate attached to two templates with ID’s 12 & 14.
                  saved the value "14" to a container using template (12).
                  on creating a new child page under said container the parent template (12) is still selected.
                  on saving the page, parent template (12) still selected.

                  also tried using template name as ChildTemplate TV value. same result.
                    • 3749
                    • 24,544 Posts
                    What system event did you connect the plugin to?
                      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
                      • 9550
                      • 123 Posts
                      just "OnDocFormSave" (group = resources, property set = 0, priority = 0)