Created
April 14, 2011 10:33
-
-
Save PJK/919240 to your computer and use it in GitHub Desktop.
TwigMacro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function templatePrepareFilters($template) { | |
$template->registerFilter($latte = new Nette\Templates\LatteFilter()); | |
TwigMacro::register($latte->getHandler()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Nette\Templates\LatteMacros; | |
use Nette\Debug; | |
/** | |
* @author Pavel Kalvoda | |
* @author Filip Procházka | |
*/ | |
class TwigMacro { | |
const SEPARATOR = '.'; | |
/** @var Nette\Templates\LatteMacros */ | |
protected static $latte; | |
/** | |
* @param Nette\Templates\LatteMacros $latte | |
*/ | |
public static function register(LatteMacros $latte) { | |
// nefuguje! vizte DefaultMacros, line 303 | |
$latte->macros['!~$'] = '<?php %' . __CLASS__ . '::expandRaw% ?>'; | |
$latte->macros['!~'] = '<?php %' . __CLASS__ . '::expandRaw% ?>'; | |
$latte->macros['~$'] = '<?php %' . __CLASS__ . '::expand% ?>'; | |
$latte->macros['~'] = '<?php %' . __CLASS__ . '::expand% ?>'; | |
self::$latte = $latte; | |
} | |
/** | |
* @param mixed $path | |
* @param mixed $modifiers | |
* @param bool $escape | |
* @return string | |
*/ | |
public static function expandRaw($path, $modifier) { | |
return self::expand($path, $modifiers, false); | |
} | |
/** | |
* @param mixed $path | |
* @param mixed $modifiers | |
* @param bool $escape | |
* @return string | |
*/ | |
public static function expand($path, $modifiers, $escape = true) { | |
$frags = explode(self::SEPARATOR, $path); | |
$result = __CLASS__ . '::travel($' . array_shift($frags) . ', ' | |
. var_export($frags, true) . ',' . var_export($escape, true) . ')'; | |
return 'echo ' . self::$latte->formatModifiers($result, $modifiers) . ';'; | |
} | |
/** | |
* @param object|array $element | |
* @param array $path | |
* @param bool $escape | |
* @return mixed | |
*/ | |
public static function travel($element, array $path = array(), $escape = true) { | |
foreach ($path as $leaf) { | |
Nette\Debug::barDump($element, "Elem"); | |
if ((is_array($element) || $element instanceof \ArrayAccess) && isset($element[$leaf])) { // http://www.php.net/manual/en/function.is-array.php#48083 | |
$element = $element[$leaf]; | |
continue; | |
} elseif (is_object($element)) { | |
if (method_exists($element, $method = "get" . ucfirst($leaf))) { | |
$element = $element->$method(); | |
continue; | |
} elseif (isset($element->$leaf)) { | |
$element = $element->$leaf; | |
continue; | |
} | |
} | |
throw new \InvalidStateException("Given structure doesn't contain specified offset, property, or getter method."); | |
} | |
return $escape ? htmlSpecialChars($element, ENT_QUOTES) : $element; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment