问题描述:
我的代码:在我的<a>标记中,有很多HTML代码。如何添加HTML链接(CakePHP的)
<a href="#">
<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>
如何在CakePHP 2.x的
答
如果你想在任何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]
);
答
如果你要使用祖阿曼的回答,请记住,通过设置'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()
功能,但我怀疑这是不是一个关注广大用户。
如果你禁用了'escape',你应该记得使用'h()'来逃避任何用户输入。为了安全起见,Cake默认故意转义链接文本,因此禁用此操作需要谨慎操作。 – drmonkeyninja
非常感谢,但我使用CKEditor添加内容注意。当内容有标签 ... 时,它不会位于$ this-> Html-> link('...')。 –
如何在$ this-> Html-> link('...')中添加标记 –