|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Parsing ASP.NET templates with PHP
We are finally there folks (once and for all)! A pre-alpha example of parsing ASP.NET templates with PHP. This is really credit to phpHTMLLib.
Here's something like an ASP.NET template; Code:
<a id="phpLink" href="http://www.php.net" runat="server"> <img id="myImg" src="http://static.php.net/www.php.net/images/php.gif" runat="server"></img> </a> <p>This is a test<br /> <a id="mySQLLink" href="http://www.mysql.com" runat="server">MySQL</a> Here's a PHP script that parses it; PHP Code:
Here's the HTML I get back; Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<title>ASP.NET to PHP!</title>
</head>
<body>
<a id="phpLink" href="http://www.php.net" alt="PHP">
<img id="myImg" src="http://static.php.net/www.php.net/images/php.gif" alt="PHP">
</a>
<p>
This is a test
<br /="1">
<a id="mySQLLink" href="http://www.mysql.com" alt="MySQL" style="background-color: yellow">MySQL</a>
</body>
</html>
PHP Code:
Anyway - I've attached what I've got as a ZIP. The approach I've used to parsing is using SAX filters, something I did before here. The parser itself is PEAR::XML_HTMLSax (http://pear.php.net/package-info.php?pacid=203) which you'll need installed to run the example. The advantage is this approach works but it's not that fast. A better alternative, once it's evolved a little, would be HTML_Template_Flexy. You also need to install phpHTMLLib obviously. The point of doing this is (arguably) the best web page design tools out there are coming from Microsoft right now, webmatrix being free to use. Also phpHTMLLib is a really excellent PHP class library but at the moment, it's not intregrated with any template engine, which means building web pages has to be done in pure PHP - note something which pleases many designers. phpHTMLLib cf ASP.NET system.web.ui Namespaces What's most fasincinating is phpHTMLLib is actually richer in some areas than ASP.NET. Aside from being able to construct HTML and XHTML there's also WML and SVG support in there. If you compare the .NET system.web.ui.htmlcontrols namespace with the children of phpHTMLLib::HTMLTagClass, phpHTMLLib is far ahead, supporting basically all HTML tags. Comparing the system.web.ui.webcontrols namespace with children of phpHTMLLib::BaseWidget, ASP.NET has alot more to offer (but that's not hard to change). Particularily interesting about phpHTMLLib is the DataList class, which shows the possibilities to reproduce some of the ASP.NET webcontrols like DataGrid. Anyway this is all hacked code right now. More as it happens. |
|
|
|
|
|
#2 | |
|
Sidewalking
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2001
Location: Philadelphia, US
Posts: 2,241
|
Quote:
![]() If I were to start using that, I'd probably have a textarea for each "widget" for entering HTML + a simplified template language. That would get parsed and turned into "widget" class definitions. Of course you'd need to add syntax for including other widgets, and well it's starting to sound sort of like Smarty, isn't it? Regardless, tremendous work, and I could really see this go somewhere useful. |
|
|
|
|
|
|
#3 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Quote:
This probably isn't making much sense right now but it's not going to take me long to put together an example which really shows it off. |
|
|
|
|
|
|
#4 |
|
Sidewalking
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2001
Location: Philadelphia, US
Posts: 2,241
|
Ah, I got lost between this bit
PHP Code:
|
|
|
|
|
|
#5 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
To quote Sun Tzu: "Know your enemy. Know yourself"
(in other words I have to confess to owning a copy of ASP.NET in a Nutshell).Basically the real "parser" is this file: require_once('SaxFilters/HTMLFilter.php'); Every time it encounters a tag with the attribute runat="server" is instantiates the corresponding phpHTMLLib "widget" and makes that widget available in the $widgets array (treating it as a global variable - another hack but perhaps a necessary one). The index it assigns to the object in the $widgets array is determined by the id attribute of the tag. Once it's finished parsing the page, you have the possibility of making further modifications to the "widget" as in the that example. That means you can respond to incoming "events" within your code e.g.; PHP Code:
|
|
|
|
|
|
#6 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
At the moment I can't see myself having a use for this;
Although well done anyway for attempting it; and getting it to run in PHP; ![]() That's not to say I will never use it though; who know's what's around the corner... One day I may have to use a .NET template ? This'll certainly help a lot from point of view of using PHP. Great Stuff ![]() |
|
|
|
|
|
#7 | |
|
32,817
![]() Join Date: Jun 2001
Location: Toronto, Canada
Posts: 10,496
|
Just out of curiosity, how would the above interpret something like this?
Code:
<asp:Calendar id="cal" SelectionMode="DayWeekMonth" ShowGridLines="true" ShowNextprevMonth="true" CellPadding="7" CellSpacing="5" DayNameFormat="FirstTwoLetters" FirstDayOfWeek="Monday" NextPrevFormat="CustomText" NextMonthText="Next >" PrevMonthText="< Prev" onSelectionChanged="SelectionChanged" onDayRender="DayRender" onVisibleMonthChanged="VisibleMonthChanged" DayHeaderStyle- BackColor="Black" DayHeaderStyle-ForeColor="White" DayHeaderStyle- Font-Name="Arial Black" runat="server"> <DayStyle BackColor="White" ForeColor="Black" Font-Name="Arial" /> <NextPrevStyle BackColor="DarkGray" ForeColor="Yellow" Font-Name="Arial" /> <OtherMonthDayStyle BackColor="LightGray" ForeColor="White" Font-Name="Arial" /> SelectedDayStyle BackColor="CornSilk" ForeColor="Blue" Font-Name="Arial" Font-Bold="true" Font-Italic="true" /> <SelectorStyle BackColor="CornSilk" ForeColor="Red" Font-Name="Arial" /> </asp:Calendar> Quote:
As such, I think this is great, if it works properly (which is why I posted the example above, which allows designers to code teh templates as they see fit without worrying about the end HTML, obviously a good thing as it allows folk to versioning up to XHTML or across to another language unlike HTML at all someday without any issues whatsoever). Anyways, I'm just curious as I've never seen this in action [img]images/smilies/smile.gif[/img] J Last edited by M. Johansson; Nov 20, 2003 at 09:19. |
|
|
|
|
|
|
#8 |
|
Talk to the /dev/null
![]() ![]() ![]() Join Date: Mar 2001
Location: Tampa, FL
Posts: 377
|
hey uhm harry why the heck cant you use my SAX code?
its fast and uses SAX style callbacks, I just never finished the extension return system. |
|
|
|
|
|
#9 | ||
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Quote:
Will take another look at it promise (never really got round to it I'm afraid).Quote:
The example you gave. Well phpHtmlLib doesn't have a calendar widget right now (that would probably be a tough one to implement) but in terms of what the template parser would do with your example (assuming a calendar widget existed); The example again; Code:
<asp:Calendar id="cal"
SelectionMode="DayWeekMonth"
ShowGridLines="true"
ShowNextprevMonth="true"
CellPadding="7"
CellSpacing="5"
DayNameFormat="FirstTwoLetters"
FirstDayOfWeek="Monday"
NextPrevFormat="CustomText"
NextMonthText="Next >"
PrevMonthText="< Prev"
onSelectionChanged="SelectionChanged"
onDayRender="DayRender"
onVisibleMonthChanged="VisibleMonthChanged"
DayHeaderStyle-BackColor="Black"
DayHeaderStyle-ForeColor="White"
DayHeaderStyle-Font-Name="Arial Black"
runat="server">
<DayStyle
BackColor="White"
ForeColor="Black"
Font-Name="Arial" />
<NextPrevStyle
BackColor="DarkGray"
ForeColor="Yellow"
Font-Name="Arial" />
<OtherMonthDayStyle
BackColor="LightGray"
ForeColor="White"
Font-Name="Arial" />
<SelectedDayStyle
BackColor="CornSilk"
ForeColor="Blue"
Font-Name="Arial"
Font-Bold="true"
Font-Italic="true" />
<SelectorStyle
BackColor="CornSilk"
ForeColor="Red"
Font-Name="Arial" />
</asp:Calendar>
PHP Code:
With phpHtmlLib the API is different - in fact you add attributes with generic methods like set_tag_attribute() and set_tag_attributes() which allows you to pass an array. The calendar control would be an extension of phpHtmlLibs BaseWidget, this being something equivalent to the system.web.ui.webcontrols namespace although as you can see, phpHtmlLib doesn't have anywhere near as many widgets as ASP.NET has available. Thats where most of the effort would have to go. Phew. Rant over. Baby calls! |
||
|
|
|
|
|
#10 |
|
32,817
![]() Join Date: Jun 2001
Location: Toronto, Canada
Posts: 10,496
|
Cool
I wasn't picking on particular widgets, was just curious Almost seems the opposite of what you'd be looking for, going back to a previous templating discussion...With templates you're looking for the designer to have the ability to control things without worrying about code. Doesn't the above example stop this from happening? I guess ultimately it would be to PHP's benefit if you could simply assign a PHP file to parse out .NET files. J |
|
|
|
|
|
#11 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Quote:
And of the template markups I've seen, ASP.NET is by far the most attactive, being fundamentally HTML will minor add ons and no "control logic". I.e. none of this; Code:
{if:foo=bar}
Hello World!
{endif}
Anyway - really have to stop before I end up as a very irresponsible father. |
|
|
|
|
|
|
#12 |
|
32,817
![]() Join Date: Jun 2001
Location: Toronto, Canada
Posts: 10,496
|
Interesting cross-linkage *L*
J |
|
|
|
|
|
#13 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 848
|
Quote:
|
|
|
|
|
|
|
#14 |
|
"Of" != "Have"
![]() Join Date: Jan 2003
Location: Calgary, Canada
Posts: 2,060
|
I was working on a very similar template engine a little while ago, however tags had server id's instead of the runat="server", so if a tag had a server:id="", it would create a new object which could control the html within that object, add html, change attributes, etc.
I never really did finish it though. It was going to be version 2 of my Template-X template parser... Maybe I'll look into it again sometime in the future... Anyways: sounds cool Harry. |
|
|
|
|
|
#15 |
|
SitePoint Zealot
![]() ![]() Join Date: Dec 2002
Location: Colorado, USA
Posts: 142
|
I have used phpHtmlLib for a number of projects and I love it. The only problem I have is that other people find it hard to learn, and the designers that I work with can't use it very easily.
This approach is very interesting, do you think it would be possible with just HTML instead of using ASP.NET? I have been analyzing your code, and it would seem that this would be very easy, but I have to admit, the Sax stuff is a little over my head. I would guess that the possibilities could also extend beyond this, as in using phpHtmlLib to create PHP-GTK projects, or XUL documents. phpHtmlLib is very extensible, and with it's new Form classes, it has become a very useful and timesaving tool for me. I am excited to see what other uses it has in store in the future. |
|
|
|
|
|
#16 |
|
SitePoint Enthusiast
![]() Join Date: Jan 2003
Location: San Diego
Posts: 58
|
I had a parser that looked for specific XML tags, and would replace the tag with PHP. For example:
PHP Code:
PHP Code:
PHP Code:
|
|
|
|
|
|
#17 | |
|
SitePoint Zealot
![]() ![]() Join Date: Jun 2002
Location: Brazil
Posts: 142
|
Quote:
Code:
{if:image}
<table border="0"><tr><td align="center">{image}</td></tr></table>
{else}
<p align="center"><font color="red">No Image</font></p>
{endif}
Hope you understood... my english is terrible. ![]() Last edited by marcoBR; Jun 7, 2003 at 13:42. |
|
|
|
|
|
|
#18 |
|
32,817
![]() Join Date: Jun 2001
Location: Toronto, Canada
Posts: 10,496
|
In ASP.NET you have DataRepeaters and stuff, so you'd just instantiate the DataRepeater and in your code-behind do the actual logic.
.NET highly encourages separation of logic from design, and even business from presentational logic. So, place your design elements, and .NET can do the rest. In reality, so long as you table is properly named, the code-behind can generate stuff into it, but the DataRepeater is generally more widely used ![]() J |
|
|
|
|
|
#19 | ||
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Quote:
The ASP.NET specific syntax is not a requirement as such (although it would allow us to use the .NET tools) but some extra syntax, over HTML, would be needed for implements controls (widgets) that make things like generating tables, calendars etc. simpler. Quote:
That example Marco gives is "doable" with this but in a different way; Code:
{if:image}
<table border="0"><tr><td align="center">{image}</td></tr></table>
{else}
<p align="center"><font color="red">No Image</font></p>
{endif}
Code:
<table id="imageTable" border="0" hidden="true" runat="server">
<tr>
<td align="center">
<img id="myImage" runat="server">
</td>
</tr>
</table>
<p id="noImage" align="center" runat="server"><font color="red">No Image</font></p>
Now your code which "manipulates" this template might look like; PHP Code:
And does a designer really want to care if there is an image or not? The designers brief may be just "create a template for an image and another for when there's no image". |
||
|
|
|
|
|
#20 |
|
SitePoint Zealot
![]() ![]() Join Date: Dec 2002
Location: Colorado, USA
Posts: 142
|
This might be a little bit off the subject, but one technique that the typo3 project does that I really like is they have a 'module' built into their CMS that parses an HTML file, and uses html comment tags to decide where the dynamic PHP data is inserted.
For example: Code:
<html>
<head>
<title><!-- TITLE BLOCK --></title>
</head>
<body>
<!-- CONTENT BLOCK ONE -->
</body>
</html>
If you could parse this html document into a phpHtmlLib object, insert the content in the correct places, then output it correctly, I think that you would be onto something. I see this as a very similar project compared to what Harry has done up above, but just takes the ASP.NET element out of it. This would allow the designer to create pages with Dreamweaver, or whatever, place them into a directory where they were parsed, and the code would take care of the rest. I realize that the whole point of this thread was to try and tie ASP.NET together with PHP, and I think that's great, but getting my designers to learn ASP.NET, AND try to get them to use different tools, AND try to get them to understand the entire ASP.NET widget set is slightly unrealistic. Just trying to use your ideas to bring the entire thing a little closer to home. References: Typo3 Tutorial: Modern Template Building |
|
|
|
|
|
#21 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Interesting. From a quick glance seems the typo template doesn't resort to placing control logic in the template which is good but you still have to populate a bunch of variables which will be inserted into the template.
The approach here is a little different. The template is what defines which "code behind" classes get loaded as the template is parsed. After parsing, the instantiated objects are available to your script for further modification. Although I'm aiming at ASP.NET syntax, there's nothing to prevent this approach being used for something phpHTMLLib specific. Certainly the widgets currently available in phpHTMLLib are different to ASP.NET's webcontrols and would require a different syntax to instantiate them. |
|
|
|
|
|
#22 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Osnabrück
Posts: 1,003
|
On a somewhat related thing, I found something similar to this for Java as well: Tapestry. It uses a very similar concept with "jwcid" tags.
Tapestry can be found here: http://jakarta.apache.org/tapestry I have installed it and am currently looking into it and it also seems as a very nice to keep the logic out of the templates. |
|
|
|
|
|
#23 |
|
SitePoint Zealot
![]() ![]() Join Date: Jun 2002
Location: Brazil
Posts: 142
|
Nice post Chris! Really Tapestry has an interesting way to maintain logic completely separeted from template and it seems easier for designer learn it.
Certainly i'll review my template system to adopt Tapestry concepts. ![]() |
|
|
|
|
|
#24 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Turns out (after fascinating discussion I had at the weekend) the Sun have caught on to what MS has done with ASP.NET: Java Server Faces. From this OReilly article, here's a JSF page;
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> **<head> ******<title>A Simple JavaServer Faces Application</title> ** </head> ** <body> ******<%@ taglib uri="http://java.sun.com/j2ee/html_basic/" prefix="faces" %> ******<font size="4">Please enter your name and password</font> ****** ******<faces:usefaces> ******** <faces:form id="simpleForm" formName="simpleForm"> ************<table> ************** <tr> ******************<td>Name:</td> ******************<td><faces:textentry_input id="name"/></td> ************** </tr> ************** <tr> ******************<td>Password:</td> ******************<td><faces:textentry_secret id="password"/></td> **************</tr> ************</table> ************<p><faces:command_button id="submit" commandName="Log In"/> ******** </faces:form> ******</faces:usefaces> ** </body> </html> More at http://www.jamesholmes.com/JavaServerFaces/ |
|
|
|
|
|
#25 |
|
32,817
![]() Join Date: Jun 2001
Location: Toronto, Canada
Posts: 10,496
|
Interesting, Hommage or Inspiration?
![]() J |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 05:29.
















Linear Mode
