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.