Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2002

    How to replace many placeholders in a template in PHP?

    Suppose I create a HTML template and want a PHP script to dynamically replace many placeholders (something like {valuename}) with values. Is there is a more efficient way than to search and replace the whole template for every value each time anew?

  2. #2
    Join Date
    Jul 2003
    You can cache the resultant html and only regenerate it if there are changes in the template.

    Regular expression matching would be best in this situation.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  3. #3
    Join Date
    Jun 2002
    Originally posted by fyrestrtr
    You can cache the resultant html and only regenerate it if there are changes in the template.
    Thanks for your reply. As my knowledge is still relatively limited, I would thank you if you could explain what this means... How do I do that?

  4. #4
    Why not use the smarty template engine? it does that for you very effectively.

  5. #5
    Join Date
    Nov 2001
    I like smarty, it compiles the template so you can design with as many include files and you wish and it only has to include them once. It is also very flexible you can use:
    PHP Code:
    require_once 'Smarty.class.php';

    $somevar "This is what I am talking about";

    // Normal
    $smarty = new Smarty;

    // Fetch
    $fetchsmarty = new Smarty;
    $html $fetchsmarty->fetch("page.tpl");

    // or pretty much anything you want.
    then I would suggest adding MMCache to your php install
    this keeps the ZIF in memory(zend intermediary file, the parsed php file right before it is executed by the zend engine).

    you could also use ionCube's php accelerator, or zend's optimizer, but since the main developer of MMCache was hired by zend, I tend to think very highly of it's quality.

  6. #6
    Great suggestion.. I SWEAR by Turck..

    I run a server that runs a *LOT* of php scripting.. (MMORPG games)

    after installing turck, my server's load average dropped a whopping 85%!

    currently, ~2,750 scripts cached, averaging 16,000 hits per day

    Definitely Turck is an awesome server-saver for php heavy servers..

  7. #7
    Join Date
    Jun 2002
    Thanks! this suggestion looks great. I think it will solve my problem!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts