Y
Y
Yuri Chernyshev2011-10-20 18:37:51
MODX
Yuri Chernyshev, 2011-10-20 18:37:51

MODx: Automatically apply a template to certain pages?

I'm just starting to learn MODX, I ran into such a problem: it is necessary to automatically apply one of the templates for certain pages when creating them . For example, for all pages that are children of the "news" resource, use the "news" template.
Is it even possible?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
d-udovichenko, 2015-03-09
@d-udovichenko

This can be done via Manager Customization
- Top Menu -> Admin Settings
- Create a new profile (tick active)
- Create a new set of rules for "Create resource"
- Bounding field: 'parent'
- Bounding value: id of the parent template
- Find the template field and assign the desired template.
Video
instruction www.youtube.com/watch?v=PS4TmQcohsA

D
d4rkr00t, 2011-10-20
@d4rkr00t

Yes, in the revo it seems to be happening if the news resource itself (the parent) has the news template

D
Desiderata, 2011-10-20
@Desiderata

In Evo 1.0.5 there is an option: Tools -> Configuration: Automatic Template Assignment (Neighbor: same template as neighboring resources in this container (if there are no neighbors, template Parent).)
I don't know about Revo, but it's quite possible, what's there also.

A
alvisX, 2014-04-09
@alvisX

I had a link to the manual in Russian, but the source is no longer available ... It's
very easy to make a plugin, in the comments all the instructions:

<?php
/**
* =========================
* ChangeTemplate
* =========================
*
* Plugin for MODX Revolution
* Set which template is inherited by children
* documents of a certain parent document
*
* Author:
* Marc Loehe (boundaryfunctions)
* marcloehe.de
*
* Modified by:
* Lorenzo Stanco <[email protected]>
* Lorenzostanco.com
*
* Usage:
*
* 1. Paste this as new plugin and connect it to system event
* 'OnDocFormRender'.
*
* 2. Assign a new TV 'changeTemplate' to each template
* for which you want to define the default children template.
*
* 3. Set the newly created TV to input type "Text"
*
* 4. Open a document and in the 'changeTemplate' TV type a
* comma separated list of template IDs.
*
* 5. Have fun!
*
*/
  
// Check Event
if ($modx->event->name == OnDocFormRender && $mode == modSystemEvent::MODE_NEW) {
    
  // Get current document ID
  if ($id = $_REQUEST['id']) {
  
    // Document Chain
    $resources = array($id);
  
    // Get parent ID
    foreach ($modx->getParentIds($id, 10, array('context' => $_REQUEST['context_key'])) as $parentId) {
      if ($parentId) array_push($resources, $parentId);
    }
      
    // Search changeTemplate in the chain
    $level = 0;
    $childTemplates = array();
    foreach ($resources as $resourceId) {
      $resource = $modx->getObject('modResource', $resourceId);
      if ($childTemplatesTV = $resource->getTVValue('changeTemplate')) {
          
        // Create template array for each tree level
        $childTemplates = @explode(',', $childTemplatesTV);
        if (empty($childTemplates)) break;
        foreach ($childTemplates as $k => $v) $childTemplates[$k] = intval(trim($v));
          
        break;
  
      }
  
      $level++;
  
    }
  
    // Set template based on tree level
    if (!empty($childTemplates)) {
      $useTemplate = $childTemplates[$level];
      if (!empty($useTemplate)) {
         
        // Set default template
        if (isset($modx->controller)) {
          $modx->controller->setProperty('template', $useTemplate);
        } else { // modX < 2.2.0
          $_REQUEST['template'] = $useTemplate;
        }
  
      }
    }
  
  }
  
}

D
dmitryosipov, 2014-09-08
@dmitryosipov

Use controllers - dependencies won't bother with templates

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question