equivalent to Ruby's -%>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lucassus
Forum Newbie
Posts: 3
Joined: Wed Apr 16, 2008 2:31 pm

equivalent to Ruby's -%>

Post by lucassus »

Hi all!

I want dynamically generate yaml file:

Code: Select all

 
Person_User:
 
    <? for ($i = 0; $i < 1; $i++) : ?>
    User_Test_<?=$i?>:
        login: test<?=$i?>
        email: test<?=$i?>@gmail.com
        role: admin
        zip_code: 44-776
        passwd: 1a1dc91c907325c69271ddf0c944bc72
        first_name: Michelle
        last_name: Briggs
        last_login_date: 2008-02-11 06:07:45
    <? endfor; ?>
 
When I execute this script from command line, I have output:

Code: Select all

 
Person_User:
 
        User_Test_0:
        login: test0        email: test0@gmail.com
        role: admin
        zip_code: 44-776
        passwd: 1a1dc91c907325c69271ddf0c944bc72
        first_name: Michelle
        last_name: Briggs
        last_login_date: 2008-02-11 06:07:45
 
Which is completely bad YAML synt
Reason: PHP don't care about white spaces after his tags.

How to avoid this unexpected behavior?
Has PHP equivalent to Ruby's -%>?
lucassus
Forum Newbie
Posts: 3
Joined: Wed Apr 16, 2008 2:31 pm

Re: equivalent to Ruby's -%>

Post by lucassus »

Fixed,

I have to add extra space after <? ?> tag. Why?
Is there more elegant solution?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: equivalent to Ruby's -%>

Post by Christopher »

I would recommend writing it out fully, rather than using any of the shorthand syntaxes. You won't have problems like you just encountered, it will be compatible with future releases, and more readable by all programmers.

Code: Select all

<?php echo $i; ?>
(#10850)
lucassus
Forum Newbie
Posts: 3
Joined: Wed Apr 16, 2008 2:31 pm

Re: equivalent to Ruby's -%>

Post by lucassus »

arborint wrote:I would recommend writing it out fully, rather than using any of the shorthand syntaxes. You won't have problems like you just encountered, it will be compatible with future releases, and more readable by all programmers.

Code: Select all

<?php echo $i; ?>
I've tried standard tags.. the same results :/
Always I have to add extra space.
It's quite annoying and it could produce lots of bugs in my code (I can't see a white space! ;) ).
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: equivalent to Ruby's -%>

Post by onion2k »

For those of us who don't use Ruby, what does "-%>" do?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: equivalent to Ruby's -%>

Post by Christopher »

lucassus wrote:I've tried standard tags.. the same results :/
Always I have to add extra space.
It's quite annoying and it could produce lots of bugs in my code (I can't see a white space! ;) ).
All of these work for me on PHP5:

Code: Select all

<?php echo $i; ?>
<?php echo $i;?>
<?php echo $i?>
(#10850)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: equivalent to Ruby's -%>

Post by Kieran Huggins »

onion2k wrote:For those of us who don't use Ruby, what does "-%>" do?
It preserves whitespace, though all the cool kids are using HAML instead of erb ;-)
Post Reply