Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="https://pbs.twimg.com/profile_images/637255421099536384/dkLZc90x.jpg" alt="Avatar" />
  </div>
  <div class="yellBody col-sm-10">

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

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

    </div>
  </div>
</div>
{% endfor %}

{{ path_for('deleteYell')}}/{{ post.id }} this throws me an error saying that there's missing info at the end of the url, which means that the post.id 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?

like image 345
Shayan Javadi Avatar asked Dec 21 '25 20:12

Shayan Javadi


1 Answers

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

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

As described here, hope this help

like image 158
Matteo Avatar answered Dec 24 '25 09:12

Matteo