Skip to content

Instantly share code, notes, and snippets.

@vojtech-dobes
Created December 14, 2011 00:12
Show Gist options
  • Save vojtech-dobes/1474583 to your computer and use it in GitHub Desktop.
Save vojtech-dobes/1474583 to your computer and use it in GitHub Desktop.
Manual rendering of Nette RadioList & CheckboxList
<?php
/**
* This file is part of the Nette Framework (http://nette.org)
*
* Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/
namespace Clevispace\Latte;
use Nette,
Nette\Latte,
Nette\Latte\MacroNode;
/**
* Macros for Radiolist & CheckboxList
*
* - {inputlist name} ... {/inputlist}
* - alternative {input} and {label}
*
* @author Vojtech Dobes
*/
class InputlistFormMacros extends Latte\Macros\MacroSet
{
/** @var bool */
private $inList = FALSE;
public static function install(Latte\Parser $parser)
{
$me = new static($parser);
$me->addMacro('inputlist', array($me, 'macroList'), array($me, 'macroListEnd'));
$me->addMacro('input', array($me, 'macroInput'));
$me->addMacro('label', array($me, 'macroLabel'));
}
/********************* macros ****************v*d**/
/**
* {inputlist}
*/
public function macroList(MacroNode $node, $writer)
{
$this->inList = TRUE;
return $writer->write('$_inputlist = $form[%node.word]; $iterations = 0; foreach ($iterator = $_l->its[] = new Nette\Iterators\CachingIterator($_inputlist->getItems()) as $_inputlistKey => $_inputlistItem):');
}
/**
* {/inputlist}
*/
public function macroListEnd(MacroNode $node, $writer)
{
$this->inList = FALSE;
return '$iterations++; endforeach; array_pop($_l->its); $iterator = end($_l->its); unset($_inputlist)';
}
/**
* {input} in {inputlist}
*/
public function macroInput(MacroNode $node, $writer)
{
if (!$this->inList) {
return FALSE;
}
return $writer->write('$_control = $_inputlist->getControl($_inputlistKey); echo Nette\Utils\Html::el(substr($_control, 1, strpos($_control, "<label") - 2))->addAttributes(%node.array); unset($_control)');
}
/**
* {label} for {input} in {inputlist}
*/
public function macroLabel(MacroNode $node, $writer)
{
if (!$this->inList) {
return FALSE;
}
return $writer->write('$_control = array($_inputlist->getControl($_inputlistKey)); $_control[1] = substr($_control[0], strpos($_control[0], "<label") + 1, -8); $_control[1] = explode(">", $_control[1]); echo Nette\Utils\Html::el($_control[1][0])->addAttributes(%node.array)->setText($_control[1][1]); unset($_control)');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment