Created
July 3, 2010 15:20
-
-
Save Cifro/462628 to your computer and use it in GitHub Desktop.
Static class for additional Nette Latte Macros with first macro {each}{eachelse}{/each}
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 | |
namespace Nette\Templates; | |
use Nette\Object; | |
use Nette\String; | |
/** | |
* Another Latte Macros | |
* | |
* @author Cifro | |
* @license MIT | |
*/ | |
class AnotherLatteMacros extends Object | |
{ | |
public function __construct() { | |
throw new \InvalidStateException("Static class."); | |
} | |
public static function register() { | |
LatteMacros::$defaultMacros['each'] = '<?php %Nette\Templates\AnotherLatteMacros::macroEach% ?>'; | |
LatteMacros::$defaultMacros['eachelse'] = '<?php endforeach; array_pop($_cb->its); $iterator = end($_cb->its); else: ?>'; | |
LatteMacros::$defaultMacros['/each'] = '<?php endif ?>'; | |
// \Nette\Templates\FormMacros::register(); // http://gist.github.com/347052 | |
} | |
public static function macroEach($content) | |
{ | |
$token = LatteFilter::fetchToken($content); | |
$modifiers = String::trim(LatteFilter::formatString($content), '"'); | |
$code = "if($token === null) $token = array(); \$___iterator = new SmartCachingIterator($token);"; | |
$code .= ' if($___iterator->count() > 0): foreach($iterator = $_cb->its[] = $___iterator ' . $modifiers . '):'; | |
return $code; | |
} | |
} |
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 | |
// blabla... | |
Nette\Templates\AnotherLatteMacros::register(); | |
// blabla... |
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
{each $someArray as $key => $value} | |
<p> | |
{if $iterator->isFirst()} | |
First item: | |
{/if} | |
{if $iterator->isLast()} | |
Last item: | |
{/if} | |
{$key} => {$value} | |
</p> | |
{eachelse} | |
<p>Empty.</p> | |
{/each} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment