# sfTemperPlugin
The ``sfTemperPlugin`` is a template compiler based on a Kohana Framework's template parser called Temper.
## Syntax
Temper supports several kind of syntax inside templates. It uses three types of delimiters: ``{}``, ``{{}}`` and ````.
See [this page](http://code.google.com/p/temper/wiki/Documentation) for more details.
### Variables
{=variable} =>
{=variable|function} =>
{=foo.variable} => getVariable() ?>
{/path/to/} should be http://example.com/path/to/ but it is not yet implemented with the Symfony's routing system
{/path/to/=variable} should be http://example.com/path/to/ but (etc, etc)
### Functions
{{echo(%foo)}} =>
{{class::method({%foo}, 'arg', 'arg')}} =>
### Tags
Tags are represented internally as objects. It allows us to add custom tags or disable those that we don't need to improve global performance. See examples to see default tags syntax.
## Configuration
sfTemperPlugin parameters are configured in app.yml. These are the default values:
all:
sfTemperPlugin:
tags: [if, else, elseif, for, foreach, switch, case, print, block, import]
prefix: 't' # or
allow_php: false
allow_helpers: false
remove_comments: true
template_dir: 'templates' # maps to sf_upload_dir/templates
You customize the use of tags. For example, if I you want add a tag named ``invoice`` to be used in your templates:
all:
sfTemperPlugin:
tags: [if, else, elseif, for, foreach, switch, case, print, block, import, invoice]
You must create a new class that inherits from Temper_Tag:
class Tag_Invoice extends Temper_Tag
{
public function parse_buffer()
{
$this->require_attributes('id');
$title = $this->require_one('title', 'alt');
return '
'.$title.'
';
}
}
And use it in your template:
test A
test B
# Examples
{{echo({%foo})}} =>
{{Example::getArguments({%var1})}} =>
{{Example::getArguments({%var1}, {%var2})}} =>
{=obj.a_field} => getAField() ?>
{=obj.b_field} => getBField() ?>
=>
=> $v):?>
- {=k} -> {=v}
=> - ->
=>
=>
=>
=> $v):?>
- {=k} -> {=v}
=> - ->
=>
=>
=>
=> $v):?>
- {=k} -> {=v}
=> - ->
=>
=>
=>
=>
$var1 is set. => $var1 is set.
=>
$var2 is set. => $var2 is set.
=>
no var set. => no var set.
=>
=>
=>
=>
=>
=>
$var1 is set to 1 => $var1 is set to 1
=>
$var1 is different than 1 => $var1 is different than 1
=>
=>
=> 1):?>
$var2 is greater than 1 => $var2 is greater than 1
=>
$var2 is less or equal than 1 => $var2 is less or equal than 1
=>
=>
no var set. => no var set.
=>
=>
=>
POSTS BLOCK
=> POSTS BLOCK
=>
=> => case "orange": ?>
ORANGE
=> ORANGE
=> => case "red": ?>
RED
=> RED
=> => case "blue": ?>
BLUE
=> BLUE
=> => case "green": ?>
GREEN
=> GREEN
=> => } ?>
=> Parses and includes prueba.inc.tpl as php code.
# Usage
You must enable sfTemperPlugin module at ``enabled_modules: [default, sfTemperPlugin]`` in your ``settings.yml`` and enable the plugin as you usually do with other plugins.
Lets see a few examples of parsing and reading inside a module called ``temper``.
## Symfony 1.0
public function executeParse()
{
if ($file = $this->getRequestParameter('file'))
{
$to = Temper::getTemplateDirPath()."/$file.php";
Temper::factory(Temper::getTemplateDirPath()."/$file.tpl")->parse($to);
$this->parsed = $to;
}
else
{
$this->parsed = "(NOT PARSED)";
}
}
public function executeRead()
{
if ($file = $this->getRequestParameter('file'))
{
$this->setFlash('color', 'red');
$this->setFlash('posts', 1);
$this->setFlash('array', array('a', 'e', 'i', 'o', 'u'));
$this->setFlash('invoice', '1234567890');
$this->setFlash('partial', Temper::getTemplateDirPath()."/$file.php"); // required 'partial' variable
return $this->renderText($this->getPresentationFor('sfTemperPlugin', 'load'));
}
return sfView::NONE;
}
## Symfony 1.1 or 1.2
public function executeParse(sfWebRequest $request)
{
if ($file = $request->getParameter('file'))
{
$to = Temper::getTemplateDirPath()."/$file.php";
Temper::factory(Temper::getTemplateDirPath()."/$file.tpl")->parse($to);
$this->parsed = $to; // To show path in parseSuccess.php
}
else
{
$this->parsed = "(NOT PARSED)";
}
}
public function executeRead(sfWebRequest $request)
{
if ($file = $this->getRequestParameter('file'))
{
$variables = array(
'partial' => Temper::getTemplateDirPath()."/$file.php",
'color' => 'red',
'posts' => 1,
'array' => array('a', 'e', 'i', 'o', 'u'),
'invoice' => '1234567890',
'var2' => 2,
'var1' => 1,
'foo' => 'foo = var',
'var' => 'var = something',
'obj' => new Example()
);
// We pass variables as usually. 'partial' is required
return $this->renderPartial('sfTemperPlugin/load', $variables);
}
return sfView::NONE;
}
# Notes
* See LICENSE to see mine and the original one.
* This plugin may be incomplete at this moment.
* Feel free of e-mail me (carlos at markhaus.com) with patches and suggestions.