简体   繁体   中英

A function call as argument in Symfony/Twig path function

I have a route like this:

 /**
     * @Route("/admin/user/edit/{id}", defaults={"id" = 0}, name="admin_user_edit")
     * @Route("/admin/user/edit")
     * 
     */
    public function editAction(Request $request, $id = 0){
    (...)

And then a Twig template, where I list all users:

 {% for user in users %}
        <li>

          <a href='{{ path('admin_user_edit'),{'id' : user.getId()} }}'>
            {{ user.getUsername() }}
          </a>

        </li>
    {% endfor %}

As you can see I'm trying to pass the 'user.getId()' as an id. And I get this:

Unexpected token "punctuation" of value "," ("end of print statement" expected) in admin/users/list.html.twig at line 5.

I know it's about this user.getId() because everything was fine before I added this piece of code.

So how can I pass a function result to this "path" function?

Your path function should have the route name and the parameters within the parenthesis like

{{ path('admin_user_edit', {'id' : user.getId() }) }}

Also, as you aren't using arguments in your user.getId() call you can just use user.id instead.

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