SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29
  1. #1
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why isn't there a template engine that uses named entities as markers?

    Having a template like... which is perfectly valid XML/XHTML, and someone browsers can even display correctly.. (Opera)

    Code:
    <!DOCTYPE html [
    <!ENTITY title "Template Title">
    <!ENTITY url "http://www.example.org">
    ]>
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    		<title>&title;</title>
    	</head>
    	<body>
    		<a href="&url;">Home</a>
    	</body>
    </html>
    Parsing this is trivial now PHP5 has libxml.

    The template engine then could allow replacement values set of each of the named entities.

    Would still need a mechanisms to define loops etc, but for putting values into templates it seems ideal.

  2. #2
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some proof of concept code.

    NamedEntityTemplateCompiler takes a string or file, and "compiles" it into a array of strings & integers. Strings are static output, and integers are references to variable names.

    Extemely fast & easy run though and render a template.

    There are limitations in XmlReader & DOMDocument but combining the use of both seems to overcome them.
    Attached Files Attached Files

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    Would still need a mechanisms to define loops etc.
    That's a pretty important "but".

  4. #4
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    That's a pretty important "but".
    Don't think its that complicated.

    Just deciding on a means to identify blocks to loop. And then encode it into the integers maybe.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    Just deciding on a means to identify blocks to loop. And then encode it into the integers maybe.
    Sure, but then you're inventing a language. And then you might as well be using smarty or perhaps xslt, if you like w3c's standards.

  6. #6
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Sure, but then you're inventing a language. And then you might as well be using smarty or perhaps xslt, if you like w3c's standards.
    No... because the template would still render fine in a browser that replaces custom entities.

  7. #7
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought the whole template engine with in a template engine discussions were dead O.o... Is there any point to re-inventing a wheel if it will be worse than the original? ._.

    Unless your creating a template markup language to be some sort of standard to go between separate scripting/programming languages? That I could potentially understand.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check out PHPtal.

  9. #9
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyways simple looping over an array/iterator of rows was trivial, as expected.

    Now would just need a method of repeating (including 0) parts of the template based on template variables.

    So if there was no rows to display, to remove uls, tables and the like.

    Using ids, and and class names maybe the way to go for this.
    Attached Files Attached Files

  10. #10
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. Now can implement some presentation logic.

    Now can remove content from the template, based on its id, though this probably should be expanded to use class names too.

    PHP Code:
    if (empty($vars['people']))
       
    $vars['#people'] = 0
    Which translates to if have no people to display, then don't output the element with id of people.
    Attached Files Attached Files

  11. #11
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ctx2002 View Post
    check out PHPtal.
    PHPTal uses custom jibber for markers. Therefore cannot preview the template directly in a browser and get a reasonable rendition of it.

    The whole point of using named entities is to be able to get close to what some refer to as the Dreamweaver test

    Though as I don't use dreamweaver, I just want to be able to quickly preview the template in a browser as I'm developing it.

  12. #12
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "PHPTal uses custom jibber for markers. Therefore cannot preview the template directly in a browser and get a reasonable rendition of it."

    you can preview PHPTal template in browser with fake data, and on runtime, all those fake data will be replaced by real data. I have read your example code, PHPTal can do that too.

    for example: following code also pass Dreamweaver test.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title tal:content="title">
    my fake title
    </title>
    </head>
    <body>
    </body>
    </html>

    by the way, any PHPTal template will pass Dreamweaver test.

  13. #13
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ctx2002 View Post
    "PHPTal uses custom jibber for markers. Therefore cannot preview the template directly in a browser and get a reasonable rendition of it."

    you can preview PHPTal template in browser with fake data, and on runtime, all those fake data will be replaced by real data. I have read your example code, PHPTal can do that too.
    The example given on the PHPTAL page would not pass the dreamweaver test.

    http://phptal.motion-twin.com/

    Attributes are a custom expression format, and wouldn't be replaced.

    Code:
    <div class="item" tal:repeat="item itemsArray">  
        <span tal:condition="item/hasDate" tal:replace="item/getDate"/>
        <a href="${item/getUrl}" tal:content="item/getTitle"/>
      <p tal:content="value/getContent"/>
    </div>

  14. #14
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not use fake data? attribute will be replaced at runtime, not in the Dreamweaver.

    try following example:

    that yahoo.com link will be replace by empty space at runtime, so when you right click your mouse try to inspect html source code, there will not a link call yahoo.com

    <div class="item" tal:repeat="item itemsArray">
    <span tal:condition="item/hasDate" tal:replace="item/getDate"/>
    <a href="${item/getUrl}" tal:content="item/getTitle"/>
    <!-- this is an fake url, will be replace by PHPTal at runtime -->
    <a tal:replace="" href="yahoo.com">Click My Yahoo</a>

    <p tal:content="value/getContent"/>
    </div>

  15. #15
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ctx2002 View Post
    why not use fake data? attribute will be replaced at runtime, not in the Dreamweaver.

    try following example:

    that yahoo.com link will be replace by empty space at runtime, so when you right click your mouse try to inspect html source code, there will not a link call yahoo.com

    <div class="item" tal:repeat="item itemsArray">
    <span tal:condition="item/hasDate" tal:replace="item/getDate"/>
    <a href="${item/getUrl}" tal:content="item/getTitle"/>
    <!-- this is an fake url, will be replace by PHPTal at runtime -->
    <a tal:replace="" href="yahoo.com">Click My Yahoo</a>

    <p tal:content="value/getContent"/>
    </div>
    How is that going to work? You end up with two links when intending only to have one when viewing the template directly in a browser.

  16. #16
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    The example given on the PHPTAL page would not pass the dreamweaver test.

    http://phptal.motion-twin.com/

    Attributes are a custom expression format, and wouldn't be replaced.
    You can do:

    HTML Code:
    <div class="item" tal:repeat="item itemsArray">  
        <span tal:condition="item/hasDate" tal:replace="item/getDate"/>
        <a href="#" tal:content="item/getTitle" tal:attributes="href item/getUrl" />
      <p tal:content="value/getContent"/>
    </div>

  17. #17
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    You can do:

    HTML Code:
    <div class="item" tal:repeat="item itemsArray">  
        <span tal:condition="item/hasDate" tal:replace="item/getDate"/>
        <a href="#" tal:content="item/getTitle" tal:attributes="href item/getUrl" />
      <p tal:content="value/getContent"/>
    </div>
    Closer, but still wouldn't pass. Anything short of having the actual value in the correct attribute isn't going to guarantee it to render as intended. As CSS attribute selectors aren't going to work as intended.

  18. #18
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Latest iteration, can now repeat elements based on class name.

    Code:
    <span class="twice">2</span>
    PHP Code:
    $vars['.twice'] = 2
    Will output

    Code:
    <span class="twice">2</span><span class="twice">2</span>
    If multiple class names are used then the iterator count is multiplied out.

    Code:
    <span class="twice thrice">6</span>
    PHP Code:
    $vars['.twice'] = 2;
    $vars['.thrice'] = 3
    Would repeat the span 6 times.

    Only repeating elements with an id zero or one times will create valid output.

    Also moved the t:repeat attribute to the element to be repeated, which results in less whitespace being introduced.
    Attached Files Attached Files

  19. #19
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    Closer, but still wouldn't pass. Anything short of having the actual value in the correct attribute isn't going to guarantee it to render as intended.
    Not sure what you mean? If it's a variable, then you can't have the actual value in the template.

  20. #20
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Not sure what you mean? If it's a variable, then you can't have the actual value in the template.
    Ah I may misunderstood what PHPTAL does... tal:attributes replaces an existing attribute I presume. And in values meant dummy values that are similar to actual, so the template looks and behaves like it would when populated.

    Code:
    <a href="http://example.org" tal:content="item/getTitle" tal:attributes="href item/getUrl" />
    Alot of template engines would fail with something like...

    ent.xhtm
    Code:
    <!DOCTYPE html [
    <!ENTITY url "http://www.example.org">
    ]>
    <html xml:lang="en"	xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    		<title>Name test</title>
    		<style type="text/css">
    			a[href ^= 'http://'] { color: #f00; }
    		</style>
    	</head>
    	<body>
    		<a href="&url;">Home</a>
    	</body>
    </html>
    As they use some custom expression format, which doesn't get replaced when previewing the template, and therefore the style rule doesn't apply.

  21. #21
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. Yes, PHPTAL allows you to provide dummy values, which are then replaced when the template is run. Browsers would ignore the template-specific tags, and just show the placeholders. It's pretty nifty actually.

    I haven't had much use for template engines, for a while, but I did take a look at them recently and PHPTAL seems to be one of the few that allows something like this.

  22. #22
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "tal:attributes replaces an existing attribute I presume", that wrong, PHPTAL replace tag not attribute.

    copy from PHPTal manual:

    tal:replace can also be used to create samples in source templates, but remove them from final output.

    <table>
    <tr tal:repeat="item myresult">
    <td tal:content="item">item value</td>
    </tr>
    <tr tal:replace="">
    <td>sample 1</td>
    </tr>
    <tr tal:replace="">
    <td>sample 2</td>
    </tr>
    </table>

  23. #23
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ctx2002 View Post
    "tal:attributes replaces an existing attribute I presume", that wrong, PHPTAL replace tag not attribute.
    I dont think so.

    http://phptal.motion-twin.com/manual...tal-attributes

  24. #24
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    Yeah, I think ctx2002 is wrong/misunderstood. PHPTAL allows you to provide dummies, and replace them. That's what the attribute called tal:attributes does.

  25. #25
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get why you want your server-side code to be valid to w3c? Why does it matter? That's not going to be the end output. You only need to worry about the actual ending HTML/CSS output being valid.


Tags for this Thread

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
  •