SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 94

Hybrid View

  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 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?

  4. #4
    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!

  5. #5
    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.

  6. #6
    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

  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
    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!

  8. #8
    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.

  9. #9
    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?

  10. #10
    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.

  11. #11
    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!

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    It's not an opinion, its a fact.
    No, it's an opinion. A templating engine would be "redundant" if it did not have advantages of its own. However, a good one does.

    Quote Originally Posted by imaginethis View Post
    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.
    You shouldn't depend on <?= or <? for portability reasons (and who knows, maybe it'd become deprecated one day). So, you're stuck with <?php echo and <?php.

    PHP Code:
    <?php foreach($users as $user): ?>
    <a href="/<?php echo htmlspecialchars($user?>">
      <?php echo htmlspecialchars($user)?>
    </a>
    <?php endforeach; ?>
    That's right. Ugly once again.

    Plus, that's a relatively simple block of code. Soon you have yourself <a href="<?php echo htmlspecialchars($user_id)?>"<?php if ($should_be_colored): ?> style="color: <?php echo htmlspecialchars($color)?>"<?php endif; ?>><?php if ($username): ?><?php echo htmlspecialchars($username)?><?php else: ?><em>(unknown)</em><?php endif; ?></a>

    If that were written in Smarty, it would be shorter and much easier to read. Plus, since Smarty's language doesn't make use of < and >, characters muddling themselves in (X)HTML look-alike syntax, it's even better.

    On top of all of this, if I want to add another escape function or so (such as one for JavaScript), I would have to muddle the global namespace with it*. One could put it in an object, but then you'd just make the template code even longer.

    *Though in PHP 5.3, functions are first class objects, but we're not there yet.

  13. #13
    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)
    PHP Code:
    <?php
     
    foreach($users as $user) {
      echo 
    "<a href=\"/" htmlspecialchars($user) . "\">\r\n";
      echo 
    htmlspecialchars($user) . "\r\n";
      echo 
    "</a>\r\n";
     }
    //foreach
    ?>
    Looks fine to me :P

  14. #14
    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"

  15. #15
    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!

  16. #16
    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

  17. #17
    SitePoint Addict
    Join Date
    Nov 2006
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    if you don't feel like ever using a template system again read this. most importantly the posts by voostind

    The use I see is not having to type out the company name out when you install a cms it just copies it everywhere... but for real programmers... ??
    Please...Never describe anything to me using foo and bar.

  18. #18
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    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.
    Then learn braceless syntax. <?php foreach( $array as $value): ?> some html here <?php endforeach; ?>

  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)
    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

  20. #20
    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!

  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 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 03:33. Reason: edited for clarity
    Ryan B | My Blog | Twitter

  22. #22
    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!

  23. #23
    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.

  24. #24
    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!

  25. #25
    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


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
  •