Use Ruby-style tags in PHP

One 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:

  1. Variables, including objects and arrays
  2. if/else
  3. while
  4. foreach
  5. Very basic arithmetic and string operators
  6. No function or method calls

That last one may raise some eyebrows. Why would we not include functions and methods? Because we can call all those funcitons in the controller, and assign their return values to variables, which we then use in the template per number 1 above. Also, allowing function calls brings the capacity for arbitrarily complex logic into UTL, as well as coupling it more tightly with the mother language, both of which defeat the purpose of UTL.

Make the dream a reality. Start by using Ruby tags.

  1. halfer’s avatar

    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’s avatar


    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.