Page 1 of 1

equivalent to Ruby's -%>

Posted: Wed Apr 16, 2008 2:39 pm
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 -%>?

Re: equivalent to Ruby's -%>

Posted: Wed Apr 16, 2008 3:07 pm
by lucassus
Fixed,

I have to add extra space after <? ?> tag. Why?
Is there more elegant solution?

Re: equivalent to Ruby's -%>

Posted: Wed Apr 16, 2008 3:14 pm
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; ?>

Re: equivalent to Ruby's -%>

Posted: Wed Apr 16, 2008 3:19 pm
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! ;) ).

Re: equivalent to Ruby's -%>

Posted: Wed Apr 16, 2008 3:35 pm
by onion2k
For those of us who don't use Ruby, what does "-%>" do?

Re: equivalent to Ruby's -%>

Posted: Wed Apr 16, 2008 5:32 pm
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?>

Re: equivalent to Ruby's -%>

Posted: Thu Apr 17, 2008 2:13 am
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 ;-)