nanotemp

Compatible with: PHP 4, PHP 5

nanotemp — A simple template engine

Description

string nanotemp ( string $template , array $data [, bool $file = true ] )
Returns the template with data strings in it. If file is set to TRUE, template is interpreted as the path to the template file, otherwise it is parsed as the template itself. $$ is used as the default marker, but it can be overridden by setting the value of $data["marker"].

Parameters

template
The template string.
data
The data array.
You can override the marker by setting the value of $data["marker"].
file
If file is set as TRUE, template will be interpreted as the template file path.

Source code

Source code of nanotemp()

<?php
function nanotemp($template$data$file true)
    {
    if(!isset(
$data['marker'])) $data['marker'] = '$$';
    if(
$file$input explode($data['marker'], file_get_contents($template));
    else 
$input explode($data['marker'], $template);
    for(
$i 0$ret ''$num count($input); $i $num$i++)
        {
        if(
$i === 0$ret.= $input[$i];
        else if(isset(
$data[$input[$i]])) $ret.= $data[$input[$i]];
        }
    return 
$ret;
    }
?>
You can download raw source here.

Example

Example #1 nanotemp() example

<?php
$template 
'Marker: $$marker$$<br />
Template engine: $$engine$$<br />
Coded by: $$coded$$'
;

$output["engine"] = "NanoTemp";
$output["coded"] = "Sh1fty";
echo 
nanotemp($template$outputfalse);
?>

The above example will output:

Marker: $$<br />
Template engine: NanoTemp<br />
Coded by: Sh1fty<br />
	

License

This engine is public domain, and as such can be used in any way imaginable. I would however appreciate it if you mentioned nanotemp when you use it and put a link to this manual.
Last updated: Mon, 17 Feb 2014