Question

Should it work in Silex with symfony/twig-bridge ?

{{ render(controller('MyController')) }}

Now I have message like this:

Twig_Error_Syntax: The function "controller" does not exist in "...

Was it helpful?

Solution 2

I've found this working:

{{ render(controller('services.controller:action', {[params]}) }}

And you can define the controller as a service:

$app['services.controller'] = function() use ($dependecy1, .., $dependencyN){
    return new \\PathToYourControllerClass($dependecy1, .., $dependencyN);
} 

OTHER TIPS

You can use it this way:

{{ render(path('your_route_id', {'id': id, 'anotherParam': param})) }}

I've found this working :

{{ render(controller('Full\\Namespace\\To\\Your\\Controller::listAction')) }}

please don't forget a double slash '\\'

Example:

{{ render(controller('Acme\\ProductController::listAction')) }}

In Your ProductController (I'm using Doctrine 2 in this example) :

public function listAction(Application $application)
{
    $em = $application['orm.em'];

    $produits = $em->getRepository('Acme\Entity\Produit')->findAll();

    return $application['twig']->render('list.html.twig', array(
        'products' => $products
    ));
}

Then in your list.html.twig

{% for product in products %}
  <h2> {{ product.name }} </h2>
{% endfor %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top