Jump to content

new line \n in template?


Manfred62
 Share

Recommended Posts

Tried to make the source (html) looking cleaner. In the code of a repeater template this doesn't work:

<?php echo "{$page->adresse_detail}<br />\n"; ?>

but this does work:

<?php echo "{$page->adresse_detail}\n<br />"; ?>

Could anyone explain this? I'm on Win7 and XAMMP.

Link to comment
Share on other sites

What does not work with your first line of code? I'm on Windows 7 as well and everything works as expected:

template:

<?php echo "{$page->title}<br />\n"; ?><?php echo "{$page->title}<br />\n"; ?>
<?php echo "{$page->title}\n<br />"; ?><?php echo "{$page->title}\n<br />"; ?>

output in 'view source', in IE, FF and Chrome, all the same:

title<br />
title<br />
title
<br />title
<br />
Link to comment
Share on other sites

thanks for the link.

tested the following:

<?php echo nl2br("$page->adresse_detail\n"); ?>
// html output:
first line<br />next line<br />third line<br />

there's no need for the <br> tag, php does it.

But new line is only working inside a string. For example, this string shows a new line in the source:

<?php echo nl2br("$page->adresse_detail\n "); ?>
// html output:
first line<br />
 next line<br />
 third line<br />
 

always nice to learn something new..

Link to comment
Share on other sites

The output of a template is ran through the PHP function trim(), which removes whitespace characters surrounding strings (start and end). \n is one of those characters.

That's right. So if "{$page->adresse_detail}<br />\n" is literally the only thing being output by the template file, then that trailing "\n" will be stripped. But in the context of a larger HTML document, you wouldn't see the "\n" stripped. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...