SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Posibble Templating "Solution" here

    Well I am going to release the source to a templating system I was working on but never finished. I would actually like to see PHP fully adopt this system in as native code or someone write it in as a standard C module.

    This templating system is entirely based upon the original system of all systems which is the JSP Tag Libraries, originally designed to not separate code from content, but to change the way the code in the content is used. This templating system makes perfect sense and please do not bother me with your rants about templating systems. The GOAL of templating systems is to make things easy for designers and encourage them to change the look of your site without much effort. This system goes on after the business logic is completed, meaning the designer has full control over the site and its logic, they will also be able to have access to extensions which allow them to do database queries and return the results or display an inline XML-RPC/RSS feed and regex the incoming data. The possibilities are expansive with this system and I strongly urge it become standard in PHP.

    Example:

    <c:out test="$myobject->variable"/>

    will output the contents of "variable" in $myobject

    or for the more complex things...

    <c:choose>
    <c:when test="$foo > 4">
    <b>The Variable foo is greater than 4</b>
    </c:when>
    <c:otherwise>
    <b>The Variable foo is less than 4</b>
    </c:otherwise>
    </c:choose>

    I referenced the JSTL (Java Standard Tag Library) for these examples and the ones inside. You can read and download the PDF on its specifications here.

    The zip file attached is a base system which loads a template and iterates line-by line combing through with a twin set of regex's. One to detect open and closed tags, futher parsing will allow you to call on specifics. The parsing is entirely based on SAX and is callback-oriented, not like DOM because its too slow for something like this(unless you can figure out something, DOM doesn't like abnormal syntax). I also included a base extension system which i never completed, but is a cut-down version of the extension system found in patXMLRenderer and is meant for creating your own tag libraries for expandability. This actually goes rather fast, I caught it going at about 35ms processing time over a 250 line template with 50 matches, not bad considering the overhead of smarty is well over 100ms at most times. The third regex will match all attributes and return them in an array, see regex.php for details.

    I have attached the LGPL license for this code and ask that anyone who brings this code into a working system credit myself and Stephan Schmidt. You don't need to call it chameleon, I just nicknamed it that because its a "skinning" system for a site. The reason i am releasing this code is so someone can continue where I left off and make it useful, some others have started projects similar to this and haven't completed this. Also, I ask that you STAY AWAY from .NET because the .NET templating system is a direct rip off of the JSP tag library system(don't believe me, check the date on the JSP tag library proposal) and I will not condone its support, ever.

    Enjoy folks, this took roughly a year to master with different regex tries and code changes to make the engine begin to work.
    Attached Files Attached Files
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  2. #2
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW I handed this source over to Harry, but I haven't heard from him in a while. Wonder how your doing with the system...

    Oh yeah, I ask anyone that improves this system to post their files on here for others to check out and try out, I would like to get the full backing of the Advanced devs here.
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally though - template arguments aside - I can't see a use for this, though if it's based on Java I suppose someone may well find a use for it 8)

    But that's just me - nowadays I use XML/XSL-T and I'm happy enough with that - I know it's not what you want to hear 8)

  4. #4
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No offense taken, and yes XML/XSL-T is another templating system I advocate. Its just a pain in the rump to learn...
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its just a pain in the rump to learn...
    It's a pain in the rump, period.

    The only reason you want to use XSLT is when you're converting some kind of complex datastructure to another kind of datastructure just once, like the contents of a database that needs to be upgraded.

    Spending thousands of CPU cycles to generate XML that conforms to some user-defined DTD, only to spend another million on converting that XML to something else again, like output a browser can understand, is just silly. At least it's completely beyond me.

    XML and XSLT are overhyped. Avoid them when you can. And that's coming from a full-time J2EE developer!

    Vincent

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe your dislike to XML/XSL-T is mainly due to using it via Java ?

    Java is known to be slow you know LoL

    But seriously though I would have to argue for using XML/XSL-T Making the comment that XML/XSL-T is only good for grabbing data from databases is benign really - there are 101 uses for this markup langauge.

    For PHP I use it to simplify Template designs from XML to xHTML though I suppose with another stylesheet I could output to Wap ? or maybe dump the resultant page to PDF me thinks...

    There is a use for it 8) You just need to find it that's all...

  7. #7
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Maybe your dislike to XML/XSL-T is mainly due to using it via Java ?

    Java is known to be slow you know LoL

    But seriously though I would have to argue for using XML/XSL-T Making the comment that XML/XSL-T is only good for grabbing data from databases is benign really - there are 101 uses for this markup langauge.

    For PHP I use it to simplify Template designs from XML to xHTML though I suppose with another stylesheet I could output to Wap ? or maybe dump the resultant page to PDF me thinks...

    There is a use for it 8) You just need to find it that's all...
    Java is slow on the desktop, but when it gets to large scale business logic apps, i think you would happily sacrifice a BIT of speed for ease of maintenance and development.

    Using XML as a database is only really good (in my opinion) when you have to use that data across different systems/languages/environments. In PHP it is quite easy to generate classes that output XHTML or PDF from small amounts of RDMS based data.

    I like the XML/XSLT system, but i agree with Vincent, in that there is too much overhead for things that don't really need it.

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, yes there wouldn't be much use for XML for example with the logical side of an application - but what about storage and distribution I wonder ?

  9. #9
    SitePoint Enthusiast Sasa's Avatar
    Join Date
    Mar 2003
    Location
    Cologne, Germany
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was wondering too, if I could use XSLT/XML in some useful way. The problem with the cpu cycles is not really that bad if results of calculations can be cached for future use. But then again I can only imagine it being used by people who want to write output filters (like mentioned above: pdf, flash, whatever) and who would these people be? Not the main developers? Maybe the desingers. So the designers will be XSLT/XML specialists and the developers will work with PHP and simply deliver XML to the desingers? I really wonder how probable this is. I myself am both in one person and the designer in me doesn't really have to learn a templating system.
    Sasa

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know about the developer/designer issue myself - I develop, not design - though I use XML/XSL-T to be applied to create xHTML from XML templates whilst using PHP/mySQL to insert dynamic content where needs be.

    Though for example, I could later have a stylesheet to transform the complete XML string my classes generate to PDF to create elements of a manual for example - a xHTML page (from the XML string) could be put into a PDF document to explain what carrying out a certain action will result in/look like in appearance.

    I'm a simple person really and I just do what I need to do to get by with LoL

  11. #11
    SitePoint Enthusiast Sasa's Avatar
    Join Date
    Mar 2003
    Location
    Cologne, Germany
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Dr
    Yeah, I think what Vincent meant is why to use the redundant step of first transforming your data to XML? If you are the developer why don't you directly program a module for transforming your output to PDF? Is that not easier than first creating something else? I like simple solutions too, but I don't see the simplicity in using this approach. I create my xhtml directly from php. XHTML already is XML (that is the reason they invented XHTML). Why take one XML file and transform it to another XML format. That totally looses me.
    Sasa

  12. #12
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've not explained things have I ? Oppss...

    In order

    XML Template -> Via XSL Stylesheet -> xHTML document

    Is how I use XML/XSL-T though I may for example use a differing XSL stylesheet to take a PHP string (XML well formed) and transform this string Via said XSL stylesheet using PHP to PDF, no ?

  13. #13
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    XML/XSLT Templating is probably *the* fastest templating system out there. I test ran a few templates and got some incredible results. 4ms or less over a single 80 line XML page and a 70 line XSL stylesheet that generated graphs. I think XML/XSLT is a definate winner, though I never touched it a year ago but I migth now since Adobe and Altova have made huge changes in their products to accomodate the huge boom in the use of XSLT. Their products such as GoLive and Stylevision make templating with XSLT a snap so I think from now on my site will endup using this instead because it has many possibilities. Mainly because XML isint just good for XSLT, but XSL can also help you transform your articles *easily* into a PDF if you wanted to with different sheets to takeon different document styles.
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  14. #14
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You - A round of applause for Theiggsta folks....

    At least someone agrees with me 8)

  15. #15
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I take back what I said on XSLT designing being a snap, GoLive has no native support for XSLT style designing and Altovas product confuses me more than helping me design an XSLT style. I guess until they make it as easy as drag and drop for XSLT design, people won't use it and I can see why, XSLT is extremely strict in syntax and is not easy to learn. Got the Wrox press book on it and its more like a language in of itself and I would rather not deal with it at all.
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  16. #16
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Booo....Booo....

    But seriously though I didn't know a thing about XSL-T either - I knew XML well formed - and after looking at a few stylesheets and some tutorials I started to learn more and more.

    I am still learning - it's not easy but I'll get there (eventually LoL). The biggest problem that I had to start with was that I didn't quite understand what <xsl:apply-templates /> did.

    Once I worked out that this meant I was on my way so to speak. Another thing I suppose that hinders/puts off a lot of people is X-PATH.

    Using X-PATH as in the sense of a directory path isn't so bad but it's the conditions/queries that you can use with X-PATH that I've still got some problems with.

    Shame you're not going to continue to learn XSL-T though cause apart from this you understand how XML/XSL-T can be used properly 8)

  17. #17
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The book I got makes it seem like a breeze, but I just cannot understand its extremely strict syntax. The error outputs don't help me either.

    Still don't understand the apply-templates tag, probably never will because its dodgy witht he way its explained anywhere, you learn one rule for it then another when you get down the road.
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  18. #18
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, for example you have the following:

    PHP Code:
    # your stylesheet
    .
    .
    <
    xsl:template match='bold'>
    <
    b>
    <
    xsl:apply-templates /></b>
    </
    xsl:template>
    .
    <
    xsl:template match='italics'>
    <
    i>
    <
    xsl:apply-templates /></i>
    </
    xsl:template>
    .

    Okay, forgetting about the root for now, you have 2 templates -

    Bold and
    Italics. You could have the following XML tag within an XML document:

    PHP Code:
    <bold>Hello World.</bold
    The above template (from the stylesheet example I've shown) would give

    HELLO WORLD

    In bold text, although the following would give both BOLD and ITALIC text:

    PHP Code:
    <bold><italic>HELLO WORLD</italic></bold
    Even though your not using the Italic template directly - if the stylesheet doesn't find another tag within <BOLD></BOLD> it'll just dump the text (or CDATA as it's known).

    On the point of the root of an XML document, here is what I have in an XSL stylesheet for the root:

    PHP Code:
    <xsl:template match='root'>
     <
    html
     <
    head>
      <
    title>Application Framework</title>
      <
    link rel='stylesheet' media='screen' href="../../../root/templates/styles.css" />
     </
    head><body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
      <
    xsl:apply-templates />
     </
    body></html>
    </
    xsl:template
    And now ALL the XML well-formed tags that I have within an XML document within the <ROOT></ROOT> tags will be transformed also.

    Hope this helps you ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •