Shawn Javadi Shawn Javadi - 10 months ago 42
PHP Question

Using Twig templating engine to dynamically create links for a route with a required placeholder ( /post/{id} )

So I have an array with posts in it that is being passed to twig and from there Twig will be outputting each post in the array using a for loop. I want each post to have a delete button with a url that has that post's id attached to it
so inside my for loop I have something like this:

{% for post in Posts %}
<div class="yellBox">
<div class="col-sm-2">
<img class="square yellBoxImage" src="" alt="Avatar" />
<div class="yellBody col-sm-10">

<h4><strong>{{ }}</strong><span class="yellDate">- {{ post.created_at }}</span></h4>
{{ post.body }}
// other buttons

//the delete button in question
<a href="{{ path_for('deleteYell')}}/{{ }}"><button type="button" name="button">Delete</button></a>
<div id="test">

{% endfor %}

{{ path_for('deleteYell')}}/{{ }}
this throws me an error saying that there's missing info at the end of the url, which means that the part is not being attatched after the /

my route is as follows:

$this->delete('/yell/{id}', 'UserController:deleteYell')->setName('deleteYell');

What change(s) should I make to get this to work?

Answer Source

You can pass the argument of the route as second argument (as array), as example:

{{ path_for('deleteYell', { 'id': }) }}

As described here, hope this help