|
Posted by Satyam on 03/30/05 18:27
This is just an idea I'm (quite unsuccessfully) been working on.
It appears to me that PHP sits between a database and a web browser.
On the database side, something I would love to have is embedded SQL, as it
exists in some other languages such as C (for example, see:
http://www.csee.umbc.edu/help/oracle8/server.815/a68022/sql.htm)
At least, what the minimum I would like is to have the very same variable
types all the way from the database end to the browser end.
Anyway, Embedded SQL is to much to ask. What I was working on, though I got
nowhere due to my lack of experience with Yacc and Lex is to have HTML or
XML directly as part of the language (I will just say XML from now on, to
indicate both HTML and XML) .
You see, both PHP and XML are block structured. Nevertheless, when we
program PHP, we only see one set of blocks, those enclosed by curly
braces{}. Most IDEs even check those for pairing, and we have Code
Beautifiers to indent them properly. The other set of blocks, those
enclosed by XML tags are invisible to the program, though keeping track of
those blocks is just as important.
Moreover, logic indicates that one set of blocks is compatible with the
other. That is, if you are generating an XML block within an if (condition)
block, the XML block has to be closed, you can't just open it and leave it
open. When the if() block closes, the XML block has to as well. Can you
point any case when this might not be so? Don't bother, I can give you
examples myself, but good programming practice can straigten that code out
nicely.
So, this is what I thought. I probably re-invented the wheel and if anybody
can point me to the source, I'll be just as happy.
I would like to see a couple of additions to PHP, which could very well be
implemented as a pre-compiler, in
the form of two new instructions, TAG and ATT which could also be
abbreviated to < and @.
The following piece of code show how to use them:
<p {
@align 'center';
echo 'este es el texto del pαrrafo';
}
Which would produce, after precompiled:
echo '<p align="center">este es el texto del pαrrafo</p>';
Just in case you wonder, yes, the text is in Spanish and it is alread
optimized in the sense that all the literal string segments that could be
strung together has been put in just one string in just a single call to
echo.
So, the TAG or < instruction would be followed by a valid tag name and then
a statement. The statement, as usual, can be replaced by a statement block,
enclosed in curly braces. I don't see any need for the tag name to be
enclosed in any kind of quotes. A variable instead of a literal tag name
should also be valid. Thus:
$p = 'p';
<$p { etc. }
would do the same. This syntax would prevent functions as a source of tag
names, which would be a very rare case and taking it under consideration
would really make the whole thing too cumbersome.
The ATT (attribute) or @ instruction is to be followed by a valid attribute
name and an expression providing the value. The attribute name can be the
value of a variable, so that
$attribute = 'align';
@$attribute 'center';
Once again functions cannot be used.
Why all these?
Basically, I don't want to keep track manually of how my XML blocks are
build.
Even a Code Beautifier would be able to handle the proper nesting of XML
(with these instructions) along PHP. The code would simply look good, and
that means it would be easy to get it right. Actually, while re-writing the
example code below, the brace matching in my IDE caught a missing curly
brace in one <input instruction.
I would also add a declarative statement to handle XML validation. The
declaration would have a reference to a DTD or Schema that describes the XML
to be generated. Optionally, the declaration would also give an xpath
string which would indicate what part of the schema is being validated. This
would allow the declaration to be used in functions or classes where only
fragments of the full schema are being generated.
And, by the by, I wouldn't mind the precompiler to take ? as a synonym for
echo.
Please notice that I am not trying to solve any specific programming
problem, and yes, I could use templates (actually I do) and I could do a
library of functions to echo XML without my actually having to assemble the
XML strings. It doesn't matter how many layers you put in between your
application and your XML strings, in the end, at some point, you have to
echo some XML. It is not a problem I am trying to solve, it is a feature I
would like to see.
Just an example of a more substantial piece of code:
<table {
<tr {
<th {
@rowspan 2;
@valign "top";
// Here I am using the ? instead of echo
? 'Dirección';
}
<td {
<input {
@name 'Direccion1';
@size 50;
@value $row['Direccion1'];
}
}
}
// notice the use of curly braces is just as optional
// as anywhere else in PGP
// This might be not propper coding practice, but it's up to the coder.
<tr <td <input {
@name 'Direccion2';
@size 50;
@value $row['Direccion2'];
}
<tr {
// Same thing here. There is no need to put curly braces in the TH
and TD tags
// but it is in the previous TR and the following INPUT
<th 'Cσdigo Postal';
<td <input {
@name 'CodPos';
@value $row['CodPos'];
}
}
}
So, this is what I had in mind and, though I tried to get somewhere with
Bison and Flex, the results have not been good, it is way over my abilities.
Thus, I though that someone is sure to know way more than I do and might be
interested.
Cheers
Satyam
Navigation:
[Reply to this message]
|