简体   繁体   English

cakephp 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. 我尝试使用html br标签,“ \\ r \\ n”和PHP_EOL,但我的表数据不会换行。 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: 这是当前如何显示我的数据的图像:

当前如何格式化我的数据

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

Add 'escape' => false to your link options to escape html characters. 在链接选项中添加'escape' => false可以转义html字符。 This will allow you to use <br> . 这将允许您使用<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
        )
    );

Options like escape are ment to be passed in the $options argument of HtmlHelper::link() , ie the third argument. 诸如escape类的选项必须在HtmlHelper::link()$options参数中传递,即第三个参数。 The second argument is ment to be used for the URL only. 第二个参数是ment,仅用于URL。

Also note that when you disable automatic escaping, you should escape the relevant parts manually in order to avoid XSS. 另请注意,当禁用自动转义时,应手动转义相关部分,以避免使用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() 另请参见Cookbook>核心库> Helpers> HTML> HtmlHelper :: link()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM