Use Ruby-style tags in PHP
Tue 5 Jun 2007 by Rob RosenbaumOne undeniable advantage of Ruby on Rails is its terse template syntax. The cumbersome
<?php echo $something ?>
is replaced by the elegant and readable
<%= something %>
The very good news is that you can use these same tags in PHP! Just add the following to your htaccess:
php_flag asp_tags on
Voila! Readable templates. What's curious is that no one seems to use this option. Perhaps it is because they are referred to as "ASP-style tags." (Can we just call them "Ruby-style tags", instead?) On all my sites, I use the long tags for blocks of code, and the short <%= %> tags for any PHP code floating in the HTML sea. In symfony, that means the template files use one tag-style, and the rest of the code uses the other. I encourage everyone - yes, everyone - to start using Ruby-style tags in their templates. The more people use it, the more common it will be to have the "asp_tags" option on by default, so people on shared servers can join in the readable fun.
It's important for a second reason - arbitrary conventions should be standardized. That is, any time we are faced with a set of possibilities that are all of equal value - such as what weird punctuation our programming language should use to demarcate itself - we should pick one standard way and stick with it. That way we reduce the learning curve of all languages (or whatever the things the convention pertains to). Imagine if there were one, universal syntax for putting server-side code into HTML. Imagine if there were a templating language that every designer knew - because it's so simple - and that every server-side language supported. It would not be a universal programming language, since we should not be using the full power of a programming language from inside our templates; that's what the controller code is for. This universal templating language (What the hell, let's go ahead and call it UTL, because giving initials to computer-denizens makes them seem like real people) should support the following things, and probably nothing else:
- Variables, including objects and arrays
- if/else
- while
- foreach
- Very basic arithmetic and string operators
- No function or method calls
Make the dream a reality. Start by using Ruby tags.
4 Comments Add your own
1. halfer | October 17th, 2007 at 5:02 am
You can indeed use the short tags in PHP. However they're not enabled on every server, which is why the symfony team don't recommend this approach for code that is intended to be portable (eg if you're writing a plugin, or an application you intend to distribute). This can cause problems for users who don't have this kind of control over their htaccess file (some shared hosts can disable it).
There's nothing wrong with short tags though - if the programmer understands their limitations but wants cleaner code, they should use them.
2. Rob | October 21st, 2007 at 12:48 pm
halfer-
My article concerned ASP-style tags, not PHP short tags; the two are often confused, even on the PHP dev forum. Short tags create a syntactic conflict with XML, and should be avoided for that reason. ASP-style tags have no such problems. I believe the confusion between the two is the reason so few people use ASP-style tags (that and their name.)
You are correct that they are less portable, but that is itself due to their infrequent use. If they were the standard rather than the exception - and there is no reason they should not be - then every hosting company would enable them by default. It is for that reason that I recommend using them whenever feasible.
3. carlos | January 24th, 2008 at 9:53 am
PHP always let you use without activating nothing in your htaccess
4. Rob Rosenbaum | January 25th, 2008 at 9:12 am
carlos-
Ruby-style tags can also be activated in php.ini with the "asp_tags" option. In many environments, though, the user will not have access to php.ini, and will have to use the htaccess method. (This is the case for this server, for example.)
Leave a Comment
Please: No emoticons or excessive punctuation.Trackback this post | Subscribe to the comments via RSS Feed