SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 52
  1. #26
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DarkAngelBGE
    Arborint, could you please provide a sample template of yours? I am interested in your approach.
    I included a template. It is just HTML with embeded ^tags^ (I did the tildes because of Webnet's comment, they are usually braces like this {tags} ).
    Christopher

  2. #27
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mmh, just some thoughts about "template systems":
    Correct me if i'm wrong, but wasnt it the intention of the PHP developers to give us, the webmasters/programmers, a bunch of functions to create dynamic websites? So why dont use them? What is the problem to place some PHP code inside HTML?

    First argument of most users: Seperate code (PHP) from presentation (xHTML)
    That's right, but i can do this without any big template system. I simply outsource functions in a seperate file, include this file at the top of the xHTML file and call the functions in the HTML where i need them, e.g.:

    Code:
    <?php include_once("mainFunctions.php"); ?>
    
    <html>
     <head>
      <title> <?php getTitle(); ?>
     </head>
     <body>
      <?php getNews(); ?>
      <?php getFooter(); ?>
     </body>
    </html>
    It is that simple! So why I should parse a whole html file with placeholders and replace them with data? What is the difference between {header} and
    <?php getHeader() ?>. I also think the "placeholder idea" is more limited in comparison to the function call, because i have the chance to give some parametrs with this functions. For example, getNews(5), mean show me the five latest news, it's more handy i think! At someplace we have to go concrete with our code, means, we must execute code also in the "presentation layer". Further, it takes more time to execute the template engine than my simple example.

    Argument No. 2: A webdesigner dont have any clue of PHP, so we must prevent him to get in contact with it? Why, what is the problem? If the WEBdesigner dont have a clue of any web programming/scripting language, fire him! A webdesigner fight with CSS, xHTML, and other stuff all the time, and he MUST know what is possible with PHP to get the most out of his design! It's right he should'nt know in-depth knowledge, but he should fairly know what it is at all. Further, what is the problem to script xHTML/CSS around some <?php .. ?> statements, it's the same like {placeholder}. By the way, design is mainly done by CSS, as sitepoint preaching for years now.

    For example, a bloggin system called Wordpress, maybe some of you know it, dont use any template engine, but the "idea" presented above. Design, means look and feel, is done completly with CSS, there are just some <?php ... ?> in the xHTML code that dont hurt anyone, and which everyone, and mainly webdesigners, should understand. There is one file for every "section": index, archive, contact, and so on, you should get the idea...

    What do you think about it?

  3. #28
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dirk_
    Mmh, just some thoughts about "template systems":
    Correct me if i'm wrong, but wasnt it the intention of the PHP developers to give us, the webmasters/programmers, a bunch of functions to create dynamic websites? So why dont use them? What is the problem to place some PHP code inside HTML?
    The original intention of PHP and its usage today are very different and have nothing to do with templating in PHP today.

    Quote Originally Posted by dirk_
    First argument of most users: Seperate code (PHP) from presentation (xHTML)
    That's right, but i can do this without any big template system. I simply outsource functions in a seperate file, include this file at the top of the xHTML file and call the functions in the HTML where i need them, e.g.:
    ..removed code..
    Its separation of logic from presentation, not code from presentation. HTML is still code, but it doesnt contain any logic.
    And your example is simple enough for a situation where you only need to replace 3 dataholders. It gets ugly when that number starts to increase.
    And i dont want to create a lot of functions for each and every dataholder:
    Code:
    function header() {
    echo 'Hello world';
    }
    Quote Originally Posted by dirk_
    It is that simple! So why I should parse a whole html file with placeholders and replace them with data? What is the difference between {header} and
    <?php getHeader() ?>. I also think the "placeholder idea" is more limited in comparison to the function call, because i have the chance to give some parametrs with this functions. For example, getNews(5), mean show me the five latest news, it's more handy i think! At someplace we have to go concrete with our code, means, we must execute code also in the "presentation layer". Further, it takes more time to execute the template engine than my simple example.
    The difference is push vs pull thinking. getHeader() is pulling the data into the template, {header} is pushing the data to the template.
    Besides, we could never have used your idea where i work as we have a webbased admin panel for templates and store them in database.
    Secondly we compile templates to php code, so speed is not a problem.
    (Parsing templates can bring up a speed problem i agree.)

    Quote Originally Posted by dirk_
    Argument No. 2: A webdesigner dont have any clue of PHP, so we must prevent him to get in contact with it? Why, what is the problem? If the WEBdesigner dont have a clue of any web programming/scripting language, fire him! A webdesigner fight with CSS, xHTML, and other stuff all the time, and he MUST know what is possible with PHP to get the most out of his design! It's right he should'nt know in-depth knowledge, but he should fairly know what it is at all. Further, what is the problem to script xHTML/CSS around some <?php .. ?> statements, it's the same like {placeholder}. By the way, design is mainly done by CSS, as sitepoint preaching for years now.
    Who said templates was just meant for designers? Do you use designers to edit that static bit of text that says "Category listing" to "List categories" ?
    Where i work editors of sites also needs to be able to edit some things, designers need to, technical staff need to.
    I dont want people to be able to use php in templates. I want to restrain options to a bare minimum of what is needed, i want to stay in control.

    Quote Originally Posted by dirk_
    For example, a bloggin system called Wordpress, maybe some of you know it, dont use any template engine, but the "idea" presented above. Design, means look and feel, is done completly with CSS, there are just some <?php ... ?> in the xHTML code that dont hurt anyone, and which everyone, and mainly webdesigners, should understand. There is one file for every "section": index, archive, contact, and so on, you should get the idea...

    What do you think about it?
    I hope no one here is argumenting that templating is the way to go in every scenario, cause is not. "The right tool for the job" is important, never forget that.

  4. #29
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dirk_
    Mmh, just some thoughts about "template systems":
    Correct me if i'm wrong, but wasnt it the intention of the PHP developers to give us, the webmasters/programmers, a bunch of functions to create dynamic websites? So why dont use them? What is the problem to place some PHP code inside HTML?
    I have no technical problem with PHP templates and I use them as well. Though I don't think your argument that designers should be able to deal the PHP in their templates is a strong one.

    However I think you have forgotten one important reason not to use PHP templates: security. If you use PHP templates you open your machine to the designer by giving access to PHP. That may not matter on little sites, but it does matter on larger ones.
    Christopher

  5. #30
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As this topic has been beaten to death, I suggest you guys check this article before you continue arguing: http://www.massassi.com/php/articles/template_engines/

    dirk_ has one good point, which is that originally PHP is a templating engine. However, in time it has grown to become nearly a full-fledged language, with most of the power other OOP languages have, which enables us to make much more complex applications. I have also found the simple PHP syntax the best for templating, and am using it with my quite complex MVC frameworks.

  6. #31
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off all, i dont say that template engines are useless, i know, nobody said that, but i want to underline this. I just recognized that many users who begin to code, want to code "something big". Many begin to code their own framework, which is okey, because you learn by coding. But if they begin to code a real project it seems there is been alot of overhead. For a little "portfolio" page of your friend you dont need a big template system behind all that.

    Like findus said
    Quote Originally Posted by Findus
    I hope no one here is argumenting that templating is the way to go in every scenario, cause is not. "The right tool for the job" is important, never forget that.
    I totally agree, with that!

    There are situtations where it is very important to seperate stuff, for example with a template engines, because of the many users who working on a system in whole. Findus gave many right ideas here!

    But i will answer tu Findus post anyway:

    Quote Originally Posted by Findus
    The original intention of PHP and its usage today are very different and have nothing to do with templating in PHP today.

    Its separation of logic from presentation, not code from presentation. HTML is still code, but it doesnt contain any logic.
    And your example is simple enough for a situation where you only need to replace 3 dataholders. It gets ugly when that number starts to increase.
    And i dont want to create a lot of functions for each and every dataholder:
    Code:
    function header() {
    echo 'Hello world';
    }
    I agree. Sorry for the lack of knowledge with this logic/presentation thing, it's excatly what i meant! You're right if you say you dont want to write functions for dataholders, but in little projects that works just fine i think. An other example coulde be to include a file with declarations of variables like:

    Code:
    indexVars.php:
    
    $title="Home"
    $header="Welcome"
    
    index.php:
    
    <?php include_once("mainVar.php" );?>
    <html>
     <head><title><?php echo $titel ?></head>
    </html>
    <body>
     <?php echo $header ?>
    </body>
    An just write bigger functions for thinks like database connections an so on. As I mentioned this idea was meant for little projects. Not for enterprise projects.


    Quote Originally Posted by Findus
    The difference is push vs pull thinking. getHeader() is pulling the data into the template, {header} is pushing the data to the template.
    Besides, we could never have used your idea where i work as we have a webbased admin panel for templates and store them in database.
    Secondly we compile templates to php code, so speed is not a problem.
    (Parsing templates can bring up a speed problem i agree.)

    Who said templates was just meant for designers? Do you use designers to edit that static bit of text that says "Category listing" to "List categories" ?
    Where i work editors of sites also needs to be able to edit some things, designers need to, technical staff need to.
    I dont want people to be able to use php in templates. I want to restrain options to a bare minimum of what is needed, i want to stay in control.
    Yeah also right, i just take the point from the designers view, it's completly correct, there are many more people who can work on a system, so you have to think about what you need.

    Quote Originally Posted by Findus
    I hope no one here is argumenting that templating is the way to go in every scenario, cause is not. "The right tool for the job" is important, never forget that.
    Right. Keep that in mind. Just want to bring this in, because i think it is very important.

    Also in intresting: Templating with XML?! I think it's a very elegant way to do things, if it gets more complex!

    Dirk

  7. #32
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arborint
    I have no technical problem with PHP templates and I use them as well. Though I don't think your argument that designers should be able to deal the PHP in their templates is a strong one.

    However I think you have forgotten one important reason not to use PHP templates: security. If you use PHP templates you open your machine to the designer by giving access to PHP. That may not matter on little sites, but it does matter on larger ones.
    I also dont have any fears to use template engines Don't understand what you mean by "strong" exactly. If strong means, that webdesigners, shouldn't deal with php code, i dont agree. I think they should have a little knowledge about it, just to know not to delete code I dont meant that they should be able to code stuff, just to know what is code! But it's also right to hide code from the webdesigner, that he/she dont get in trouble with code and the system dont work anymore coz' they delete a line, or something.

    Security is an important thing, right! But if you code for yourself with a friend as webdesinger you need more time to code something like that you mentioned above, than take it "my way" (please, it is in no way a perfect one, i mean: to do it a little bit simpler). Just you and your webdesigner have access to these files and know what they do.

    But for bigger sites/projects i totally agree!

  8. #33
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    As this topic has been beaten to death, I suggest you guys check this article before you continue arguing: http://www.massassi.com/php/articles/template_engines/
    Its important to train the argumentation muscle in case you should run into such an discussion in real life without internet connection
    The content of that article i would think is common knowledge on this forum yes. But its really hollow, and i for one would never use a templating engine like that.
    There are three reasons you want to use a template engine.
    1. Separation of logic and presentation
    2. Ease the job for the designer and coder so they can work separately on the same page easier.
    3. Let the designer move things around without giving him acess to your entire server.

    What that article doesnt cater for at all is point 3, which is just as important as point 1 for many. Its not just about making things look fancy etc, you gotta consider security aswell.

    Edit: Forgot to say that i agree that templating has been discussed to death (close anyway).

  9. #34
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arborint
    That an HTML generator class not a Template class. I might do something like this instead:
    PHP Code:
    class HTMLTag {
        function 
    setAttribute($name$value) {}
        function 
    toHTML() {}
    }

    class 
    HTMLTable extends HTMLTag {
        function 
    addRow($row) {}
    }

    class 
    HTMLTableRow extends HTMLTag {
        function 
    addCol($col) {}
    }

    class 
    HTMLTableCol extends HTMLTag {
        function 
    addContent($html) {}

    Heh, an html generator. Anyone remember that DOM article on phpPatterns "The Way of the Widget" where Harry used the dom to generate html? I smirked at the idea of using something like that where no designer in my area could ever dream of modifying the html

    Anyway, templating really depends on the skillset of the designer... if you have one of those designers who like their pretty GUI designer and have no clue what <abbr> would be used for, and don't like touching the "html source code" because they're a "graphical person", you may be a little stuck. I used smarty once and the designer complained the site looked completely broken in goLive, and when they edited something it broke it further, same thing with using xsl.

    But designer capabilities aside, I like the XSLT route myself. I've done something where I had a visitor that converted objects to xml, combined it with xsl, and served it up as xhtml. The only drawback I see is there are possible speed issues, which in that case I would suggest just using php as the templating language (since a lot of designers do know a little php to be comfortable with it). Just keep business logic seperated and php as sparse as possible.

    Here would be a good example, say you were generating a table of employees:

    PHP Code:
    <table id="employees">
     <thead>
      <tr>
       <th>Last Name</th>
       <th>First Name</th>
       <th>Department</th>
       <th>Hire Date</th>
      </tr>
     </thead>
     <tbody>
      <?php foreach(Employees::getAllEmployees() as $employee) { ?>
       <tr>
        <td><?php echo $employee->getLastName(); ?></td>
        <td><?php echo $employee->getFirstName(); ?></td>
        <td><?php echo $employee->getDepartment(); ?></td>
        <td><?php echo $employee->getHireDate(); ?></td>
       </tr>
      <?php ?>
     </tbody>
    </table>
    Basically you'd just be doing the same thing that java programmers use JSPs for the output from their servlets. Some may shun the idea of the fact some php code is there, but it is the fastes way with the least overhead imho. With an xml structure of a nodelist of employees, you could write the above as:

    Code:
    <table id="employees">
     <thead>
      <tr>
       <th>Last Name</th>
       <th>First Name</th>
       <th>Department</th>
       <th>Hire Date</th>
      </tr>
     </thead>
     <tbody>
      <xsl:for-each select='/Employees/employee'>
       <tr>
        <td><xsl:value-of select="./lastName"/></td>
        <td><xsl:value-of select="./firstName"/></td>
        <td><xsl:value-of select="./department"/></td>
        <td><xsl:value-of select="./hireDate"/></td>
       </tr>
      </xsl:for-each>
     </tbody>
    </table>
    Basically choose something that best serves your needs, but I really do need to suggest somethng about templates before you go using them. If it's a professional project for a client, you may want to consider using a pre-written templating engine like smarty, WACT's, PEAR::Sigma, or that other framework with a smilin shroom as it's mascot... because writing a templating engine is a project in itself that can very much overshadow the time and effort working on actual business logic, not to mention the documentation you'll need to write for others to use it!

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  10. #35
    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 OfficeOfTheLaw
    The only drawback I see is there are possible speed issues
    I think xslt has gotten a rumor of being incredibly slow. Surely, with very traffic heavy sites this might become an issue, but aren't we looking at a clearcut case of premature optimizatin here ? Besides - with some caching applied it makes no difference.

    I'd say a more impeding problem of the xml->xslt->xhtml route is that the whole chain has to be wellformed xml to work painlessly. Designers using visual tools such as golive don't like being told that their code should comply to xhtml-strict. And even worse - content might be violating the rules aswell.

  11. #36
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    Its separation of logic from presentation, not code from presentation. HTML is still code, but it doesnt contain any logic.
    And your example is simple enough for a situation where you only need to replace 3 dataholders. It gets ugly when that number starts to increase.
    And i dont want to create a lot of functions for each and every dataholder:
    Code:
    function header() {
    echo 'Hello world';
    }
    It's easier to just use getters and setters! You can even create a generic get() that accepts an x amount of params:

    Code:
    <div id="productList">
    echo $this->get('productList', $total_per_page=15, $show_pager=true);
    </div>
    Your main get method checks for a method called getProductList(), if it exists... call it with the params, return the results. If not, look for a generic variable called 'productList', that has been set by set('productList', $list) etc.

    There are way to keep content editors away from PHP, even if it's used in the templates. Forms!

  12. #37
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like there are plenty of people, reinventing the hot water!

  13. #38
    SitePoint Zealot
    Join Date
    Aug 2005
    Location
    Bucharest, Romania
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The use of template is based on a main need: separate the design from logic.
    This way, the programmer can do his job without affecting the design, and the designer can do his job, without interfeering with the coding process.

    When code flexibility is needed on the design side (situations when smarty's language is used ), a simple MVC approach can solve the issue, the view can act as a templating engine:

    [php]
    <?php foreach($item->findAll() as $row) { ?>
    <tr>
    <td><?=$row['info']?></td>
    <!-- other columns -->
    </tr>
    <?php } ?>

    The View can provide the means to access data needed for displaying. This way, you don't need to pass huge amounts of data to a template class, no extra running time to parse some template files and so on.

    But everyone can choose its own path and a suitable templating engine or he can write one. But there is no point in reinventing the wheel.

    <offtopic>
    I would really like a comparison between templating engines, and I found a topic dedicated to a list of templating engines. maybe someone will dedicate time to review some of them, compared with others widely used.
    </offtopic>

  14. #39
    SitePoint Enthusiast SТОRМ's Avatar
    Join Date
    Sep 2004
    Location
    Ukraine
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excuse me for replying to an old topic, but I just don`t understand one thing. When someone tells, that when you use <?php ... ?> in your templates, you create a security issue - isn`t that ridiculous, I mean I understand that a designer can add any code he wants etc. BUT when you get ready templates from a designer, don`t you check them ? For example I always check them becouse I`m interested to know what a designer have done. So if a designer adds some PHP that shoudn`t be there you can see it BEFORE you add templates to your site. And then you can kick a designers *** for that he is doing things he shoudn`t do.

  15. #40
    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 SТОRМ
    Excuse me for replying to an old topic, but I just don`t understand one thing. When someone tells, that when you use <?php ... ?> in your templates, you create a security issue - isn`t that ridiculous, I mean I understand that a designer can add any code he wants etc. BUT when you get ready templates from a designer, don`t you check them ? For example I always check them becouse I`m interested to know what a designer have done. So if a designer adds some PHP that shoudn`t be there you can see it BEFORE you add templates to your site. And then you can kick a designers *** for that he is doing things he shoudn`t do.
    It depends on your thoroughness, your routines and what kind of template designer you're talking about.

    Anything that has to be checked manually is susceptible to human error. And some template designers may be your customers, as in users of a blog service. You might not want to kick their ***s too hard.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  16. #41
    SitePoint Enthusiast SТОRМ's Avatar
    Join Date
    Sep 2004
    Location
    Ukraine
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you dagfinn, now I see...

  17. #42
    SitePoint Zealot metacube's Avatar
    Join Date
    Jun 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, the security aspect depends very much upon the application and it's intended audience. I recently ditched a full-blown sef-written template parser in favour of including pre-compiled php files coupled with some nifty caching.

    Whereas my previous templates looked like this:

    HTML Code:
    {$include->header.tpl.php}
    
    <body>
        <h1>{$settings->site_name} &raquo; {$title}</h1>
        <p>Welcome back to the site, {$username}! This page shows the current products we have on offer.</p>
    
    {$products}
        <table>
            <thead>
                <tr>
                    <td>ID</td>
                    <td>Name</td>
                    <td>Text</td>
                </tr>
            </thead>
        {$products->data}
            <tr>
                <td>{$id}</td>
                <td>{$name}</td>
                <td>{$text}</td>
            </tr>
        {/$products->data}
    </table>
        {$products->nodata}
            <p>There are no products!</p>
        {/$products->nodata}
    {/$products}
    
    <p>Your role is: {$userrole}<br />
    Your title is: {$usertitle}</p>
    
    {$include->footer.tpl.php}
    The new ones look like:

    PHP Code:
    {$include->header.tpl.php}

    <h1><?=$title;?></h1>

    <p>Welcome to {$settings->site_name}!</p>

    <?php if(isset($users) && count($users)>0): ?>
        <table>
            <thead>
                <tr>
                    <td>{$lang->name}</td>
                    <td>{$lang->role}</td>
                </tr>
            </thead>
        <?php foreach($users as $user): ?>
            <tr>
                <td><?=$user['name'];?></td>
                <td><?=$user['role'];?></td>
            </tr>
        <?php endforeach; ?>
        </table>
    <?php else: ?>
        <p style="color:red">{$lang->no.users}</p>
    <?php endif; ?>

    {$include->footer.tpl.php}
    The second version may seem a little harder to write, and does require some PHP knowledge, but then again, for this application the people writing templates will invariably have that knowledge.

    What this system does is to load in the template file and using some preg_replace_callbacks replaces the include, lang and settings directives with the required variables and includes. Then the entire template is stored in cache as a regular php file. This file is then included by the the $template->display() function. First time compilation takes a little longer, but after that it only needs to actually include the file and parse the variables into it. All done!

    Sure, someone could put print_r($GLOBALS) into the template somewhere, but they'd just be hurting themselves.

    For my particular application, this works fine. It has a nice seperation of business and presentation logic (the template simply assumes all the possible variables are actually present and then decides what to do with them), but no seperation of code and markup (which, strictly speaking is code anyway). For some apps this might not suffice, of course. I guess that it depends on the nature of the application.

    The benefit of using PHP as presentation logic language? It's easy! No need to scour the template, looking for placeholders, loop blocks and replacement blocks. Saves in time when writing the app, saves in time when creating the templates and saves in time when rendering pages.

  18. #43
    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)
    @metacube

    I don't get it. Why do this :
    HTML Code:
    {$include->header.tpl.php}
    <p>Welcome to {$settings->site_name}!</p> 
    Instead of this :
    PHP Code:
    <?php include('header.tpl.php'); ?>
    <p>Welcome to <?=$settings->site_name?>!</p>

  19. #44
    SitePoint Zealot metacube's Avatar
    Join Date
    Jun 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple: I want to pre-render the template, as it were. This means that a lot of standard variables don't need to be loaded when using the cached template, saving includes and db queries. Also, the includes are added into the template using simple text replacements, and not with proper PHP includes. I could do it with PHP, but the problem then is that the bits of PHP code that I want to keep are removed and there'll be a load of errors due to undefined variables etc. This method, while not the most elegant, certainly works te best for me

  20. #45
    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 metacube
    Simple: I want to pre-render the template, as it were. This means that a lot of standard variables don't need to be loaded when using the cached template
    So basically, you're caching the static content, but still rendering the dynamic content. And to keep the two separated, you have two template-languages (php for dynamic content and smarty-like-syntax for static content) ?
    I believe I heard that this strategy also is used in the smarty templating engine. I never really bothered doing something like that - rather I have a page be either static or dynamic. I can see the advantage of it though.

  21. #46
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by metacube
    Simple: I want to pre-render the template, as it were. This means that a lot of standard variables don't need to be loaded when using the cached template, saving includes and db queries. Also, the includes are added into the template using simple text replacements, and not with proper PHP includes. I could do it with PHP, but the problem then is that the bits of PHP code that I want to keep are removed and there'll be a load of errors due to undefined variables etc. This method, while not the most elegant, certainly works te best for me
    Could you post a download for your code? Just curious to see how you are doing it.

    -matt

  22. #47
    SitePoint Zealot metacube's Avatar
    Join Date
    Jun 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @kyberfabrikken:

    Yeah, and that is something that still needs some work. Ideally, it would
    use just php, but I need to figure out how to build the full template without it throwing errors all over the place and without it stripping the dynamic PHP out >.< . It's still early days, so I'll prolly end up refining it as I put it to use. For now, it works fine

    @matt:

    It's part of a larger application, so some of the properties and methods used aren't in this snippet. But here ya go:

    PHP Code:
    class OutputHandler extends InputHandler {

        var 
    $tpl_vars = array();

        function 
    assign($name$value) {
            
    $this->tpl_vars[$name] = $value;
        }

        function 
    assign_vars($array) {
            if(
    is_array($array)) {
                foreach(
    $array as $key => $value) {
                    
    $this->tpl_vars[$key] = $value;
                }
            }
        }

        function 
    display($file$return false) {
            
    // check for compiled tpl
            
    $c_file $this->cache_get_file_path($file'templates');
            if(!
    file_exists($c_file)) {
                
    $c_template file_get_contents(MC_TPL_PATH.'/'.$file);
                for(
    $i=0$i<2$i++) { // loop twice to make sure any includes are parsed properly
                    
    $c_template preg_replace_callback('~{\$include->(.[^}]*)}~', array(&$this'parse_includes_callback'), $c_template);
                    
    $c_template preg_replace_callback('~{\$lang->(.[^}]*)}~', array(&$this'parse_lang_callback'), $c_template);
                    
    $c_template preg_replace_callback('~{\$settings->(.[^}]*)}~', array(&$this'parse_settings_callback'), $c_template);
                }
                
    $this->cache_save_cache($file$c_template'templates');
                if(
    MC_DEBUG) { $this->new_debug_entry('Pre-Render Template `'.$file.'`;'); }
            }
            
    extract($this->tpl_vars);
            
    ob_start();
            include(
    $c_file);
            
    $contents ob_get_contents();
            
    ob_end_clean();
            if(
    MC_DEBUG) { $this->new_debug_entry('Include Template `'.$file.'`;'); }
            if(
    $return) {
                return 
    $contents;
            } else {
                echo 
    $contents;
            }
        }

        function 
    parse_lang_callback($m) {
            if(isset(
    $this->lang[$m[1]])) {
                return 
    $this->lang[$m[1]];
            } else {
                return 
    '[undefined lang var: '.$m[1].']';
            }
        }

        function 
    parse_includes_callback($m) {
            
    $tpl_file MC_TPL_PATH.'/'.$m[1];
            if(
    file_exists($tpl_file)) {
                
    $include file_get_contents($tpl_file);
                return 
    $include;
            } else {
                return 
    '[include : `'.$tpl_file.'` could not be found]';
            }
        }

        function 
    parse_settings_callback($m) {
            if(isset(
    $this->settings[$m[1]])) {
                return 
    $this->settings[$m[1]];
            } else {
                return 
    '[undefined setting var: '.$m[1].']';
            }
        }

    By the way, I was inspired by this article: http://www.sitepoint.com/article/beyond-template-engine

  23. #48
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Santa Rosa, CA
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My favorite templating engine is...PHP. Seriously, I have never, ever, ever understood what the need is for anything beyond what PHP gives you. All you need to do is provide some simple variables and include the template file inside of a template class. PHP Savant's a pretty good example of this, but I rolled my own that's even simpler.

    If a designer can't handle a few
    PHP Code:
    <?php echo $something ?>
    and
    PHP Code:
    <?php foreach ($things as $thing): ?>
    ***
    <?php endforeach; ?>
    then they have no business called themselves Web designers. Learn the tools you use or get out of the business.

    Jared (someone who was into the graphics and visual HTML work long before he knew anything about PHP and SQL and all that stuff)
    Last edited by JaredWhite; Oct 6, 2005 at 12:31. Reason: Fixed a typo

  24. #49
    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 metacube
    (...) but I need to figure out how to build the full template without it throwing errors all over the place
    You could simply lower the error-reporting level: error_reporting(E_ALL ^ E_NOTICE);
    Quote Originally Posted by metacube
    (...) and without it stripping the dynamic PHP out
    That's why you have two syntaxes - if you have a look at some of the code-generating tools out there (such as propel and other ORM's), you'll see they have the same-problem and solution.

  25. #50
    SitePoint Zealot metacube's Avatar
    Join Date
    Jun 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken, thanks for the tip. What's an ORM? I've never heard that term before...


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
  •