1

My code:

<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>

How to add <?php echo $this->Html->link('...') ?> in CAKEPHP 2.x

Nam Lee
  • 891
  • 1
  • 9
  • 20

2 Answers2

2

If you want to insert HTML element in any HTML helper, you have to add 'escape' => false. Check the document https://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

Simple Example:

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

For you case:

$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]
);
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40
0

If you are going to use Aman's answer, remember that by setting 'escape' => false you are disabling a default security feature. So you probably want to make sure you then escape any user input using the h() method:-

$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]
);

If you've got a lot of markup you want inside your <a> tags it is sometimes simpler to use $this->Html->url() instead (and can lead to more readable code):-

<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>

The only real disadvantage I am aware of doing this second example is that you lose any functionality that you may add to $this->Html->link(), but I suspect this isn't a concern for the majority of users.

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59