Source for file SmartyConvertor.php
Documentation is available at SmartyConvertor.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Alan Knowles <alan@akbkhome.com> |
// +----------------------------------------------------------------------+
// $Id: SmartyConvertor.php,v 1.2 2004/04/03 12:09:00 alan_k Exp $
// Smarty Conversion compiler
// takes a smarty template, and converts it to a flexy one.
// then does a standard flexy compile.
// anything that is not supported gets changed to HTML comments
a simple script: 'convertsmarty.php'
$file = $_SERVER['argv'][1];
$x = new HTML_Template_Flexy(array(
'compileDir' => dirname(__FILE__) , // where do you want to write to..
'templateDir' => $dir , // where are your templates
'locale' => 'en', // works with gettext
'forceCompile' => true, // only suggested for debugging
'debug' => false, // prints a few errors
'nonHTML' => false, // dont parse HTML tags (eg. email templates)
'allowPHP' => false, // allow PHP in template
'compiler' => 'SmartyConvertor', // which compiler to use.
'compileToString' => true, // returns the converted template (rather than actually
'filters' => array(), // used by regex compiler..
'numberFormat' => ",2,'.',','", // default number format = eg. 1,200.00 ( {xxx:n} )
'flexyIgnore' => 0 // turn on/off the tag to element code
echo $x->compile(basename($file));
then run it at the command line:
php convertsmarty.php /path/to/a/smarty/template.tpl > /path/to/the/flexy/templates.html
require_once 'HTML/Template/Flexy/Compiler.php';
* The Smarty Converter implementation.
* designed primarily to be used as above, to convert from one to another.
* however it could be used inline to convert simple smarty templates into
* flexy ones - then compile them on the fly.
* @version $Id: SmartyConvertor.php,v 1.2 2004/04/03 12:09:00 alan_k Exp $
class HTML_Template_Flexy_Compiler_SmartyConvertor extends HTML_Template_Flexy_Compiler {
* see HTML_Template_Flexy_Compiler::compile()
* @param object The core flexy object.
* @param string optionally a string to compile.
* @return true | string string when compiling to String.
function compile (&$flexy,$string=false )
$data = $this->convertToFlexy ($data);
if ($flexy->options ['compileToString']) {
require_once 'HTML/Template/Flexy/Compiler/Standard.php';
$flexyCompiler = HTML_Template_Flexy_Compiler_Standard;
$flexyCompiler->compile ($flexy,$data);
* The core work of parsing a smarty template and converting it into flexy.
* @param string the contents of the smarty template
* @return string the flexy version of the template.
* @see see also methods.....
function convertToFlexy ($data)
preg_match_all("!" . $leftq . "\s*(.*?)\s*" . $rightq . "!s", $data, $matches);
// find all the tags/text...
$text = preg_split("!" . $leftq . ".*?" . $rightq . "!s", $data);
$max_text = count($text);
$max_tags = count($tags);
for ($i = 0 ; $i < $max_tags ; $i++ ) {
$compiled_tags[] = $this->_compileTag ($tags[$i]);
// error handling for closing tags.
for ($i = 0; $i < $max_tags; $i++ ) {
$data .= $text[$i]. $compiled_tags[$i];
* stack for conditional and closers.
* compile a smarty { tag } into a flexy one.
* @return string the converted version
function _compileTag ($str)
if (($str{0 } == '*') && (substr($str,-1 ,1 ) == '*')) {
return $this->_convertVar ($str);
return $this->_convertConfigVar ($str);
return " <!-- what is this? $str -->";
// this is where it gets messy
// this is very slow - but what the hell
// - its alot more readable than a long regext.
// - it doesnt infringe on copyright...
// convert to $t->TemplateConfigLoad()
$args = $this->convertAttributesToKeyVal (substr($str,strpos( $str,' ')));
return '{templateConfigLoad(#'. $args['file']. '#,#'. $args['section']. ')}';
case (preg_match('/^if \$(\S+)$/', $str,$matches)):
case (preg_match('/^if \$(\S+)\seq\s""$/', $str,$matches)):
// convert to : {if:sssssss}
$var = $this->_convertVar ('$'. $matches[1 ]);
case (preg_match('/^if #(\S+)#$/', $str,$matches)):
case (preg_match('/^if #(\S+)#\sne\s""$/', $str,$matches)):
// convert to : {if:sssssss}
$var = $this->_convertConfigVar ('#'. $matches[1 ]. '#');
case (preg_match('/^if\s!\s\$(\S+)$/', $str,$matches)):
case (preg_match('/^if \$(\S+)\seq\s""$/', $str,$matches)):
// convert to : {if:sssssss}
$var = $this->_convertVar ('$'. $matches[1 ]);
return '{if:!'. substr($var,1 );
if (!$this->stack['if']) {
if (!$this->stack['if']) {
return " <!-- UNSUPPORTED TAG: $str FOUND -->";
* convert a smarty var into a flexy one.
* @param string the inside of the smart tag
* @return string a flexy version of it.
function _convertVar ($str)
// look for modfiers first.
$var = substr($var,1 ); // strip $
// aaaa.bbbb.cccc => aaaa[bbbb][cccc]
// aaaa[bbbb] => aaa[bbbb]
// aaaa->bbbb => aaaa.bbbb
$mods = " <!-- UNSUPPORTED MODIFIERS: $mods -->";
return '{'. $var . '}' . $mods;
* convert a smarty key="value" string into a key value array
* cheap and cheerfull - doesnt handle spaces inside the strings...
* @param string the key value part of the tag..
* @return array key value array
function convertAttributesToKeyVal ($str)
$ret[$bits[0 ]] = ($bits[1 ]{0 } == '"') ? substr($bits[1 ],1 ,-1 ) : $bits[1 ];
* convert a smarty config var into a flexy one.
* @param string the inside of the smart tag
* @return string a flexy version of it.
function _convertConfigVar ($str)
$var = substr($var,1 ,-1 ); // strip #'s
$mods = " <!-- UNSUPPORTED MODIFIERS: $mods -->";
return '{configVars.'. $var . '}' . $mods;
Documentation generated on Mon, 11 Mar 2019 10:15:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|