Osama Yawar
Osama Yawar

Reputation: 369

CAKEPHP: Linking the whole <div>

I have a having view of a specific field in Notes table.

    echo "<div id='notescover'>";
    echo "<p class='text'>"; 
    echo $viewnotes['Note']['notes']; 
    echo "</p>"; 
    echo "</div>";

And i have linked Comment to the specific note URL e.g /note/view/IDofNote

echo $this->Html->link('Comment', array( 'controller'=>'notes', 'action'=>'view',$viewnotes['Note']['id']), array('class'=> 'light_blue'));

I want to remove this link and link the same action to the above div tag. I was able to link the text inside the field like this

echo $this->Html->link($viewnotes['Note']['notes'], array( 'controller'=>'notes', 'action'=>'view',$viewnotes['Note']['id']), array('class'=> 'light_blue'));

but i want the whole box that makes to be linked.

How can i do that?

Upvotes: 1

Views: 4789

Answers (2)

Nishal K.R
Nishal K.R

Reputation: 1130

You can use Link or URL method inside HtmlHelper function

Using Link

$content = "<div id='notescover'>".
           "  <p class='text'>" . $viewnotes['Note']['notes'] . "</p>".
           "</div>";

echo $this->Html->link( $content, 
                        array( 'controller'=>'notes', 'action'=>'view', $viewnotes['Note']['id']), 
                        array('class'=> 'light_blue', 'escape' => false)
);

Using URL

Adding the URL inside the HTML

<a href="<?= $this->Html->url(array('controller'=>'notes', 'action'=>'view', $viewnotes['Note']['id']), 
                              array('escape' => false)); ?>" 
  class="light_blue">
   <div id='notescover'>
      <p class='text'><?= $viewnotes['Note']['notes']; ?></p>
   </div>
</a>

Upvotes: -1

BadHorsie
BadHorsie

Reputation: 14544

Something like this:

$text = '<p class="text">' . $viewnotes['Note']['notes'] . '</p>';
$div = $this->Html->div(null, $text, array('id' => 'notescover'));

echo $this->Html->link(
    $div, 
    array('controller' => 'notes', 'action' => 'view', $viewnotes['Note']['id']),
    array('class' => 'light_blue', 'escape' => false)
);

You could also compact it some more by doing this, if you prefer:

echo $this->Html->link(
    '<div id="notescover"><p class="text">' . $viewnotes['Note']['notes'] . '</p></div>', 
    array('controller' => 'notes', 'action' => 'view', $viewnotes['Note']['id']),
    array('class' => 'light_blue', 'escape' => false)
);

You can put anything you like into the first parameter of Html::link(), but if you are adding markup in there, and not just plain text, it's important to include the escape => false attribute on the link, to avoid rendering the HTML special characters in your link as text.

Although, in my opinion, things like this become so convoluted when you use the Cake HTML helpers for anything that isn't simple, it's not even worth the time and effort to come up with code like this. It also makes reading the code way more complicated than it needs to be, especially when you have to decipher the code later down the line.

Upvotes: 4

Related Questions