PHP – Template Engine in One Line of Code


templateEngine.php:

<?php

print preg_replace(“/\{([^\{]{1,100}?)\}/e”,”$$1″,file_get_contents(“template.tpl”));

?>

template.tpl:

<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>{header}</h1>
{text}
</body>
</html>

variables:

$title=”Example page”;
$header=”My Examples”;
$text=”See the placeholders replaced?”;

 

REGEX Explanation:

"/\{([^\{]{1,100}?)\}/e","$$1"
I’m delimiting the regular expression with / / and using the modifier ‘e’ which causes the second argument to be evaluated by PHP.

The pattern looks for an opening curly-brace ( \{ ) – the end of the pattern is a closing curly-brace ( \} ).

In between the two braces I look for any character which isn’t an opening curly-brace [^\{], avoiding mistaken nesting of tags.

I match between 1 and 100 of these non-{ characters by writing {1,100} and then I make the match non-greedy (try to find the shortest strings between { and }, not the longest) by adding a?. (? after *, + or {a,b} expressions changes them to non-greedy – in other situations? means 0 or 1 of the preceding).

The full string of non-{ characters is matched and stored as string $1 by surrounding that part of the pattern with brackets ().

Finally, the second argument of the preg_replace is “$$1”, using variable variables. If the pattern encounters “{title}” then the matched string $1 is “title” and so $$1 is $title. 

 

source:

http://www.webmasterworld.com/php/3444822.htm

Advertisements

Install Smarty in Windows


This document assumes that your webserver and php5 is running.

Download Smarty - http://smarty.php.net

Installation - Windows, IIS/Apache, PHP5
Extract files, rename Smarty.x.x.x to smarty (suggest OUTSIDE of your www root!)
Example: d:\smarty
Run phpinfo.php to find out your php.ini location
Edit php.ini's include_path and add the location of the libs folder.
example: include_path = ".;d:\smarty\libs"
Restart IIS/Apache
Setup these two folders INSIDE your www root:
(wwwroot)/smarty/templates  (this is where your templates will go)
(wwwroot)/smarty/configs

Setup these two folders OUTSIDE of your www root:
d:/smarty/templates_c
d:/smarty/cache

Setup security settings for the webserver to write to these four folders

In (wwwroot) create index.php and in (wwwroot)/smarty/templates/index.tpl with the following code:

index.php:
<?php
// load Smarty library
require('Smarty.class.php');

$smarty = new Smarty;

$smarty->template_dir = 'd:/inetpub/wwwroot/smarty/templates';
$smarty->config_dir = ' d:/inetpub/wwwroot/smarty/config';
$smarty->cache_dir = 'd:/smarty/smarty_cache';
$smarty->compile_dir = 'd:/smarty/smarty_templates_c';

$smarty->assign('name','fish boy!');

$smarty->display('index.tpl');
?>

index.tpl
<html>
<body>
Hello, {$name}!
</body>
</html>

Now open index.php in your web browser (requested from your webserver)

http://localhost/index.php

You can work this out to a referenced script/class:
smarty_connect.php:
<?php

// load Smarty library
require('Smarty.class.php');

class smarty_connect extends Smarty
{
   function smarty_connect()
   {
        // Class Constructor.
        // These automatically get set with each new instance.

		$this->Smarty();

		$this->template_dir = ' d:/inetpub/wwwroot/smarty/templates';
		$this->config_dir = ' d:/inetpub/wwwroot/smarty/config';
		$this->compile_dir = 'd:/smarty/templates_c';
		$this->cache_dir = 'd:/smarty/cache';

		$this->assign('app_name', 'Intranet');
   }
}
?>

index.php:
<?php
require('smarty_connect.php');

$smarty = new smarty_connect;

$smarty->assign('name','Ned');

$smarty->display('index.tpl');
?>

index.tpl:
<html>
<body>
 Hello, {$name}!
</body>
</html>

If you are getting an error that Smarty.class.php isn't found
chances are that your include_path isn't correct or
you didn't edit the one that the webserver is using,
check your phpinfo.php!

source:
http://news.php.net/php.smarty.dev/2703