SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 94
  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Template System Techniques

    Hiya!,

    I currently planning to make my own template system, but am intrigued to know what the different techniques are in order to link the templates with the PHP code.

    I know of one method, "Regular Expressions".

    Also, what method does the following class use?

    http://www.phpclasses.org/browse/file/14674.html

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's been said a few hundred times, PHP is a template language. Creating a template language on top of a template language is redundant.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a matter of opinion. I find <?php foreach ($users as $user) { ?><a href="/<?php echo htmlspecialchars($user) ?>"><?php echo htmlspecialchars($user) ?></a><?php endforeach; ?> exceptionally hard on the eyes and a real pain to write. After I've mocked up a design in Photoshop and I want to write its corresponding HTML code, I don't want to waddle in such ugly code spaghetti. You also can't cleanly add template-specific functions to be used in only templates due to PHP's handling of scope.

    You might want to look into how Smarty does it.

  4. #4
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    It's been said a few hundred times, PHP is a template language. Creating a template language on top of a template language is redundant.
    If it's a template language, how do you separate the Processing (PHP) from the Mark-Up (HTML) code?

  5. #5
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    That's a matter of opinion.
    It's not an opinion, its a fact.

    Quote Originally Posted by sk89q View Post
    I find <?php foreach ($users as $user) { ?><a href="/<?php echo htmlspecialchars($user) ?>"><?php echo htmlspecialchars($user) ?></a><?php endforeach; ?>
    PHP Code:
    <? foreach($users as $user): ?>
    <a href="/<?= htmlspecialchars($user?>">
      <?= htmlspecialchars($user)?>
    </a>
    <? endforeach; ?>
    Not so hard on the eyes. This is hardly much more than what you'd have in smarty. It's not hard to write and there's no ugly spaghetti code to "waddle" through.

    Quote Originally Posted by sk89q View Post
    You also can't cleanly add template-specific functions to be used in only templates due to PHP's handling of scope.
    Can't argue with that. But I like the thought of being able re-use my code.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  6. #6
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    {foreach from=$myArray item=foo}
        <
    li>{$foo}</li>
    {/foreach} 
    PHP Code:
    <?php foreach($myArray as $foo?>
        <li><?= $foo ?></li>
    <?php endforeach; ?>
    looks the same to me. Any there's no smarty overhead =]

    AND you dont have to learn another "language"

  7. #7
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    If it's a template language, how do you separate the Processing (PHP) from the Mark-Up (HTML) code?
    MVC
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  8. #8
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    AND you dont have to learn another "language"
    Or create one

    But seriously, if your going to make a better template language to improve upon PHP, write it in C
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  9. #9
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you don't feel like ever using a template system again read this. most importantly the posts by voostind

  10. #10
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Hiya!,

    I currently planning to make my own template system, but am intrigued to know what the different techniques are in order to link the templates with the PHP code.
    Honestly I am not sure why people use templates...

    sample.php
    PHP Code:
    <html>
    //header stuff
    <body>
    <?php
    include('nav.php');
    include(
    'sidebar.php');
    ?>
    <div class="mainContent">
    .......
    </div>
    <?php include('footer'); ?>
    </body>
    </html>
    in templates all you would be editing is the stuff in mainContent anyhow
    Ryan B | My Blog | Twitter

  11. #11
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    Yes, but you need a way to link the Processing (PHP) to the Mark-Up (HTML) code.

  12. #12
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Yes, but you need a way to link the Processing (PHP) to the Mark-Up (HTML) code.
    Can you expand more? look up at my sample code. I used php to include another file, the browser looks at the code, sees I am calling a new file (nav.php), writes/processes the file (meshes nav.php into my original code) and spits out the page. If you then look at the source, you won't see a php include call but you would instead see the code in nav.php
    Ryan B | My Blog | Twitter

  13. #13
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Yes, but you need a way to link the Processing (PHP) to the Mark-Up (HTML) code.
    I think your missing the point. If PHP is the template language you don't need to separate HTML from PHP. You need only seperate Business Logic from View.

    Separating PHP from HTML with a template language is nothing more than putting a huge semaphore in the your code's execution saying WAIT FOR IT! WAIT FOR IT! WAIT FOR IT! KEEP WAITING! KEEP WAITING! THERE YOU GO . The shortest distance between any two points is a straight line. No matter how many triangles, circles, squares or squiggly lines you draw, you want get from point A to point B any faster than a straight line.

    +1 for geometry theorems being put to use >.>
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  14. #14
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    Honestly I am not sure why people use templates...

    sample.php
    PHP Code:
    <html>
    //header stuff
    <body>
    <?php
    include('nav.php');
    include(
    'sidebar.php');
    ?>
    <div class="mainContent">
    .......
    </div>
    <?php include('footer'); ?>
    </body>
    </html>
    in templates all you would be editing is the stuff in mainContent anyhow
    er... that is a template >.>...
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  15. #15
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    er... that is a template >.>...
    I wasn't saying it was >.> I was just saying I am not sure why people still use templates since you can make something like one in php, where you include files except where you put content... isn't that what a template does basically? Say a, b, and c is a fixed region, but D is where you have the ability to write content...
    Last edited by rguy84; Dec 12, 2008 at 02:33. Reason: edited for clarity
    Ryan B | My Blog | Twitter

  16. #16
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    I wasn't saying it was >.> I was just saying I am not sure why people still use templates since you can make something like one in php, where you include files except where you put content... isn't that what a template does basically? Say this, this and this is a fixed region, but that is where you have the ability to write content...
    In incredibly simple terms yes. I think I understand what you were attempting to say. "Honestly I am not sure why people use template languages"...
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  17. #17
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is dead on (see #10). If you want to be old school, look at ssi includes.
    Ryan B | My Blog | Twitter

  18. #18
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be some advantages of using a Template Engine, as everybody uses them.

    I recently looked at some jobs websites for PHP Developer roles (Just curious to see what some requirements are), and many of the positions require the knowledge of Zend and Smarty.

  19. #19
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Zend is not a template language. I would say it is a library (framework) that extends PHP. All Zend and Smarty does is make your life easier as a dev. Companies that require Zend/Smarty/Cake are saying is "We want somebody that uses his/her resources." Example: I just looked at the smarty site, it has a guestbook app built in... I could take a bit of time to make my own guestbook, OR use smarty's 'template':
    PHP Code:
    include('smarty.class.guestbook.php');
    $myGB = new SmartyGuestbook();
    ...
    echo 
    $myGB
    (note that is not real code, just a guess)... and that took me 30 seconds to make a guestbook
    Ryan B | My Blog | Twitter

  20. #20
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    There must be some advantages of using a Template Engine, as everybody uses them.

    I recently looked at some jobs websites for PHP Developer roles (Just curious to see what some requirements are), and many of the positions require the knowledge of Zend and Smarty.
    Smarty pareses its template files and store its compiled generated PHP code while giving you the possibility to generated cached HTML pages. There many websites that use smarty as well as many applications, like X-Cart -- a complete pain in the backside -- that currently use smarty. Those websites and applications still need maintenance, so knowledge of Smarty is beneficial... Several PHP frameworks have built in caching capabilities -- Zend Framework and CakePHP do -- so I don't see what benefit using any additional template language other than php would provide.

    Quote Originally Posted by rguy84 View Post
    Zend is not a template language. I would say it is a library (framework) that extends PHP.
    Zend is company, not a framework. Zend Framework is a PHP framework.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  21. #21
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis;4073717[URL="http://www.zend.com"
    Zend[/URL] is company, not a framework. Zend Framework is a PHP framework.
    hey I have been studying all day, take it easy eh. I think the OP got the drift...I hope
    Ryan B | My Blog | Twitter

  22. #22
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    If it's a template language, how do you separate the Processing (PHP) from the Mark-Up (HTML) code?
    Pretty easily? You don't have to output the HTML in the same file you keep all your logic.

  23. #23
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Pretty easily? You don't have to output the HTML in the same file you keep all your logic.
    How would you do that then?
    I'm now confused now, as so many people use template engines, and to me it seems a good idea (i.e. easier to edit pages). But after reading the information above, and viewing this thread, I don't want to waste my time developing something that doesn't actually need to be done.

    Many people state that PHP is already a template system, if this is true, how would you link the templates with the PHP?

  24. #24
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That thread shows exactly how. You can still keep your template in a separate file, just keep it as a php file.

    template.tpl.php (or whatever you want to call it):
    PHP Code:
    <html>
    <head>
    <title><?= $title ?></title>
    </head>

    <body>
    </body>
    </html>
    another file:

    PHP Code:
    //business logic
    if ($onething) {
    $title "another thing";
    } else {
    $title "something else";
    }
    //if

    include "template.php" 
    Personally I wouldn't use short tags though, and I normally don't bother with separate include files for templates - all my business logic is in the objects, and I keep limited display logic and html output in the same file, that makes use of these objects.

  25. #25
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So is PHP classed as a template system because you can include files? e.g. include('some_file.php');


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
  •