I am using Kendo UI as the frontend library for a Symfony2.3.4 app and trying to establish best practices for working with Controllers and the Twig templating engine. My question is specific to the Kendo PHP wrapper library which allow for easy instantiation of Kendo objects.
The problem is passing those objects to twig templates. For example:
// ItemController.php
public function addAction(){
$options = ['option1', 'option2', 'option3']
$dropdown = new \\Kendo\UI\DropDownList($options);
$select = $dropdown->render();
return $this->render('TestBundle:Titles:add.html.twig',
['options' = $options, 'dropdown' => $dropdown, 'select' => $select];
}
With this controller I can pass the Kendo object ($dropdown) into the template, but the actual rendered dropdown ($select) throws an array to string error. Now I can still render the object in the template by using the $options array:
// add.html.twig
<h1>Add Item</h1>
<!-- This renders the Kendo object -->
{{ dump(dropdown) }} <!-- object(Kendo\UI\DropDownList) (5) { etc -->
<!-- This throws an error -->
{{ dump(select) }}
<input id="dropdownlist" />
<script>
// this renders the dropdown
$("#dropdownlist").kendoDropDownList({
dataSource: {
data: [
{% for option in options %}
"{{ option }}",
{% endfor %}
]
}
});
</script>
There doesn't seem to be an efficient way of passing a string snippet of html into a template in the Symfony2/Twig system. I can still render the dropdown in the template (by using the $options array) but then what's the use of having the PHP Kendo object?
Also, say I have an owner for each item and I want to create a dropdown of owner options in the addAction of the ItemController? Can I have a selectAction in the OwnerController that returns html for an Owner dropdown or an array of Owners?
This type of layout seemed to have been dealt with by the slots of Symfony 1.* ... but it is not clear how to do it with Symfony2 and Twig.
What is the 'best practice' way of dealing with formbuilding in Symfony ... particularly when you are calling from more than Entity (Items & Owners) in one form?
And/or is there a way of properly using PHP wrappers for JS objects like those in Kendo UI within a standard Symfony2 controller?