Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • 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
  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\ParseException,
 18:     Nette\Utils\Strings;
 19: 
 20: 
 21: 
 22: /**
 23:  * Macros for Nette\Forms.
 24:  *
 25:  * - {form name} ... {/form}
 26:  * - {input name}
 27:  * - {label name /} or {label name}... {/label}
 28:  * - {formContainer name} ... {/formContainer}
 29:  *
 30:  * @author     David Grudl
 31:  */
 32: class FormMacros extends MacroSet
 33: {
 34: 
 35:     public static function install(Latte\Compiler $compiler)
 36:     {
 37:         $me = new static($compiler);
 38:         $me->addMacro('form',
 39:             'Nette\Latte\Macros\FormMacros::renderFormBegin($form = $_form = $_control[%node.word], %node.array)',
 40:             'Nette\Latte\Macros\FormMacros::renderFormEnd($_form)');
 41:         $me->addMacro('label', array($me, 'macroLabel'), '?></label><?php');
 42:         $me->addMacro('@input', array($me, 'macroAttrInput'));
 43:         $me->addMacro('input', 'echo $_form[%node.word]->getControl()->addAttributes(%node.array)');
 44:         $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = $_form[%node.word]', '$_form = array_pop($_formStack)');
 45:     }
 46: 
 47: 
 48: 
 49:     /********************* macros ****************d*g**/
 50: 
 51: 
 52:     /**
 53:      * {label ...} and optionally {/label}
 54:      */
 55:     public function macroLabel(MacroNode $node, $writer)
 56:     {
 57:         $cmd = 'if ($_label = $_form[%node.word]->getLabel()) echo $_label->addAttributes(%node.array)';
 58:         if ($node->isEmpty = (substr($node->args, -1) === '/')) {
 59:             $node->setArgs(substr($node->args, 0, -1));
 60:             return $writer->write($cmd);
 61:         } else {
 62:             return $writer->write($cmd . '->startTag()');
 63:         }
 64:     }
 65: 
 66: 
 67: 
 68:     /**
 69:      * n:input
 70:      */
 71:     public function macroAttrInput(MacroNode $node, $writer)
 72:     {
 73:         if ($node->htmlNode->attrs) {
 74:             $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
 75:             return $writer->write('echo $_form[%node.word]->getControl()->addAttributes(%var)->attributes()', $reset);
 76:         }
 77:         return $writer->write('echo $_form[%node.word]->getControl()->attributes()');
 78:     }
 79: 
 80: 
 81: 
 82:     /********************* run-time writers ****************d*g**/
 83: 
 84: 
 85: 
 86:     /**
 87:      * Renders form begin.
 88:      * @return void
 89:      */
 90:     public static function renderFormBegin($form, $attrs)
 91:     {
 92:         $el = $form->getElementPrototype();
 93:         $el->action = (string) $el->action;
 94:         $el = clone $el;
 95:         if (strcasecmp($form->getMethod(), 'get') === 0) {
 96:             list($el->action) = explode('?', $el->action, 2);
 97:         }
 98:         echo $el->addAttributes($attrs)->startTag();
 99:     }
100: 
101: 
102: 
103:     /**
104:      * Renders form end.
105:      * @return string
106:      */
107:     public static function renderFormEnd($form)
108:     {
109:         $s = '';
110:         if (strcasecmp($form->getMethod(), 'get') === 0) {
111:             $url = explode('?', $form->getElementPrototype()->action, 2);
112:             if (isset($url[1])) {
113:                 foreach (preg_split('#[;&]#', $url[1]) as $param) {
114:                     $parts = explode('=', $param, 2);
115:                     $name = urldecode($parts[0]);
116:                     if (!isset($form[$name])) {
117:                         $s .= Nette\Utils\Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
118:                     }
119:                 }
120:             }
121:         }
122: 
123:         foreach ($form->getComponents(TRUE, 'Nette\Forms\Controls\HiddenField') as $control) {
124:             if (!$control->getOption('rendered')) {
125:                 $s .= $control->getControl();
126:             }
127:         }
128: 
129:         if (iterator_count($form->getComponents(TRUE, 'Nette\Forms\Controls\TextInput')) < 2) {
130:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
131:         }
132: 
133:         echo ($s ? "<div>$s</div>\n" : '') . $form->getElementPrototype()->endTag() . "\n";
134:     }
135: 
136: }
137: 
Nette Framework 2.0beta2 API API documentation generated by ApiGen 2.3.0