Twig for CakePHP

2019-08-05 10:31发布

I am trying to use Twig with CakePHP, so I installed this plugin:
https://github.com/predominant/TwigView

And in CakePHP's demo, we build a blog, and I can use this in a tpl file:

{% for post in posts %}
<tr>
    <td>{{post.Post.id}}</td>
    <td>{{post.Post.title}}</td>
    <td>Edit | Delete</td>
    <td>{{post.Post.created|date("F j, Y")}}</td>
</tr>
{% endfor %}

What I can't get to work, is converting this:

<?php
echo $this->Html->link(
    'Add Post', array('controller' => 'posts', 'action'     => 'add')
);
?>

I have tried all of these, and none of them work:

{{ html.link("Add Post", {"controller" : "posts", "action" : "add"}) }}
{{ _view.html.link("Add Post", {"controller" : "posts", "action" : "add"}) }}
{{ this.html.link("Add Post", {"controller" : "posts", "action" : "add"}) }}

I don't get any errors, it just gets replaced with nothing. Anyone know how I can fix this issue?

标签: php cakephp twig
4条回答
放我归山
2楼-- · 2019-08-05 11:08

You have to explicitly declare the helpers in the controller to make it work:

public $helpers = array('Html', 'Form');

See GitHub issue #14 and #13 where I got this from.

查看更多
走好不送
3楼-- · 2019-08-05 11:09

Maybe it just won't take an array as the argument or doesn't understand what controller or action is. Try:

{{ html.link("Add Post", "/posts/add" }}
查看更多
地球回转人心会变
4楼-- · 2019-08-05 11:10

Proper syntax for hyperlinks html helper is:

{{ html.link('Add Post', '/posts/add') }}
查看更多
Ridiculous、
5楼-- · 2019-08-05 11:19

Is it escaping the output? If so, to get the full HTML, use RAW

{{ html.link("Add Post", {"controller" : "posts", "action" : "add"})|raw }}
查看更多
登录 后发表回答