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

Classes

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