Hi, I'm experimenting with using templates. Is this the best/standard/optimal way to do things?

I am basically loading the file, doing a str_replace() on it and then outputting it. I am matching <{ "foo" }> and foo is stored in the array.

<?

function template($pattern,$replace,$file) {

$file = implode("",file($file));

foreach($pattern as $key => $value) {
	$pattern[$key] = "<{ \"".$value."\" }>";
}

echo(str_replace($pattern,$replace,$file));
}

$pattern[] = "conf_title";
$replace[] = "Hello World!!! I am working";
$pattern[] = "lang_welcome";
$replace[] = "Welcome to the templates test";

template($pattern,$replace,"./helloworld.tpl");

?>

helloworld.tpl

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><{ "conf_title" }></title>
<meta http-equiv="Content-Type"
  content="text/html; charset=iso-8859-1" />
</head>

<body>
<p><{ "lang_welcome" }></p>
</body>
</html>

Thanks !

  • Damo

    I would do something like this:

    <?php

    function template($file, $replace)
    { if (! file_exists($file))
    die("template file not found!");

    $file = implode("",file($file)); 
    
    $new_file = strtr($file, $replace);
    
    echo $new_file; 

    }

    $replace = array
    ( "[xxx:conf_title]" => "Hello World!!! I am working",
    "[xxx:lang_welcome]" => "Welcome to the templates test"
    );

    template("./helloworld.tpl", $replace);

    ?>

    helloword.tpl:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>[xxx:conf_title]</title>
    <meta http-equiv="Content-Type"
    content="text/html; charset=iso-8859-1" />
    </head>

    <body>
    <p>[xxx:lang_welcome]</p>
    </body>
    </html>

    So You can see Your placeholders even if they are not correct substituted.

      Originally posted by Avochelm
      Hi, I'm experimenting with using templates. Is this the best/standard/optimal way to do things?

      Short, simple, to the point, and offers the chance for expansion in future; a good start. Enhancing the template syntax to allow more programmatic inclusions in future would mainly involve changing str_replace to preg_replace_callback. In fact, using preg_replace would allow a little more flexibility (<{"lang_welcome"}> won't at present be recognised, for example). I'd also throw in a check to wipe out any unrecognised tags and replace them with empty strings.

        Hi, Thanks for your feedback! I have taken your advice and updated the function to allow for the absence of a space. I also added in an extra RegEx to remove empty tags.

        <?
        function template($pattern,$replace,$tpl) {
        
        $tpl = implode("",file($tpl));
        
        foreach ($pattern as $key => $value) {
        	$pattern[$key] = "/<\{ *\"".$value."\" *\}>/";
        }
        
        $pattern[] = "/<\{.*\}>/";
        $replace[] = "<!-- Notice: Unmatched tag removed! -->";
        
        $tpl = preg_replace($pattern,$replace,$tpl);
        
        echo($tpl);
        }
        ?>

        As a result of my RegEx, I can now have an infinate amount of spaces inbetween the <{ and }>. How flexible! 🙂

          I just make my templates php pages and then include them.

            I just make a bunch of functions that echo out the code and put them in a include file EX:
            fucntion my_header($page_title) {
            echo '<html><head><title>' . $page_title . '</title></head>';
            }
            and then just call them when I need them 🙂 I figure template sytems would be to much slower, especially on my computer 🙁

              I prefer to separate my templates from PHP; I've been in situations where apps written in other languages had to play with the templates, and I wasn't about to write enough of a PHP interpreter in each language.

                Oh, the templates system by far is the easiest way of handling a project of large magnitude. When combined with your text in a language file, it is truly a sturdy combination -easily customisable and easily translatable.

                Templates also have the benifit of greater control of the output and the frontend doesn't get in the way of your scripts. You just set the variables and echo out everything when your done. This is very helpful when dealing with sessions and headers.

                  Originally posted by Weedpacket
                  I prefer to separate my templates from PHP; I've been in situations where apps written in other languages had to play with the templates, and I wasn't about to write enough of a PHP interpreter in each language.

                  I limit myself to variables and loops so it wouldn't be hard to write a parser if I had to, but I havn't had that happen yet.

                    One quick question pertaining to Regular Expressions: If I have .* (one or more of anything), how would I make it so that "anything" includes line returns. Currently If I have something like "<{ foo\n }>", where there is a line return, it does not pick it up.

                    Eg: "/<{.*}>/" wont match:

                    <{
                    foo
                    }>

                      If you're using he PCRE functions then use the s modifier
                      "/<{.*}>/s"
                      HTH
                      Bubble

                        Originally posted by drawmack
                        I limit myself to variables and loops so it wouldn't be hard to write a parser if I had to, but I havn't had that happen yet.

                        Okay; if you're using a limited subset of PHP then it's pretty much the same as a dedicated template markup language. The only problems I see involve things like the potential for namespace collisions, and templates being written by people who don't really know what they're doing (or worse, think they know what they're doing) munting the script when they're included.

                          Write a Reply...