hello world
hello world

Reputation: 316

cakephp 2.X newline not working

I've tried using html br tag, "\r\n" and PHP_EOL yet my table data will not line break. I don't understand why it just appends it to a single line instead of giving a carriage return.

Here's an image of how it's currently showing my data:

How it's currently formatting my data

<table>
    <tr>
        <th>Article</th>
        <th>Action</th>
    </tr>

    <?php
      foreach ($posts as $post):
    ?>
       <tr>
            <td>    
<?php
    echo $this->Html->link($this->Time->format($post['Post']['created'], '%d %b %Y', 'invalid') 
         . " - " . $post['Post']['article_title'] 
         . PHP_EOL . "<br />\n" . "\r\n" 
         . $post['Post']['article_link'], array(
        'controller' => 'posts',
        'action' => 'view',
        'inline' => false,
        'escape' => false,
        $post['Post']['id']
    ));
?>

            </td>

<td>
<?php
    echo $this->Html->link('Edit', array(
        'action' => 'edit',
        $post['Post']['id']
    ));
?>
<?php
    echo $this->Form->postLink('Delete', array(
        'action' => 'delete',
        $post['Post']['id']
    ), array(
        'confirm' => 'Are you sure?'
    ));
?>
           </td>
        </tr>
    <?php
endforeach;
?>
   <?php
unset($post);
?>
</table>

Upvotes: 1

Views: 249

Answers (2)

bill
bill

Reputation: 1656

Add 'escape' => false to your link options to escape html characters. This will allow you to use <br>.

    echo $this->Html->link($this->Time->format($post['Post']['created'], '%d %b %Y', 'invalid') 
         . " - " . $post['Post']['article_title'] 
         . PHP_EOL . "<br />\n" . "\r\n" 
         . $post['Post']['article_link'],
         array(
            'controller' => 'posts',
            'action' => 'view',
            'inline' => false,
            'escape' => false, // move this
            $post['Post']['id']
        ),
        array(
            'escape' => false // to here
        )
    );

Upvotes: 1

ndm
ndm

Reputation: 60463

Options like escape are ment to be passed in the $options argument of HtmlHelper::link(), ie the third argument. The second argument is ment to be used for the URL only.

Also note that when you disable automatic escaping, you should escape the relevant parts manually in order to avoid XSS.

echo $this->Html->link(
    $this->Time->format($post['Post']['created'], '%d %b %Y', 'invalid')
        . " - "
        . h($post['Post']['article_title']) // escape manually
        . "<br />"
        . h($post['Post']['article_link']),  // escape manually
    array(
        'controller' => 'posts',
        'action' => 'view',
        $post['Post']['id']
    ),
    array(
        'inline' => false,
        'escape' => false
    )
);

See also Cookbook > Core Libraries > Helpers > Html > HtmlHelper::link()

Upvotes: 1

Related Questions