Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • CacheMacro
  • CoreMacros
  • FormMacros
  • MacroSet
  • UIMacros
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Latte\Macros;
 13: 
 14: use Nette,
 15:     Nette\Latte,
 16:     Nette\Latte\MacroNode,
 17:     Nette\Latte\PhpWriter,
 18:     Nette\Latte\CompileException,
 19:     Nette\Forms\Form,
 20:     Nette\Utils\Strings;
 21: 
 22: 
 23: 
 24: /**
 25:  * Macros for Nette\Forms.
 26:  *
 27:  * - {form name} ... {/form}
 28:  * - {input name}
 29:  * - {label name /} or {label name}... {/label}
 30:  * - {formContainer name} ... {/formContainer}
 31:  *
 32:  * @author     David Grudl
 33:  */
 34: class FormMacros extends MacroSet
 35: {
 36: 
 37:     public static function install(Latte\Compiler $compiler)
 38:     {
 39:         $me = new static($compiler);
 40:         $me->addMacro('form',
 41:             'Nette\Latte\Macros\FormMacros::renderFormBegin($form = $_form = (is_object(%node.word) ? %node.word : $_control[%node.word]), %node.array)',
 42:             'Nette\Latte\Macros\FormMacros::renderFormEnd($_form)');
 43:         $me->addMacro('label', array($me, 'macroLabel'), '?></label><?php');
 44:         $me->addMacro('input', '$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%node.array)', NULL, array($me, 'macroAttrInput'));
 45:         $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = (is_object(%node.word) ? %node.word : $_form[%node.word])', '$_form = array_pop($_formStack)');
 46:     }
 47: 
 48: 
 49: 
 50:     /********************* macros ****************d*g**/
 51: 
 52: 
 53:     /**
 54:      * {label ...} and optionally {/label}
 55:      */
 56:     public function macroLabel(MacroNode $node, PhpWriter $writer)
 57:     {
 58:         $cmd = '$_input = is_object(%node.word) ? %node.word : $_form[%node.word]; if ($_label = $_input->getLabel()) echo $_label->addAttributes(%node.array)';
 59:         if ($node->isEmpty = (substr($node->args, -1) === '/')) {
 60:             $node->setArgs(substr($node->args, 0, -1));
 61:             return $writer->write($cmd);
 62:         } else {
 63:             return $writer->write($cmd . '->startTag()');
 64:         }
 65:     }
 66: 
 67: 
 68: 
 69:     /**
 70:      * n:input
 71:      */
 72:     public function macroAttrInput(MacroNode $node, PhpWriter $writer)
 73:     {
 74:         if ($node->htmlNode->attrs) {
 75:             $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
 76:             return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%var)->attributes()', $reset);
 77:         }
 78:         return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->attributes()');
 79:     }
 80: 
 81: 
 82: 
 83:     /********************* run-time writers ****************d*g**/
 84: 
 85: 
 86: 
 87:     /**
 88:      * Renders form begin.
 89:      * @return void
 90:      */
 91:     public static function renderFormBegin(Form $form, array $attrs)
 92:     {
 93:         $el = $form->getElementPrototype();
 94:         $el->action = $action = (string) $el->action;
 95:         $el = clone $el;
 96:         if (strcasecmp($form->getMethod(), 'get') === 0) {
 97:             list($el->action) = explode('?', $action, 2);
 98:             if (($i = strpos($action, '#')) !== FALSE) {
 99:                 $el->action .= substr($action, $i);
100:             }
101:         }
102:         echo $el->addAttributes($attrs)->startTag();
103:     }
104: 
105: 
106: 
107:     /**
108:      * Renders form end.
109:      * @return string
110:      */
111:     public static function renderFormEnd(Form $form)
112:     {
113:         $s = '';
114:         if (strcasecmp($form->getMethod(), 'get') === 0) {
115:             $url = explode('?', $form->getElementPrototype()->action, 2);
116:             if (isset($url[1])) {
117:                 list($url[1]) = explode('#', $url[1], 2);
118:                 foreach (preg_split('#[;&]#', $url[1]) as $param) {
119:                     $parts = explode('=', $param, 2);
120:                     $name = urldecode($parts[0]);
121:                     if (!isset($form[$name])) {
122:                         $s .= Nette\Utils\Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
123:                     }
124:                 }
125:             }
126:         }
127: 
128:         foreach ($form->getComponents(TRUE, 'Nette\Forms\Controls\HiddenField') as $control) {
129:             if (!$control->getOption('rendered')) {
130:                 $s .= $control->getControl();
131:             }
132:         }
133: 
134:         if (iterator_count($form->getComponents(TRUE, 'Nette\Forms\Controls\TextInput')) < 2) {
135:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
136:         }
137: 
138:         echo ($s ? "<div>$s</div>\n" : '') . $form->getElementPrototype()->endTag() . "\n";
139:     }
140: 
141: }
142: 
Nette Framework 2.0.8 API API documentation generated by ApiGen 2.8.0