Web Hosting Forums

Results 1 to 8 of 8

This is a discussion on Template like stuff with PHP?? in the Hosting Talk & Chit-chat forum
Does anyone know how to do those templates in PHP that basically look like this: Code: <html><head> <h1>This is where the {HEADER} goes</h1> etc, etc. ...

  1. #1
    Loyal Client
    Join Date
    Aug 2002
    Posts
    266

    Template like stuff with PHP??

    Does anyone know how to do those templates in PHP that basically look like this:

    Code:
    <html><head>
    <h1>This is where the {HEADER} goes</h1>
    etc, etc.
    I've got a script where I want the HTML cosmetics person to only have to deal with a simple HTML file that has no PHP in it (except for these {} keywords}.

    Am I even making any sense?

  2. #2
    Loyal Client the_ancient's Avatar
    Join Date
    Feb 2004
    Posts
    3,474
    Depends on your needs and skill level... there are several templating Engines out there (google for "php template engine" ) more common ones are

    Smarty
    patTemplate
    xTemplate
    bTemplate

    Or you can write your own, if you want to replace just "{mytag}" type things you can create a regex to evaluate everything in the braces, or you can make an array with specific tags, then use the function str_replace, or preg_replace, to search the template file and replace the tags with real content
    -------------------------
    the_ancient
    MP Technology Group

  3. #3
    Loyal Client
    Join Date
    Aug 2002
    Posts
    266
    My skill level is fair... so do I just read in this separate HTML file as if I'm reading in a text file, and do my str_replace stuff on it and spit it out?

    I guess my concern is that I'm going to open a can of worms with quotes and having to escape certain characters and whatnot. Or is it just a matter of reading it in and spitting it out unaltered (except for replacing the templated stuff)?

    Thanks for the quick response.

  4. #4
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Adding to that short list:

    Savant
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  5. #5
    CTO JPC-Masood's Avatar
    Join Date
    Aug 2002
    Location
    Jaguar Servers
    Posts
    1,916
    Like the_ancient said, try this instead of writing your own:

    http://smarty.php.net/
    [COLOR="DarkRed"]Masood N. [/COLOR] | [COLOR="Red"]Chief Technical Officer[/COLOR][SIZE="3"] JaguarPC.com[/SIZE]

  6. #6
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    I'm a big fan of Smarty, but it is a rather complex system if you just want to do some simple variable substitution.

    If you only wnat to replace a handful of variables with text and you don't need to worry about any complexities like loops or conditionals then the process you describe (read in file, run str_replace on the keys, print output to browser) is the easiest way to do it. You don't have to do anything special, just add the {var} tags to your normal HTML file and you'll be good to go.

    If you do need the advanced features then you'll be better off using one of the existing systems. Like I said, I'm a Smarty fan (I used to be a patTemplate fan, that one's good too), but your best bet is to play with a couple of different systems and see what works best for your skill level and way of thinking, as all systems tend to implement the same concepts in very different ways.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  7. #7
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,661
    Sorry, I still don't understand the question, and apparently nobody else does either...
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL 2010

  8. #8
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Quote Originally Posted by jason View Post
    all systems tend to implement the same concepts in very different ways.
    Not all the same concepts. There are two different concepts in play:
    1) They all help with the very wise desire to seperate the view (the template) from the content.

    2) Many of them, including the popular Smarty, also introduce a new language syntax, to write the templates in. That often doesn't make much sense and comes with significant downsides attached.

    Plus:
    -> You can allow people that you do not fully trust to freely alter the templates. That probably means the users themselves, on a place like MySpace, so.. does this apply to your project?
    -> The template syntax is ideally simpler to fathom for a designer than a template in PHP. Unfortunately, rarely significantly so.

    Minus:
    -> As the PHP programmer, you have an extra syntax to learn, even though PHP is a perfectly good templating language itself; heck it started out as nothing more than a templating language.
    -> All templates must undergo and extra parsing step, to translate them into PHP for the engine to swallow. This is extra work for your server and a lot of extra complexity that can fail you. So, if you take a templating engine with its own syntax: do make sure it solves this self-created overhead by caching the "compiled" templates and do make sure to pick a mature one, as you don't want a needless headache over this yourself.

    Now, some of them "defend" the extra syntax by stating that it helps in achieving principle 1. That's bullocks. That principle is a mindset, not software enforceable. Views invariably have need of some "view logic" too, so a template engine simply needs some constructs like if-then and foreach. As soon as you have those you can (ab)use them for non-view logic just as easily.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

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
  •