2017-10-13 144 views

回答

2

如果你想在任何HTML帮助插入HTML元素添加<?php echo $this->Html->link('...') ?>,你必须添加'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] 
); 
+1

如果你禁用了'escape',你应该记得使用'h()'来逃避任何用户输入。为了安全起见,Cake默认故意转义链接文本,因此禁用此操作需要谨慎操作。 – drmonkeyninja

+0

非常感谢,但我使用CKEditor添加内容注意。当内容有标签 ... 时,它不会位于$ this-> Html-> link('...')。 –

0

如果你要使用祖阿曼的回答,请记住,通过设置'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()功能,但我怀疑这是不是一个关注广大用户。

相关问题