【问题标题】:Using Twig templating engine to dynamically create links for a route with a required placeholder ( /post/{id} )使用 Twig 模板引擎为具有所需占位符 ( /post/{id} ) 的路由动态创建链接
【发布时间】:2016-11-20 19:30:19
【问题描述】:

所以我有一个数组,其中包含正在传递给 twig 的帖子,然后 Twig 将使用 for 循环从那里输出数组中的每个帖子。我希望每个帖子都有一个删除按钮,其 url 附有该帖子的 id 所以在我的 for 循环中我有这样的东西:

{% 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 }} 这向我抛出了一个错误,提示 url 末尾缺少信息,这意味着 post.id 部分未在 / 之后附加

我的路线如下:

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

我应该进行哪些更改才能使其正常工作?

【问题讨论】:

    标签: php twig url-routing slim


    【解决方案1】:

    您可以将路由的参数作为第二个参数(作为数组)传递,例如:

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

    here 所述,希望对您有所帮助

    【讨论】:

    • 这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多