SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 33
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Templates and mysql

    Could someone please point me to an article or tutorial showing how to store templates in mysql database and using them.

    Also looking at a variety of template systems such as:

    Smarty
    Awesome Template
    PHPLIB
    FastTemplates

    Could you guys please recomend which template would be a wise choise to use. And also if these template systems support mysql.

    Thx in advance

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Templates engines are evil, you will belive me after reading this: http://www.sitepointforums.com/showt...threadid=67849

    This will explain everything you need...!
    - website

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only if they're bloated (ie Smarty) and mis-used and/or abused

    I think there is a tutorial over at www.edevcafe.com or was it www.evolt.org ?

    Sorry cannot exactly remember which one; also try www.phpbuilder.com and/or www.devshed.com for more general idea's on templating.

    Good Luck.

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dr Livingston: I though every single Sitepoint member got convinced after that thread ! Vincent surely convinced me! (and he messed up M. Johansson's hair )
    - website

  5. #5
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am convinced, but the point was not that template engines are evil. The point was that template engines on top of PHP for the sole purpose of keeping any PHP code away from HTML was evil..

  6. #6
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, yes, true
    - website

  7. #7
    "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)
    example: my template engine still allows you to use <?php tags exactly as if it was in a PHP file, if you really want to use it, and also supports simple transformations of <if $var==1> to PHP, nothing overly complex, just keeps business logic and presentational logic seperate.
    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.

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but do any of these template engines allow you to store the templates in the database

  9. #9
    "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)
    Well mine's really more of a parser, you can store it anywhere you like... unfortunately I can't post it. Sorry .

    Although there should be some out there that do it.
    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.

  10. #10
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Zoltron, AFAIK Smarty can fetch templates from a db:

    http://smarty.php.net/manual/en/temp....elsewhere.php

    And no matter what the others may say, I have found working with it very easy. (Haven't tried the db option though)
    Last edited by mkrz; May 23, 2003 at 05:27.

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have been reading the post about templates and how they shouldn't be used. In the examples i saw "&=" being used
    ex. $it =& new DataFileIterator($data);

    could someone please explain to me what that is used for.

  12. #12
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    - website

  13. #13
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    Dr Livingston: I though every single Sitepoint member got convinced after that thread ! Vincent surely convinced me! (and he messed up M. Johansson's hair )
    Sorry, but I was totally unconvinced by the post. Template design is not the henious crime it's made out to be when used in the correct context. I fully appreciate that it is not suited to every application, and perhaps not even suited to the majority of applications. However, to dismiss it out of hand is slightly tunnel-visioned (no offence intended) and closes an avenue which I believe every developer striving towards an MVC design philosophy, should keep open.

    It was interesting to see examples such as:

    {title|ucfirst}' (Smarty)

    versus:

    <?= ucfirst($title) ?>

    and certainly if that was all template designs were used for then the argument against them would be stronger. However, start moving away from the very basic "echo some text" design, and into real world "conditional expressions and loops determine output design" and templates come more into their own when you want to pass the formatting of the content to non-PHP web designers.
    Code:
    <table>
      <patTemplate:tmpl name="myTable">
    </table>
    is easier for a non-PHP'er in my opinion than:
    Code:
    print '<table class="tbl">';
    
    for ($i=0, $max = sizeOf($arrData); $i < $arrData; ++$i) {
        echo "<tr>\n    <td>Category $arrData[$i]</td>\n";
        echo "  <td>Status $row[1]</td>\n";
        echo "  <td><a href=\"order.php?id=$arrData[3]\">Order me</td>\n";
        echo '</tr>';
    }
    print '</table>';
    which itself could be 300 lines of code down in some included php file somewhere. How will the HTML developer even know which file to start looking in? Even this previous example still doesn't completely do templates full justification.

    Once you start getting into nested loops, with few embedded conditions whose output is the result of a user-defined function thrown in for good measure, it becomes more and more useful to use a template. If I ask a pure HTML developer to add a style to the Administrator's Control Panel link (let's say make it red for argument's sake), he/she will not have to negotiate their way through reams of, what might be, unintelligable PHP code and condition statements, to find the link in question. In fact, they won't come into contact with any PHP at all.

    Let me make one final point (as always with these kind of discussions, we could all go on forever). Vincent is NOT wrong with his point of view. It differs from mine but that produces healthy debate. My point is that you cannot simply say to anyone considering a template design that they don't need one. They may not, but without extensive information on what it is they wish to achieve, and who is likely to support it, then I think judgement should be reserved. I am not advocating that every developer should use templated design; they shouldn't! Just keep an open mind to the concept.

    Hair still intact!


  14. #14
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not disagreeing with Vincent at all

    Also I am not dismissing Templating either - I merely stated from my own observations that Smarty is bloated - and it is to be honest with you.

    Another point with Smarty is that the majority of user's of Smarty never will use the full functionality of it; also there is the steep learning curve you have to think about.

    There are better solutions available. Hope this clears up your questions, etc ?

  15. #15
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't comment directly on Smarty as I have never used it (although I did read about it on their web site, but opted for patTemplate as it used xml style tags). I was commenting more in general on the template concept.

    This one will always generate interesting debate

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

  17. #17
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, as for your example
    Code:
    <patTemplate:tmpl name="myTable">
    I must say that I find that more complicated to figure out then the example with the for loop, which you infact, make much more complicated then it really is, php can move in and out of html with the <? ?> which I think makes it a lot like template engines, as to clear up your example, I would do
    PHP Code:
    <table class="tbl">
    <? for ($i=0$max sizeOf($arrData); $i $max; ++$i) { ?>
    <tr>
        <td>Category <?=$arrData[$i]?></td>
        <td>Status <?=$row[1]?></td>
        <td><a href="order.php?id=<?=$arrData[3]?>">Order me</td>
        </tr>
    <? ?>
    </table>
    or even better:
    PHP Code:
    <table class="tbl">
    <? foreach($arrData as $val) { ?>
    <tr>
        <td>Category <?=$val?></td>
        <td>Status <?=$row[1]?></td>
        <td><a href="order.php?id=<?=$arrData[3]?>">Order me</td>
        </tr>
    <? ?>
    </table>
    As you can see this does the same but is much more readable then all your echo lines

    On my websites I don't make this templating complicated. First when the page loads I load a specific class (usually different on different pages), that class holds reference to my own template class that is extreamly simple. I do almost all processing in my functions and methods before I load the template. The template gets some variables it has been assigned and it uses simple loops (mostly foreach) and a very few custom functions.

    As for your example again, I must say I don't have a clue how it works... Where is the table structure ? in some other file perhaps ? this I simply find harder to manage and/or maintain because the templates are so ripped apart, I must say that 'my' (originally yours ) example says more about how the layout is then yours.

    And then Dr Livingstone makes good point, people hardly ever need all that is included with the template 'engines', all these things that make it so slow ... (at least slower then simple php).
    I just see no point in working hard making things more complicated then they need to be!
    As Vincent had already pointed out you also have to spend time having the 'latest' verision of the template 'engine' and you have to (in most cases) to learn some ugly template syntax, which in fact I gave up learning because it was so stupid, take this example:
    Code:
    {section name=show loop=$data}
      {$data[show].content}
    {/section}
    and this is how Smarty does it instead of simply
    PHP Code:
    <? foreach ($data as $row) { ?>
    <?=$row
    ['content']?>
    <? 
    ?>
    stupid isn't it ?

    I mean, this is simply unneccisary, there is no point in inventing the wheel when it already exists!

    Hope my post makes some sense (just woke up )
    - website

  18. #18
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly, apologies as some of my code in my previous example appeared to be missing (fixed now). Not sure what happened there but I was copying and pasting a bit.

    I had no trouble learning the limited syntax available for use with the patTemplate engine (took one day of reading and that was about it). As usual, I learned what I required and ignored the rest until such time as it became needed.

    You say people hardly ever need all the functionality of the template engines. How many of the PHP functions do you use out of all that are available? They only slow you down if you actually run code through them. There's no harm with them being included at start up.

    You seem to think templates are slow. I agree, by their nature, they are slower than straight-forward PHP, but to say they are slow is simply incorrect. To prove my point take a look here:

    http://events.lexusonwersclub.co.uk
    User: testacc
    Pass: testacc (you won't be able to add anything, obviously)

    Does it seem slow to you? The complete event management tool is written using patTemplate and PHP.

    It seems you are missing an important point though. This isn't about how easy it is for you to read, learn the syntax for, and understand, it's how easy it is for someone, who has never had any experience of PHP, is able to design the HTML page (using your results from the server side processing). They don't even have to learn the template syntax. They simply need to know what bits display what and style and integrate them accordingly. I find it surprising that you think jumping in and out of php tags within a large HTML document to be easier to read, remembering that such HTML output could be embedded very deep within surrounding PHP code. It simply begs for a syntactical error to rear it's ugly head. So easy to miss off the closing } (<?php } ?>), portability and/or completely changing the layout of the HTML document is out of question unless you know which closing } is for which part of the code and having to edit echo, print, printf, or heredoc statements (perhaps negotiating double quoting, etc).

    Is this something you expect a HTML/graphics only developer to do? Personally I don't.

    So, the code to construct the table in my previous example would be:
    PHP Code:
    for ($i 0$max sizeOf($arrData); $i $max; ++$i) { 
        
    $tpl->addVar('myTable''CATEGORY'$arrData[$i]['title']);
        
    $tpl->addVar('myTable''STATUS'$arrData[$i]['status']); 
        
    $tpl->addVar('myTable''ID'$arrData[$i]['link']);
        
    $tpl->parseTemplate('myTable''a');
    }

    $tpl->displayParsedTemplate('myTable'); 
    Code:
    <!-- HTML file -->
    <!-- No PHP code at all -->
    <table>
      <patTemplate:tmpl name="myTable">
      <tr>
          <td>{CATEGORY}</td>
          <td>{STATUS}</td>
          <td><a href="order.php?id={ID}">Order Me</a></td>
      </tr> 
      </patTemplate:tmpl>
    </table>
    If the designer then chooses to not display the results in a table, but now as a heading, paragraph and straight-forward link, they need only modify the template and move the placeholders
    Code:
    <h2>{CATEGORY}</h1>
    <p>Presently this item is {STATUS}<br />(<a href="order.php?{ID}">Click to order</a>)</p>
    Absolutely no contact with any PHP was required to change the layout, no editing of echo tags, print tags, printf statements, heredocs, etc.

    The point of templates is not about the PHP designer; it's about the HTML developer. Separating the business rules from the presentation.

    I stress again my final point of before. Templates are not for everbody and every application. However, it is simply wrong to say there is never a need for them in any case, for anyone.
    Last edited by torrent; May 24, 2003 at 12:42.

  19. #19
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You say people hardly ever need all the functionality of the template engines. How many of the PHP functions do you use out of all that are available? They only slow you down if you actually run code through them. There's no harm with them being included at start up.
    As for the php functions, they are there, you can't do anything to not include them...
    About the speed, it is different between template engines, but take smarty for example, it is just one giant class that is always loaded, when the class is loaded it parses everything within it, lot of cpu wasted on parsing all that since most isn't used ...
    To prove my point take a look here:
    Server down ...

    As for the rest. First of all, I hardly ever use echo print or printf in the templates, I only use <?=$var?>, and your point saying no php code, is it any better to have some patTemplate code then php ?
    And you say I will get confused by the closing point of {} blocks, I must say I have not been through that before, and even if so it simply generates a error and is fixed very easily.

    As for my method of doing the same thing as you did below:
    PHP Code:
    $tpl->assignVar('variable'$arrData);
    $tpl->useTemplate('myTemplate'); 
    Code:
    <!-- HTML file -->
    <table>
      foreach ($variable as $val) {
      <tr>
          <td><?=$val['title']?></td>
          <td><?=$val['status']?></td>
          <td><a href="order.php?id=<?=$val['id']?>">Order Me</a></td>
      </tr> 
      <? } ?>
    </table>
    I find this just as simple, and really, learning php basics is very easy!
    Then again you must admit that since this template simply uses pure php and needs no external 'parsing' it is faster, and that is ofcourse a big part of this all.
    And yes, changing this to be header is just as easy with this method...
    Absolutely no contact with any PHP was required to change the layout, no editing of echo tags, print tags, printf statements, heredocs, etc
    Absolutely no contact with patTemplate syntax was required, no editing of echo, print or printf tags ...
    The point of templates is not about the PHP designer; it's about the HTML developer. Separating the business rules from the presentation.
    I simply can't see how this (my) method is in anycase harder to learn then the patTemplate one ...
    I stress again my final point of before. Templates are not for everbody and every application. However, it is simply wrong to say there is never a need for them in any case, for anyone.
    Well, I agree, I use templates, I simply don't add on some huge class/function library and parsing and comlicated syntax and some unneccisary stuff, I just use PHP which is ofcourse, just another template engine

    So to make my final point, I think "templates" are good, seperating buisness logic from presentation logic is good, but adding different syntax to the templates and some big file of function library (that is hardly ever used) is simply unneccisary since doing the same thing with PHP is just as simple...
    - website

  20. #20
    "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)
    My (very simple) template parser handles 5 tags total I think, as well as support for PHP for those who would like to use it, and supports short-tags whether the server has it enabled or not.

    I find that flexible, powerful for advanced users, simple for beginners, and an all-around good system, as well as it being written specifically for one project, and only contains features that one project needs, not 200 extras that will never be used in the app.
    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.

  21. #21
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    Server down ...
    Bloody typical
    PHP Code:
    $tpl->assignVar('variable'$arrData);
    $tpl->useTemplate('myTemplate'); 
    Code:
    <!-- HTML file -->
    <table>
      foreach ($variable as $val) {
      <tr>
          <td><?=$val['title']?></td>
          <td><?=$val['status']?></td>
          <td><a href="order.php?id=<?=$val['id']?>">Order Me</a></td>
      </tr> 
      <? } ?>
    </table>
    Now show me the same example while remaining XHTML compliant (i.e. no short tags ).

    Remember, this is not what's easiest for you to learn, it's what's easiest for the HTML developer to work with. They don't want to learn templates or PHP they simply need to know where certain information is in order to be able to style and integrate it.

    I am not saying that you are wrong to not use templates in your circumstances, only that it is wrong for you to make blanket statements that template engines are never needed, useless, and completely without benefit. I do not believe loading a large class at the start of a script has any noticeable performance deficit. In fact, if you like I can run some start and end time tests to see how much difference it makes (I've never done this and so could be shooting myself in the foot here) We could start a whole new discussion on writing efficient PHP code that would have more bearing on the performance of a script than including a template engine class.

  22. #22
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by torrent
    only that it is wrong for you to make blanket statements that template engines are never needed, useless, and completely without benefit
    Quote Originally Posted by website
    So to make my final point, I think "templates" are good, seperating buisness logic from presentation logic is good
    ...

    But I don't think that my developers will feel better if they are XHTML compliant or not, it really doesn't matter in my templates, it is just a standard that doesn't need to be followed...

    Smarty speed check:
    Just a tiny check, this code:
    PHP Code:
    <?
    function getmicrotime(){ 
        list(
    $usec$sec) = explode(" ",microtime()); 
        return ((float)
    $usec + (float)$sec); 

    $start getmicrotime();
    include (
    'Smarty.class.php');
    $smarty = new Smarty;
    $end getmicrotime();
    echo (
    $end-$start);
    ?>
    took 0.123445987701 seconds.
    This code:
    PHP Code:
    <?
    function getmicrotime(){ 
        list(
    $usec$sec) = explode(" ",microtime()); 
        return ((float)
    $usec + (float)$sec); 

    $start getmicrotime();
    $end getmicrotime();
    echo (
    $end-$start);
    ?>
    took 0.000570058822632 seconds

    I think this is just quite a bit, just including the file and instantiating the class takes so long time. What do you think ?

    Note: These times were not the best times and not the worste, I tried to take it from the middle, and this was not done on very fast computer
    - website

  23. #23
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just FYI:

    I've found the bc_math functions to be more precise to do calculations and have written a little simple class to do this for me.
    I just thought I'd post it here, maybe it could be to someone's benifit:
    PHP Code:
    class Chrono
    {
        var 
    $start;
        var 
    $stop;
        var 
    $ellapsed;
        var 
    $decimal;
        function 
    Chrono($decimal 6)
        {
            
    $this->decimal = (int)$decimal;
            
    $this->start null;
            
    $this->stop null;
            
    $this->ellapsed null;
        }
        function 
    start()
        {
            
    $this->start $this->getMicrotime();
        }
        function 
    stop()
        {
            
    $this->stop $this->getMicrotime();
            
    $this->ellapsed bcsub($this->stop$this->start$this->decimal);
        }
        function 
    reset()
        {
            
    $this->Chrono$this->decimal );
        }
        function 
    getTime()
        {
            return 
    $this->ellapsed;
        }
        function 
    getMicrotime()
        {
            list(
    $dec$t_stamp) = explode(' 'microtime());
            return 
    bcadd($t_stamp$dec$this->decimal);
        }
    }

    // usage:
    $chrono =& new Chrono();
    $times = array();
    $chrono->start();
    // do some stuff
    $chrono->stop();
    $times[] = $chrono->getTime();
    $chrono->reset();
    $chrono->start();
    // do some other stuff
    $chrono->stop();
    $times[] = $chrono->getTime();
    //... 
    Maybe stop() should be rewritten, so the last line in the method would be something like
    PHP Code:
    return ($this->ellapsed bcsub($this->stop$this->start$this->decimal));
    // OR
    $this->ellapsed bcsub($this->stop$this->start$this->decimal);
    return 
    $this->getTime(); 
    To safe you a few lines in the class usage ;-)
    Per
    Everything
    works on a PowerPoint slide

  24. #24
    "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 torrent
    Now show me the same example while remaining XHTML compliant (i.e. no short tags [img]images/smilies/smile.gif[/img])
    Well not that this is really the same, but in my template system, the short tags are still XML compliant, because I make sure the <? doesn't have xml and the like after it.
    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.

  25. #25
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    Well not that this is really the same, but in my template system, the short tags are still XML compliant, because I make sure the <? doesn't have xml and the like after it.
    <? in XML is considered to be a processing instruction. You follow <? with = in a well-formed XML document and the parser will generate an error.

    Website, you and I are coming from completely different sides of the divide regarding development. You don't consider standard compliance to be important, I do. You don't think it is important to ever separate the application logic from the view, I do (sometimes). I doubt we shall ever agree on this. You also consider that including a class which made your script take just over a 1/10th of second to load, to be a noticeable difference, I don't. I would defy anybody, who didn't know otherwise, to have been able to tell you which script included the template engine, and which one didn't. So, in the real world, my statement from before stands...."I do not believe loading a large class at the start of a script has any noticeable performance deficit"

    I still believe many of the arguments against templates are weak, without much "real-world" evidence (like the good ol' performance one every bangs on about) and usually based on other people's opinions as opposed to personal experience. I still remain wholly unconvinced that they are a bad thing when used at the right time and for the right purpose.

    I shall leave you to have the last word on the matter and thank you for a stimulating debate


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
  •