This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » General discussion » <?php echo $var ?> versus <?= $var ?> in templates?
<?php echo $var ?> versus <?= $var ?> in templates? [message #1465] Sun, 01 January 2006 09:15 Go to next message
isaac  is currently offline isaac
Messages: 4
Registered: January 2006
Location: Dunedin, Otago, New Zeala...
Junior Member

<?php echo $var ?> versus <?= $var ?> in templates?[ 5 vote(s) ]
1.<?php echo $var ?> 4 / 80%
2.<?= $var ?> 1 / 20%
3.no preference 0 / 0%

Rather than the longish and somewhat cumbersome <?php echo $var ?> in templates PHP supports the shorthand equivalent <?= $var ?> i.e. <?= is a tag for "echo value of containing statement" and as far as I know only works for single statements e.g. you can't do a <?= $var; $var ?> although you can do pretty much anything else including the use of functions that return a value.

I just posted this as a comment here http://www.symfony-project.com/content/book/page/view.html when I realised there may be a reason why the people behind Symfony don't use / recommend this? Is it bad practice like the <? and <% short tags? And if so why?

I havn't been using the Symfony framework for long but I must say its beyond excellant, I'm really excited to see how far this project will go. From what I've seen I'd sooner believe that people who wrote something such as Symfony are not using <?= by choice rather than lack of knowledge of it.

Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #1466 is a reply to message #1465 ] Sun, 01 January 2006 11:16 Go to previous messageGo to next message
simonbun  is currently offline simonbun
Messages: 205
Registered: October 2005
Faithful Member
The use of <?= $var; ?> is enabled through a setting in php.ini. Thus it is not supported on all servers, while <?php echo $var; ?> is. Thats why its the more compatible thing to do.

All in all i think the symfony sourcecode should keep the long tags for compatibility, but I prefer to use the shorthand tags in my own code as well. Especially in the views.

If it turns out some server doesn't support it, its always possible to do a global replace of '<?=' by '<?php echo'.

[Updated on: Sun, 01 January 2006 11:17]

Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #1469 is a reply to message #1466 ] Mon, 02 January 2006 00:54 Go to previous messageGo to next message
Ian  is currently offline Ian
Messages: 383
Registered: October 2005
Location: Boulder, CO (USA)
Faithful Member
I think <?= is just plain bad coding practice.


Ian

Join us on IRC for LIVE support!
Report bugs here.
Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #1474 is a reply to message #1469 ] Mon, 02 January 2006 04:28 Go to previous messageGo to next message
psylence  is currently offline psylence
Messages: 10
Registered: December 2005
Junior Member
acidbox wrote on Sun, 01 January 2006 17:54

I think <?= is just plain bad coding practice.


Assuming you have control of the server, how is typing '=' worse than 'php echo'? To me, seeing <?= means I'm seeing output, not processing, it's more descriptive.

If you don't have control of the server, <?= isn't really bad practice so much as stupid because you will run into problems eventually.

[Updated on: Mon, 02 January 2006 04:28]

Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #1475 is a reply to message #1465 ] Mon, 02 January 2006 05:23 Go to previous messageGo to next message
Ian  is currently offline Ian
Messages: 383
Registered: October 2005
Location: Boulder, CO (USA)
Faithful Member
Not to mention that <? is depreciated because of XML conflicts


Ian

Join us on IRC for LIVE support!
Report bugs here.
Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #1483 is a reply to message #1474 ] Mon, 02 January 2006 18:55 Go to previous messageGo to next message
fabien  is currently offline fabien
Messages: 484
Registered: October 2005
Faithful Member
Administrator
We don't use <?= in the symfony core because of compatibility with all settings.

But If you want to use <?= in your own templates, I think it is easier to read, so go with it.

I use it all the time in my own projects Embarassed
Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #1522 is a reply to message #1483 ] Tue, 03 January 2006 20:42 Go to previous messageGo to next message
jimbo  is currently offline jimbo
Messages: 17
Registered: November 2005
Junior Member
I use "<?=" because it is syntactic sugar. It is easier for me to read, write and maintain. I didn't know about the compatabilities problems though.
Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #1530 is a reply to message #1465 ] Wed, 04 January 2006 05:11 Go to previous messageGo to next message
isaac  is currently offline isaac
Messages: 4
Registered: January 2006
Location: Dunedin, Otago, New Zeala...
Junior Member
Quote:

Not to mention that <? is depreciated because of XML conflicts


Quote:

I didn't know about the compatabilities problems though.


Yes "<?" conflicts with XML but I don't think "<?=" does? For non-echo statements I would still use "<?php". Please corrent me if I am wrong. Thanks for all your thoughts in any case.
Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #20769 is a reply to message #1465 ] Thu, 01 February 2007 17:43 Go to previous messageGo to next message
TomTom101  is currently offline TomTom101
Messages: 10
Registered: November 2006
Junior Member
The first thing I did was install the sfSmartyView plugin.

Using PHP code in templates renders the whole idea of templating useless. It's supposed to provide tokens that can be replaced with data from the model, exclusively controlled by the controller.

I'd rather expect a discussion about which template engine should be used (smarty, ezTemplate, XSL) or whether to build a new symfony templating engine.

Advocating Smarty which does provide contoll mechanisms might seem contradictory to what I'm saying above, but I think it's the best compromise and offers the best from both worlds.

Without the Smarty plugin I would not have taken Symfony into consideration.

Best,
Thomas
Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #20770 is a reply to message #1465 ] Thu, 01 February 2007 18:11 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
Quote:

Using PHP code in templates renders the whole idea of templating useless.

This is a matter of opinion. Only values that are intended for the presentation layer are exposed to that layer in symfony, which a definite benefit and is certainly not useless. True, a 'proper' templating system will offer you that plus a greater degree of protection against bad work in templates, but there is a slight performance drop to be paid.

I guess for design shops, where designers and non-programmers need to amend templates, there may be a demand for an extra layer of templating. For us, the symfony system is ideal: only PHP programmers will be touching the output layer anyway.


Remember Palestine
Re: <?php echo $var ?> versus <?= $var ?> in templates? [message #21412 is a reply to message #1465 ] Mon, 12 February 2007 01:34 Go to previous message
jfcaouette  is currently offline jfcaouette
Messages: 21
Registered: May 2006
Junior Member
I'd tend to agree with halfer. PHP is itself a rather well-endowed templating engine. Adding a "PHP templating engine" on top of that just means wrapping PHP into a new toy language, hence adding another parsing round for the sake of being able to render your variables with a different syntax. Of course, as was pointed out, if you have web-designers without any PHP knowledge working on the templates, then maybe it's better to isolate them inside the template layer by not giving them direct access to PHP functions. But that's the one good reason I would see.

Conceptually speaking, separation of concerns doesn't simply equate separation of PHP and HTML. It's about keeping the business and presentation logic where they belong. You can do that just as well with PHP as with a templating engine. Smarty is in fact so powerful that you can still break MVC in your templates while using it. Razz Now that makes the whole idea of templating useless. The article below states it well, I think:

http://www.massassi.com/php/articles/template_engines/

Just my personal opinion. I appreciate the fact that symfony does not force upon us the addition of the extra templating layer. I'm personally glad (because there are people who will really need it) the sfSmarty solution exists as a plugin, and not as part of the symfony core.

[Updated on: Mon, 12 February 2007 02:04]

Previous Topic:Use of Peer classes
Next Topic:Multiple File Upload in Backend
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software