Skip to content

Instantly share code, notes, and snippets.

@alooze
Last active December 14, 2015 05:48
Show Gist options
  • Save alooze/5037487 to your computer and use it in GitHub Desktop.
Save alooze/5037487 to your computer and use it in GitHub Desktop.
MODX Evolution plugin for clean output for HTML blocks with placeholders or another MODX entities.
//<?php
/**
* cleanEmptyBlock plugin for MODX Evo
* @Author: alooze ([email protected])
* @Version: 0.3POC
* @Date: 16.04.2013
* @Event: OnWebPagePrerender
* @Params: &passes=Количество проходов (Passes Count);text;1
*/
/**
*
*
* Usage:
*
* 1) Install plugin on OnWebPagePrerender event.
* 2) Use syntax:
* [? _HTML_CODE_WITH_~~[+placeholder+]~~_AND_MORE_CODE_?]
* 3) If [+placeholder+] was replaced with empty string and in code was found '~~~~', all [?...?] construction will be dropped.
*
* Can be used for
*
* [?...~~{{CHUNK}}~~ ...?]
* [?...~~[[SNIPPET]]~~ ...?]
* [?...~~[!SNIPPET!]~~ ...?]
* [?...~~[*TV*]~~ ...?]
* [?...~~[(CONFIG_PARAM)]~~...?]
*/
$e = $modx->event;
switch ($e->name) {
case 'OnWebPagePrerender':
$passes = isset($passes) ? $passes : 1;
for ($j=0; $j<$passes; $j++) {
//для каждого прохода обрабатываем готовый код страницы
$replace= array ();
$matches= array ();
//находим конструкции вида
//[? _какой-то HTML код ~~[+ph+]~~ с плейсхолдером или ~~[!snip!]~~ сниппетом_ ?]
if (preg_match_all('~\[\?([^\[\]]*?)\?\]~ms', $modx->documentOutput, $matches)) {
//print_r($matches[0]);
$checkCnt = count($matches[1]);
for ($i= 0; $i < $checkCnt; $i++) {
if (strpos($matches[1][$i], '~~~~')) {
//плейсхолдер или сниппет отдали пустое значение, блок не выводим
$replace[$i] = '';
} else {
//между ~~ ~~ есть хотя бы пробел, этого достаточно, чтобы показать весь блок
$replace[$i] = str_replace('~~', '', $matches[1][$i]);
}
}
$modx->documentOutput = str_replace($matches[0], $replace, $modx->documentOutput);
}
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment