20

I am using Symfony2 and Twig. I have a function (below) in my controller that returns a specific text. Is it possible to call that function directly from my template and change the {{text}} in my template to whatever the function returns, possibly via Ajax?

Here's my function:

public function generateCode($url) {
    $url = $_SERVER['SERVER_NAME'] . '/embed/' . $url;
    $return = '<iframe>'.$url.'</iframe>';
    return $return;
}

Another controller function calls the function above and renders my template:

public function getCodeAction($url) {
    $text = $this->generateCode($url);
    return $this->render('MyMyBundle:User:code.html.twig', array('text' => $text));
}

In my template I am using:

{{ text }}

to display the value.

6 Answers 6

36

In Symfony 2.2, this was changed.

The render tag signature and arguments changed.

Before:

{% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': BlogBundle:Post:error' } %}

After:

{% render controller('BlogBundle:Post:list', { 'limit': 2 }), { 'alt': 'BlogBundle:Post:error' } %}

or

{{ render(controller('BlogBundle:Post:list', { 'limit': 2 }), { 'alt': 'BlogBundle:Post:error'}) }}

Note: The function is the preferred way.

See https://github.com/symfony/symfony/blob/2.2/UPGRADE-2.2.md

Sign up to request clarification or add additional context in comments.

1 Comment

to avoid any confusion: {{ render(controller(... }} is the function
23

You can use ajax if you have dynamic data, but as far as I can see from your brief info, you can always execute that controller function directly from your view:

{% render "MyMyBundle:User:generateCode" with { 'url': 'your url here' } %}

More Information on this available at: http://symfony.com/doc/2.0/quick_tour/the_view.html, under Embedding other Controllers

2 Comments

Thanks. It worked, I just had to play around with the render method a little - now even Ajax works smoothly. Thanks!
@Mike What do you mean with playing around?
9

For the record, in new versions you need to use the absolute URL:

{{ render url('my_route_id', {'param': value}) }}

Comments

7

{{ render(controller("AcmeDemoBundle:Demo:topArticles", {'num': 10})) }}

Comments

4

In Silex I solved it like this:

{{ render(url('route_name', {'param': value})) }}

If you do not have the route name, URL can be used:

{{ render(app.request.baseUrl ~ '/some-path/' ~ value) }}

If using URL we should always concat the baseUrl.

Comments

2

Symfony 2.6+

in twig:

{{ render(controller('AppBundle:PropertySearch:featuredProperties', {'limit': 15})) }}

controller:

/**
 * featuredPropertiesAction
 * 
 * @param Request $request
 * @param int $limit
 *
 * @return Response
 */
public function featuredPropertiesAction(Request $request, $limit)
{
    $search = $this->resultsHelper->featuredSearch($limit);

    return $this->render('HASearchBundle::featured_properties.html.twig', [
        'search' => $search,
    ]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.