SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 75
  1. #1
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Realy, how do you do it?

    I know this topic has been beaten to death, but to be honest I've still not found a solution that can cope with all the needs I / my applications have. I'm talking about templateing or separating business from presentation logic. The possible options I see today are these:

    • XML + XSLT, in php5 the support for XML is realy nice. But this will mostly result in a DB > PHP > XML > PHP > XML+XSLT > XHTML approach which is far from practical - especially when working with ORMs, etc.
    • Using php, ah yes - I've thought much about this one and this seems by far the best aproach atm. But I've not foundy any good way to abstract things so you can use generic iterators, etc. to print information. Also haven't decided on push vs. pull
    • Write your own, this is a big no-no for me as home made template "syntaxes" usually consist of some strange combination of # { and @.


    So I ask you here at sitepoint, how've all of you solved it?

  2. #2
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP. That's what all the smart kids are doing and I don't have a reason to stray from the path. I have tried at least Smarty and XSLT, but neither were for me.

  3. #3
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku
    PHP
    How are you doing it in php?

  4. #4
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't really solved it yet. Though I like the idea of using the XmlReader to read in a template, and XmlWriter to write the result, then just a matter of putting something in the middle.

    XmlReader -> MagicHappensHere -> XmlWriter

    XmlReader should support XInclude, so get includes for free. The XmlWriter on the end ensures no XSS exploits, as everything gets escaped correctly, and character sets become less of a issue to.


    This was a "cute" idea too
    http://www.mikenaberezny.com/archives/40
    Using a custom stream wrapper, to get from a simpler syntax to PHP.

  5. #5
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    I haven't really solved it yet. Though I like the idea of using the XmlReader to read in a template, and XmlWriter to write the result, then just a matter of putting something in the middle.

    XmlReader -> MagicHappensHere -> XmlWriter

    XmlReader should support XInclude, so get includes for free. The XmlWriter on the end ensures no XSS exploits, as everything gets escaped correctly, and character sets become less of a issue to.
    This isn't much of a response but: XML got the nickname XM(hel)L a reason ;p. For configs, etc. I think it's fine tho.

    Quote Originally Posted by Ren
    http://www.mikenaberezny.com/archives/40
    That's a nice one, will toy around with it some and post the results.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku
    PHP. That's what all the smart kids are doing and I don't have a reason to stray from the path. I have tried at least Smarty and XSLT, but neither were for me.
    +1. Here's a little more about a simple design I've been using.

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm more in favour nowadays of using PHP within the context of applying the Composite View to generate web pages

    In my view (pun?) you can't really beat using a Composite

  8. #8
    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 thr
    This isn't much of a response but: XML got the nickname XM(hel)L a reason ;p. For configs, etc. I think it's fine tho.
    Well XHTML is XML, the various "DoMagicHere" bits experiemnted with was replacing bits based on CSS style rules

    so title would match <title> and have a ReplaceChildren action, to rewrite the title and so on. I think its interesting idea anyway, and encourages a more semantic HTML.

    Another experiment had the XHTML loaded into a DOM composite structure, based on runat="server" style attributes, so could manipulate it, then render.

    That's a nice one, will toy around with it some and post the results.
    Yeah, if made the @$action perform echo htmlspecialchars($this->action), and have something slightly longer for outputting raw html, might be good.
    Last edited by Ren; Feb 20, 2006 at 12:13. Reason: Had to add composite ;)

  9. #9
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    How are you doing it in php?
    Well, I have an MVC setup with the Controllers pushing data from Model to View layer. The data is often lazy-loaded, though, and the View layer can make calls back to the Controller layer, so the boundary between push and pull is a bit blurred.
    Quote Originally Posted by Ren
    This was a "cute" idea too
    http://www.mikenaberezny.com/archives/40
    Using a custom stream wrapper, to get from a simpler syntax to PHP.
    Heh, that's actually quite clever. A novel idea for once

    Someone there asked whether it would be possible to get rid of array()... Tell me, Ren, as you're more proficient with regex here, would parsing {'abc', 'foo' => 'bar', 'bar' => $foo, $bar => 'foo'} be problematic?
    Quote Originally Posted by Ren
    Yeah, if made the @$action perform echo htmlspecialchars($this->action), and have something slightly longer for outputting raw html, might be good.
    @@$action? Defeats the original purpose a bit, but hey.

  10. #10
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    How are you doing it in php?
    http://www.massassi.com/php/articles/template_engines/

  11. #11
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I toyed around some with the idea found at http://www.mikenaberezny.com/archives/40, that + reading at http://www.php.net/manual/en/functio...r-register.php gave me the result I attached. I a TemplateView + StreamView, the StreamView is responsible for the same things found at mikenaberezny.com (thinking about adding support for some custom stuff like view://modifiers@file.php ah well thats for tomorrow). The TemplateView is just a holder for the variables, but it has the __toString() methods implemented that makes the template render itself. This gives me the possibility to assign Template Views to other Template Views variables, like this:

    PHP Code:
    <?php
    $main 
    = new TemplateView;
    $main->title "TemplateView Demo";
    $main->body = new TemplateView('templates/body.php');
    $main->body->header "Some programming languages";
    $main->body->languages = array('PHP','Java','C++');
    ?>
    And in the template do this:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		<title><?=@$title?></title>
    	</head>
    	<body>
    		<?=@$body?>
    	</body>
    </html>
    The <?=@$body?> is then translated to <?php echo $this->body; ?> and since $this->body is a TemplateView it will call the __toString() method and render it also - easily nestable templates.

    Here's the full source + an example(require php 5.0+ i think, only tested it at 5.1.2 tho):
    Attached Files Attached Files

  12. #12
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    Worcester
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Currently, we use Smarty at work. The main reason I like it is that we can get it to "fail safe" so that displaying a variable using the default syntax of {$var} will cause it to be escaped.

    I would like to move to a PHP based solution at some point though to save having to know two separate syntaxes.

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    Sweden
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My opinion is that Smarty is to powerful and almost replaces php. I will use bTemplate for my next project.

    It's nice, simple and can easily be extended..
    http://www.massassi.com/bTemplate/

  14. #14
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use my own template engine that is sort of based on XSLT in construction, except instead of a test="" attribute, you declare your tests with attributes like var, value, and condition. Of course this is subject to change on my whim, but the spec is getting solidified. The backend code needs some work though

    Here is just a very simple example of generating a page (don't confuse my terminology "page controller" with the Page Controller pattern, they are different beasts).

    "app root"/lib/pages/mypage/page_controller.php:
    PHP Code:
    <?php

    class pageControllerMypage extends pageControllerBase {

        public function 
    generate($db$service_plugin$template) {
            
    $template->setTemplateVar('foo''true');

            
    /**
             * Forgive this bit of nonsense.
             * Usually template block or loop areas
             * are constructed while iterating over
             * a record set, making it much less silly looking.
             */
            
    $fooNames = array(
                array(
    'name' => 'BarBar'),
                array(
    'name' => 'FooBar'),
                array(
    'name' => 'FooFoo'),
            );

            
    $template->setTemplateVar('fooNames'$fooNames);

            
    $service_plugin->generateMypageTemplate();
        }

    }
    And the template file:
    "web root"/templates/pages/mypage/index.html:
    HTML Code:
    <p>Hello, we are checking for foo.</p>
    
    <flexie:choose>
    	<flexie:when var="foo" value="true">
    		<p>We have found some foo!.</p>
    		<p>
    		<flexie:block var="fooNames">
    			{block.rowCount}. {name}<br />
    		</flexie:block>
    		</p>
    	</flexie:when>
    	<flexie:otherwise>
    		<p>Sorry, no foo for you.</p>
    	</flexie:otherwise>
    </flexie:choose>
    which then generates:
    Hello, we are checking for foo.

    We have found some foo!.

    1. BarBar
    2. FooBar
    3. FooFoo

  15. #15
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JoL
    You call that simple? The loop constructs are just insane and some of the other things, god =/.

  16. #16
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using PHPTAL. Despite what everyone always says in these threads, designers will not use PHP...... unless you've all discovered a new breed to the ones I've come accross.

  17. #17
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    You call that simple? The loop constructs are just insane and some of the other things, god =/.
    It is my opinion that loops are far too complex to use in templates. Period. They should not be in templates at all, too much design behind a loop.

    The most logic my templates deal with are if/else statements.


    I wrote a template parser a few years ago that I still use today, it was for a commercial product and it does everything I need it to, and more. No complaints here.

    While ideally I'd love to see a nice template engine built into PHP, (there actually are, but that's not the point here), there are far too many disagreements on what it should do, shouldn't do, etc.

    There are SO many template engines for PHP, it's kinda sickening, haha, but that doesn't stop me from using mine... yet at least, I may look into a more standard one at some point, but at this point in time, my template engine is built to satisfy the client, designer, and programmer, without making things stupidly complex.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  18. #18
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Show me how you print say a user list without a loop.

  19. #19
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't say don't use loops, I said keep the loops out of the templates.

    Template "user":

    Code:
    <strong>Username</strong>: $username<br />
    Template "page":

    Code:
    <include template="header" />
    <h1>Users:</h1>
    $users
    <include template="footer" />
    Code:

    PHP Code:
    $users '';
    foreach (
    $users as $user)
    {
        
    $username html_entities($user['name']);
        eval(
    $template->store('user''users'true));
    }
    eval(
    $template->get('page')); 
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  20. #20
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    easily nestable templates.
    I've worked with a similar design. I found it too limiting as each template has it's own scope. There's no way nested templates can access variables assigned to their 'parent' templates. You'll be thinking about scope *a lot* when designing. That can get rather annoying, especially when there are relationships between template bits.
    I found it much more flexible to share all variables (assign all to one template instance). It's the template file's responsability to render() nested templates (shared bits most of the time). This results in more 'readable' templates. You don't have to dig through controlling logic to understand a view's structure.
    Of course there will be some inefficiency because all variables are rendered with each render() call.

  21. #21
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rossriley
    Using PHPTAL. Despite what everyone always says in these threads, designers will not use PHP...... unless you've all discovered a new breed to the ones I've come accross.
    Another plus for PHPTAL, which is my favorite too: it will escape output by default, making it safer against XSS attacks.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  22. #22
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit #1: I missnamed a variable in CachingTemplateView, uploaded a new version.
    Edit #2: Uploaded the UML diagram over the classes

    Ok, here's my 7.7kb result after some more hacking with the ViewStream. There's currently 7 classes/interfaces - they and thier functionallity is as follows:

    • View - interface, defines render() and __toString();
    • TemplateView - The standard template wrapper which takes a filename as argument and then calls render() to render it with it's local variables
    • CachingTemplateView - Same as TemplateView but caches the result(this will probably be refactored a standard Filter).
    • ViewChain - Chains any amount of views and when it's rendered() it calles render() on all of it's Views and concatenates them, and returns the result.
    • ViewFilter - Filters any type of view as it applies a filter to it's output from render().
    • ViewFilterChain - Adds any amount of filters(addFilter()) and any amount of Views(addView() - which can also be filters), it first renders all the views and then runs the result thru all of the filters.
    • ViewStream - defines the view:// stream in php used to translate @$ to $this and parent-> to getParent()->


    So basicly you can mix/match any amount of views of any kind(filter,templateview,chain) and just call render() on the top one and it'll do all the work.

    Quote Originally Posted by michel
    There's no way nested templates can access variables assigned to their 'parent' templates.
    If you use @$parent->variable it will be translated to $this->getParent()->variable, if you do @$parent->parent->variable it will be translated to $this->getParent()->getParent()->variable to get your parents parent ;).
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by thr; Feb 21, 2006 at 05:39.

  23. #23
    Can we go to a 48 hour day?
    Join Date
    May 2002
    Location
    MI
    Posts
    906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use Savant for the templating. I end up creating "builder" php pages that pass the final info to the template. It allows you to define custom escaping routines for output which can be nice. The add on for forms works out ok and I've gotten much better performance than when I tried smarty for a little while.
    mitechie.com
    "Techies just think a little differently
    ...at least that is what they keep telling me."

  24. #24
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    If you use @$parent->variable it will be translated to $this->getParent()->variable, if you do @$parent->parent->variable it will be translated to $this->getParent()->getParent()->variable to get your parents parent .
    Reminds me of DOM scripting. A little too complicating for my taste. Implementing a hierarchy of objects forces you into thinking about scope, which works countproductive for me when designing templates. That's the main reason I ditched previous template designs. But then again, I'm just a simple graphic designer .

  25. #25
    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 thr
    If you use @$parent->variable it will be translated to $this->getParent()->variable, if you do @$parent->parent->variable it will be translated to $this->getParent()->getParent()->variable to get your parents parent .

    Wouldn't adding

    PHP Code:
    public function __get($variable)
    {
        if (
    $this->parent)
            return 
    $this->parent->{$variable};

    to TemplateView allow in alot of cases to just use the simpler syntax, unless a variable of local scope is masking a more global variable.


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
  •