Search code examples
phphtmlcakephpnewlinecakephp-2.x

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>

Solution

  • 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()