使用 Silex 的 app.url_generator.generate 将路由参数传递给 Twig 模板

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

我正在使用 Silex 创建一个快速网站。我正在尝试将变量从路由传递到动态页面中的视图。

这是我的控制器代码:

$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');

这是我的树枝代码:

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

{% block pagecontent %}

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

{% endblock %}

当我在浏览器中直接输入 URL 比如 http://foobar/clients/view/123 时,它起作用了。

但是,我正在尝试在我的页面中动态创建链接。

这是一个 HTML 片段,展示了我正在做的事情:

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

我收到 Twig 异常消息:

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

我该如何解决这个问题?

试试看:

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