Skip to content

Instantly share code, notes, and snippets.

@alroniks
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alroniks/c3d432f08fcee3ba1b35 to your computer and use it in GitHub Desktop.
Save alroniks/c3d432f08fcee3ba1b35 to your computer and use it in GitHub Desktop.
extends
<?php
/*
* Nested templates in MODX Revolution
*
* Using: Make layout template and call it in other template as [[>layout]]
* Use [[@yield]] for determine block that will be replaced by child temlate
*
* Required OnParseDocument event
*
* @author Ivan Klimchuk <ivan@klimchuk.com>
*/
if (!function_exists('ext')) {
function ext($content, $currentTpl) {
global $modx;
$pattern = '#\[\[\>(.+?)\]\]#si';
if (preg_match($pattern, $content, $m) > 0) {
$tag = $m[0];
$tpl = $m[1];
$parentTpl = $modx->getObject('modTemplate', array('templatename' => $tpl));
$currentTpl = $modx->getObject('modTemplate', $currentTpl);
$content = $parentTpl->content;
if (preg_match($pattern, $parentTpl->content)) {
$content = ext($parentTpl->content, $parentTpl->id);
}
$currentTpl = str_replace($tag, '', $currentTpl->content);
$content = str_replace(
'[[@yield]]',
$currentTpl,
$content
);
}
return $content;
}
}
switch($modx->event->name) {
case 'OnParseDocument':
if ($modx->context->key != 'mgr') {
$modx->documentOutput = ext($modx->documentOutput, $modx->resource->template);
}
break;
}
[[>layout]]
<section class="content">
<div class="container">
<div class="row">
<div class="col-lg-12">
[[$crumbs]]
<h2 class="header">[[*longtitle:empty=`[[*pagetitle]]`]]</h2>
<div class="content">
[[*content]]
</div>
</div>
</div>
</div>
</section>
<!DOCTYPE html>
<html lang="ru">
<head>
[[$head]]
</head>
<body>
[[$topline]]
[[$header]]
[[$navigation]]
[[@yield]]
[[$footer]]
[[$bottomline]]
<script src="[[++assets_url]]theme.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment