1
4

2 回答 2

2

如果你想在任何 HTML 助手中插入 HTML 元素,你必须添加'escape' => false。检查文档https://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

简单示例:

$this->Html->link('<b>My Content</b>','#',[
    'escape' => false
]);

对于你的情况:

$this->Html->link(
    $this->Html->div('list_content',
        $this->Html->para('title',$note['Note']['title']).
        $this->Html->para('create_at',$note['Note']['create_at']).
        $this->Html->para(null,substr($note['Note']['content'], 0,100) . '...')
    ),
    '#',
    ['escape' => false]
);
于 2017-10-13T08:02:47.257 回答
0

如果您要使用 Aman 的答案,请记住,通过设置'escape' => false禁用默认安全功能。因此,您可能希望确保使用以下h()方法转义任何用户输入:-

$this->Html->link(
    $this->Html->div('list_content',
        $this->Html->para('title', h($note['Note']['title'])).
        $this->Html->para('create_at', h($note['Note']['create_at'])).
        $this->Html->para(null, substr(h($note['Note']['content']), 0,100) . '...')
    ),
    '#',
    ['escape' => false]
);

如果您在<a>标签中有很多想要的标记,则有时使用起来会更简单$this->Html->url()(并且可以产生更具可读性的代码):-

<a href="<?= $this->Html->url('#') ?>">
  <div class="list_content">
      <p class="title"><?php echo $note['Note']['title']; ?></p>
      <p class="create_at"><?php echo $note['Note']['create_at'] ?></p>
      <p> <?php echo substr($note['Note']['content'], 0,100) . '...' ?></p>
   </div>
</a>

我知道做第二个例子的唯一真正的缺点是你失去了你可能添加到的任何功能$this->Html->link(),但我怀疑这不是大多数用户关心的问题。

于 2017-10-13T15:22:06.653 回答