schmittjoh/JMSTwigJsBundle — Issue #5: Can we port functions like form_errors, form_row, form_widget?
Author: jonathaningram
Created: 2012-03-06 00:24:52+00:00
State: open

Originally created at https://github.com/schmittjoh/twig.js/issues/18:

Is it easy and possible for me to provide a PR to enable use of the `Symfony\Bridge\Twig\Extension\FormExtension` functions:

```php
new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
'csrf_token' => new \Twig_Function_Method($this, 'getCsrfToken'),
'_form_is_choice_group' => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))),
'_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))),
```

In twig.js? I could be on the wrong workflow with what I am doing, but basically I have overridden some blocks for my forms (this is for a collection type, so think of many rows in the form that you can add and remove), and I'd like to be able to render the row in JavaScript in order that I can dynamically add rows using twig.js. My overridden template for my type `mytype` (e.g. `form.html.twig`) is similar to this:

```jinja
{%- block acme_core_mytype_row -%}

{{- form_errors(form) -}}
{{- form_row(form.key) -}}
{{- form_row(form.value) -}}

{{- block('tools') -}}

{%- endblock -%}
```

With the intent that I can call this from JS:

```js
var myTemplate = new acme.core.mytype.form.html(Twig);

$("table.mytable tbody").append(myTemplate.renderBlock("acme_core_mytype_row", {
form: {
// all the properties of the form that I want to be in the new row
}
}));
```

What do you think?