简体   繁体   中英

Passing route parameter to Twig template using Silex's app.url_generator.generate

I am using Silex to create a quick website. I am trying to pass a variable from a route to the view in a dynamic page.

Here is my controller code:

$app->get('/clients/view/{refnum}', function (Silex\Application $app, Request $request, $refnum) {
    return $app['twig']->render('client.twig', array('flag'=>0, 'refnum' => $refnum));
})->bind('client_view');

Here is my twig code:

{% extends 'layout.html.twig' %}

{% block pagecontent %}

<div class="container-fluid">
    <div class="row">
<h1>Hello, {{ refnum|default('Not Found!')|capitalize }}!</h1>
    </div>
</div>

{% endblock %}

When I directly type a URL like http://foobar/clients/view/123 in the browser, it works.

However, I am trying to dynamically create the links in my page.

This is an HTML snippet showing what I'm doing:

<td><a href="{{ app.url_generator.generate('client_view', {'refnum': {{refnum}}  } ) }}">John Smith</a></td>

I get the Twig exception message:

Twig_Error_Syntax in ExpressionParser.php line 281: A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in "clients.home.twig" at line 47

Ho do I fix this?

试试看:

<td><a href="{{ app.url_generator.generate('client_view', {'refnum': refnum  } ) }}">John Smith</a></td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM