sfTemperPlugin
0.1.2beta
for sf 1.2sf 1.1sf 1.0 MIT
Kohana Temper template parser into Symfony. Just a template parser.
Developers
License
sfTemperPlugin Library
based on http://alexsancho.name/archives/2008/03/temper-kohana-template-parser/
@author Carlos Escribano Rey <carlos at markhaus.com> or <carlosescri{at}gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[IF YOU THINK THIS LICENSE IS NOT COMPATIBLE WITH THE LATTER THE ORIGINAL SOFTWARE'S AUTHOR LICENSE MUST BE APPLIED.]
=====================================================================
Temper (Template Parser) Library
based on http://ioreader.com/2007/05/08/using-a-stack-to-parse-html/
@package Temper Module
@author Alex Sancho
@copyright (c) 2008 Alex Sancho
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of copyright holders nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
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 <prefix:tag></prefix:tag>.
See this page for more details.
Variables
{=variable} => <?php echo $variable ?>
{=variable|function} => <?php echo function($variable) ?>
{=foo.variable} => <?php echo $foo->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/<?php echo $variable ?> but (etc, etc)
Functions
{{echo(%foo)}} => <?php echo($foo) ?>
{{class::method({%foo}, 'arg', 'arg')}} => <?php echo call_user_function_array(array('class', 'method'), array('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' # <t:tag ...></t:tag> or <t:tag ... />
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 '<div id="'.$this['id'].'">'.$title.'</div>';
}
}
And use it in your template:
<t:invoice id="123" title="test A" />
<t:invoice id="234" alt="test B" />
<div id="123">test A</div>
<div id="234">test B</div>
Examples
{{echo({%foo})}} => <?php echo($foo);?>
{{Example::getArguments({%var1})}} => <?php echo call_user_func_array(array('Example', 'getArguments'), array($var1)); ?>
{{Example::getArguments({%var1}, {%var2})}} => <?php echo call_user_func_array(array('Example', 'getArguments'), array($var1, $var2)); ?>
{=obj.a_field} => <?php echo $obj->getAField() ?>
{=obj.b_field} => <?php echo $obj->getBField() ?>
<ul> => <ul>
<t:for var="i" start="1" end="10" increment="1"> => <?php for($i=1; $i<=10;$i+=1):?>
<li>{=i}</li> => <li><?php echo $i ?></li>
</t:for> => <?php endfor;?>
</ul> => </ul>
<ul> => <ul>
<t:foreach var="array" val="v"> => <?php foreach((array) $array as $v):?>
<li>{=v}</li> => <li><?php echo $v ?></li>
</t:foreach> => <?php endforeach;?>
</ul> => </ul>
<ul> => <ul>
<t:foreach var="array" key="k" val="v"> => <?php foreach((array) $array as $k => $v):?>
<li>{=k} -> {=v}</li> => <li><?php echo $k ?> -> <?php echo $v ?></li>
</t:foreach> => <?php endforeach;?>
</ul> => </ul>
<ul> => <ul>
<t:foreach var="Example::getArray()" key="k" val="v"> => <?php foreach((array) Example::getArray() as $k => $v):?>
<li>{=k} -> {=v}</li> => <li><?php echo $k ?> -> <?php echo $v ?></li>
</t:foreach> => <?php endforeach;?>
</ul> => </ul>
<ul> => <ul>
<t:foreach var="Example::MYCONSTANT" key="k" val="v"> => <?php foreach((array) Example::MYCONSTANT as $k => $v):?>
<li>{=k} -> {=v}</li> => <li><?php echo $k ?> -> <?php echo $v ?></li>
</t:foreach> => <?php endforeach;?>
</ul> => </ul>
<p> => <p>
<t:if isset="var1"> => <?php if(isset($var1) && $var1 != NULL):?>
$var1 is set. => $var1 is set.
<t:elseif isset="var2" /> => <?php elseif(isset($var2) && $var2 != NULL):?>
$var2 is set. => $var2 is set.
<t:else /> => <?php else: ?>
no var set. => no var set.
</t:if> => <?php endif;?>
</p> => </p>
=>
<p> => <p>
<t:if isset="var1"> => <?php if(isset($var1) && $var1 != NULL):?>
<t:if var="var1" eq="1"> => <?php if($var1 == 1):?>
$var1 is set to 1 => $var1 is set to 1
<t:else /> => <?php else: ?>
$var1 is different than 1 => $var1 is different than 1
</t:if> => <?php endif;?>
<t:elseif isset="var2" /> => <?php elseif(isset($var2) && $var2 != NULL):?>
<t:if var="var2" gt="1"> => <?php if($var2 > 1):?>
$var2 is greater than 1 => $var2 is greater than 1
<t:else /> => <?php else: ?>
$var2 is less or equal than 1 => $var2 is less or equal than 1
</t:if> => <?php endif;?>
<t:else /> => <?php else: ?>
no var set. => no var set.
</t:if> => <?php endif;?>
</p> => </p>
<t:block id="posts"> => <?php if (isset($posts)):?>
<div id="posts" style="border:1px dotted red;">POSTS BLOCK</div> => <div id="posts" style="border:1px dotted red;">POSTS BLOCK</div>
</t:block> => <?php endif; ?>
<t:switch var="color"> => <?php switch($color) {
<t:case val="orange"> => case "orange": ?>
<div style="border:1px dotted orange;">ORANGE</div> => <div style="border:1px dotted orange;">ORANGE</div>
</t:case> => <?php break;
<t:case val="red"> => case "red": ?>
<div style="border:1px dotted red;">RED</div> => <div style="border:1px dotted red;">RED</div>
</t:case> => <?php break;
<t:case val="blue"> => case "blue": ?>
<div style="border:1px dotted blue;">BLUE</div> => <div style="border:1px dotted blue;">BLUE</div>
</t:case> => <?php break;
<t:case val="green"> => case "green": ?>
<div style="border:1px dotted green;">GREEN</div> => <div style="border:1px dotted green;">GREEN</div>
</t:case> => <?php break;
</t:switch> => } ?>
<t:import file="prueba.inc.tpl" /> => 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.