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


    Official Support/Comments for automaticTpl
    http://modxcms.com/forums/index.php?topic=28071


    Here is a plugin that allows the administrators to specify what template a document should use. I would use it when you do not want the client to touch the templates and you have 1 or more levels in one of your containers.

    Eg:

    here is basic structure

    / (root)
    programs
    myprogram1
    mysubprogram11
    myprogram2
    mysubprogram2
    Note: Each level has a diffrent template


    NEW: Version 0.3 allows any kind of structure for you containers.

    Lets say you want to automate the assignment of the templates for the myprogram and mysubprogram levels

    I solved this by creating a TV for the programs template called automaticTpl that works with the automaticTpl plugin. You need to specify the id of the TV to the plugin.

    Inside the tv you would specify the template id the manager has to set automatically Eg: 5,7. This means myprogram level will use template 5 and mysubprogram template 7

    more information in the code:

    
     /* Automatic template
     *
     * Children will inherit the template specified in the 
     * automaticTpl TV. If no valid Template ID is found Inherit Template Plugin will kick in (if installed)
     *
     * Configuration:
     *
     * create a TV named automaticTpl, 
     * 				Input Type:  Text
     * Choose the templates you would use it for. When used give a coma separated list of templates id for your sub-containers/documents
     * Get the id of the TV from the modx_site_tmplvars
     * 
     * create a new plugin called automaticTpl and paste this code
     * check the OnDocFormPrerender event
     *			make sure this plugin is called after Inherit parent Template plugin
     * in configuration section paste the following: &automaticTplTvID=automaticTpl TV ID;text; 
     * click outside the box for the parameters fields to appear
     * enter the id for the automaticTpl TV
     *
     * Version 0.3 
     * 0.3 - completly changed the logic. You can use the automaticTpl TV for any container
     *       Previous version was limited to a certain container structure. Now other containers with different structures can be placed anywhere in the parent container
     *       The used template will be taken from the first parent that has a value for the automaticTpl TV. If no automaticTpl TV is found then the parent template is used
     * 0.1 - first version
     *
     */
    global $content;
    $e = &$modx->Event;
    
    switch($e->name) {
      case 'OnDocFormPrerender':
        if(($_REQUEST['pid'] > 0) && ($id == 0)) {
    			//init parent
    			$parent = $_REQUEST['pid'];
    			//init level
    			$level = 0;
    			//init the TV value
    			$tplVal = ""; 
    			
    			//find the first parent that has an automaticTpl TV and on which level
    			while($parent){
    				//increase level
    				$level++;
    				
    				//check if the parent has the automaticTpl TV
    				$sql = "SELECT * FROM ".$modx->db->config['table_prefix']."site_tmplvar_contentvalues WHERE tmplvarid = " . $automaticTplTvID . " AND contentid = " . $parent;
    				$rs= $modx->dbQuery($sql);
    				$row = $modx->fetchRow($rs);
    				
    				if($row && $row['value'] != ''){
    					//stop while loop
    					$parent = 0;
    					//save TV value
    					$tplVal = $row['value'];
    				}else{
    					//get next parent
    					$sql = "SELECT parent FROM ".$modx->db->config['table_prefix']."site_content WHERE id = " . $parent;
    					$rs= $modx->dbQuery($sql);
    					$row = $modx->fetchRow($rs);
    					
    					$parent = $row['parent'];
    				}
    			}//end While
    			
    			if($tplVal){
    				//set template to the automaticTpl value
    				$aTplVals = explode(",",$tplVal);
    				if(isset($aTplVals[$level-1])){
    					$content['template'] = intval($aTplVals[$level-1]);
    				}else{
    					//do nothing - Inherit Template Plugin will kick in
    				}
    			}else{
    				//do nothing - Inherit Template Plugin will kick in
    			}
    		}
    	break;
    
      default:
        return;
        break;
    }
    
    



    Any questions are welcomed.

      • 7231
      • 4,205 Posts
      Cool idea. Just thinking, what if instead of using a TV we could use the configuration tab in the plug-in manager to set the ids of the templates. This would lower the overhead since the query to the tv would not be needed.

      I normally use an adapted version of the inherit template plugin that sets a few presets (via a switch statement) and leaves the rest to be inherited.
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 5340
        • 1,624 Posts
        Quote from: dev_cw at Aug 12, 2008, 01:35 PM

        Cool idea. Just thinking, what if instead of using a TV we could use the configuration tab in the plug-in manager to set the ids of the templates. This would lower the overhead since the query to the tv would not be needed.

        Not sure this is the best solution. You need to associate the template of the first container with the list of "sub-templates". So you need to know another template id.
        But for sure will save some queries. I more comfortable to do the config in the document rather than the plugin.


        Quote from: dev_cw at Aug 12, 2008, 01:35 PM

        I normally use an adapted version of the inherit template plugin that sets a few presets (via a switch statement) and leaves the rest to be inherited.

        My plugin is based on inherit template plugin and another one called Child Template
        In my case if you create another level and you don’t specify the template id, the parent template will be inherited.

        Can you give an example of what you use. I am not sure I get your presets idea.

        Thx
          • 5340
          • 1,624 Posts
          Hi all,

          I’m thinking of "rebranding" this plugin to "Default Templates" and add a new functionality.

          The new functionality would be called "Allowed Templates" and will extend the "Default Templates" plugin as below:

          As you probably know you can define in a container TV a list of templates for children:

          12,13,20 -> first child will be created using template id 11, the grandchild 13 and so on

          Now let’s add the "Allowed Templates" functionality

          The input will look like

          12[14|16], 13[20|40], 20[16|24|30]

          What this does is to limit the available templates in the dropdown list. It is useful if your website has 50 templates and only n are compatible OR can be used in a section of your website.


          Thank You



            • 25663 MODX Staff
            • 12,272 Posts
            Sounds great Cipa. smiley
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 5340
              • 1,624 Posts
              Almost done

              What do you think of this name for the plugin: Default+Allowed Templates?
                • 25663 MODX Staff
                • 12,272 Posts
                Template Defaults
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 3749
                  • 24,544 Posts
                  Quote from: Cipa at Aug 11, 2009, 04:49 PM

                  Almost done

                  What do you think of this name for the plugin: Default+Allowed Templates?
                  Restrict Default Templates

                  or how about "Template Cop" wink

                  I’m really glad you gave up Tpl in the name because that would have caused confusion with Tpl Chunks and there’s enough confusion around their name to begin with. smiley

                    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
                    • 5340
                    • 1,624 Posts
                    How about Default Restricted Templates?
                      • 25663 MODX Staff
                      • 12,272 Posts
                      "Restricted" to me has negative connotations. Template Cop would work, or I still think Template Defaults says pretty much exactly what it does. Maybe Tempalte Sets, or Set Templates.
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me