Commit 7f6fc398 by Alexander Makarov

Fixes #1799: Added form_begin, form_end to twig extension

parent 539b74d1
......@@ -52,26 +52,26 @@ or `$this->renderPartial()` controller calls:
echo $this->render('renderer.twig', ['username' => 'Alex']);
```
### Additional syntax
Yii adds some extra syntax constructs additionally to standard Twig ones.
###
{{registerAssetBundle('AppAsset')}} - Registers asset bundle of a given name
### Forms
There are two form helper functions `form_begin` and `form_end` to make using forms more convenient:
```
{% set form = form_begin({ ... }) %}
{{ form.field(...) }}
{% form.end() %}
{% set form = form_begin({
'id' : 'login-form',
'options' : {'class' : 'form-horizontal'},
}) %}
{{ form.field(model, 'username') | raw }}
{{ form.field(model, 'password').passwordInput() | raw }}
<div class="form-group">
<input type="submit" value="Login" class="btn btn-primary" />
</div>
{{ form_end() }}
```
#### Getting URL for a route
### Getting URL for a route
There are two functions you can use for URLs:
......
......@@ -5,6 +5,7 @@ Yii Framework 2 twig extension Change Log
--------------------------
- Bug #2925: Fixed throwing exception when accessing AR property with null value (samdark)
- Enh #1799: Added `form_begin`, `form_end` to twig extension (samdark)
2.0.0-beta April 13, 2014
......
......@@ -11,6 +11,7 @@ use Yii;
use yii\base\View;
use yii\base\ViewRenderer as BaseViewRenderer;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
/**
* TwigViewRenderer allows you to use Twig templates in views.
......@@ -115,11 +116,6 @@ class ViewRenderer extends BaseViewRenderer
$this->setLexerOptions($this->lexerOptions);
}
// $this->addFunctions([
// 'rot13' => 'str_rot13',
// 'jsonEncode' => '\yii\helpers\Json::encode',
// ]);
// Adding global 'void' function (usage: {{void(App.clientScript.registerScriptFile(...))}})
$this->twig->addFunction('void', new \Twig_Function_Function(function ($argument) {
}));
......@@ -132,6 +128,14 @@ class ViewRenderer extends BaseViewRenderer
return Url::to(array_merge([$path], $args), true);
}));
$this->twig->addFunction('form_begin', new \Twig_Function_Function(function ($args = []) {
return ActiveForm::begin($args);
}));
$this->twig->addFunction('form_end', new \Twig_Function_Function(function () {
ActiveForm::end();
}));
$this->twig->addGlobal('app', \Yii::$app);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment