cakephp 2.X换行无法正常工作

时间:2017-08-24 14:52:47

标签: php html cakephp newline cakephp-2.x

我尝试过使用html br标签,“\ r \ n”和PHP_EOL但我的表数据不会换行。我不明白为什么它只是将它附加到一行而不是回车。

以下是目前显示我的数据的图片:

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>

2 个答案:

答案 0 :(得分:1)

'escape' => false添加到您的链接选项以转义html字符。这样您就可以使用<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
        )
    );

答案 1 :(得分:1)

escape之类的选项可以在$options的{​​{1}}参数中传递,即第三个参数。第二个参数是仅用于URL的。

另请注意,禁用自动转义时,应手动转义相关部分以避免XSS。

HtmlHelper::link()

另请参阅 Cookbook > Core Libraries > Helpers > Html > HtmlHelper::link()