[PHPLIB-DEV] custom tags From: Rimon Barr (barr <email protected>)
Date: 01/17/00

Here's an idea I was tossing around in my head after working with PHP and
some other web scripting tools. I like PHP a lot. However, there are some
ideas in other environments that could be integrated into PHP with
relative easy and, in my opinion, great benefit.

The new JSP, as well as Roxen, allow the user to create custom tags. The
main advantage of doing this is separating out programming and style.

The "programmer" would write something like

  <errormessage>
    You forgot to enter a password
  </errormessage>

And the "style-master" would go ahead and define that <errormessage> has
to be in blinking red bold letters, and apply the appropriate pre- and
post-HTML.

Of course one could do this in PHP. One writes:
  <%=errormessage("You forgot to enter a password");%>

But what happens when the internal string gets messy? And in complex HTML
pages things quickly get messy. For example, every time you open a new
table cell you need to output font tags (but only for certain browsers is
it truly necessary!) It's nice to define a new <td> tag, called <td2> or
<_td> that does this for me, and applies some global constant font, which
I can easily change. Then I can forget about all these details and focus
on real application issues.

So how does Roxen do this? They have some language called Pike in which
they can define tags. Have a look at the manuals for Roxen and Pike at
http://www.roxen.org, to see some other really neat examples of how this
can be used. It's actually quite a powerful idea, and I urge you to read
the manuals. Pike is a nice language. It's a pity that they chose to make
Roxen an independent web server, and didn't create a module within Apache,
as the Perl and PHP communities did. They have some technical reasons (not
wanting to spawn threads for each request), but I'm not sure that the
performance issues outweigh inter-operability considering Apache is not
that bad!

Similarly, in JSP, tag behaviour is defined with methods. See
http://www.javasoft.com/jsp.

Tags can also have certain properties like whether the text inside is pre-
or post-processed, and what function is used to implement it.

I was thinking that it would be useful to have the same kind of
functionality in PHP. Maybe all tags that look like <_abc> could be
registered with some function. Registration would define their properties
and which function to call to process them. The processing function would
have to be of a fixed signature, and this could a string for what is
between the open and close tags, and perhaps more. And whenever the
interpreter sees this tag either in HTML mode, or within a string, (it's
important to allow these tags to be processes in both contexts, because
they are useful in both) the interpreter would see if there is a
registered function to execute, instead of just doing the default which is
emitting the text (for backwards compatibility). Of course, we would have
to do something special for empty tags, and there are a few minor
implementation and syntax details to iron out, but this is not a big deal.
Does all that make sense? I explain in more detail and clarity.

I was wondering if anyone had thoughts on this matter, and would find such
a feature useful. I personally feel that although it does not strictly add
functionality, it's a big timesaver and it cleans up a lot of ugly code.
Isn't that what PHP (and other scripting languages) are all about?

All the best,
Rimon.

-- 
  *
  |  Rimon Barr, Ph.D. student, Computer Science, Cornell University.
  |  Email: barr <email protected>
  |  WWW:   http://www.cs.cornell.edu/barr/
  |
  | "In spite of everything I still believe that people are really
  |  good at heart."        -- Anne Frank, The Diary of Anne Frank,
  |                            entry of July 15, 1944.
  +----

- PHPLIB Developers Mailing List. Send messages to <phplib-dev <email protected>>. To unsubscribe, send "unsubscribe" to <phplib-dev-request <email protected>> in the body, not the subject, of your message.