Skip to content

Commit

Permalink
v.1.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos M committed Dec 9, 2016
1 parent 036f81f commit 79eeda9
Show file tree
Hide file tree
Showing 20 changed files with 1,278 additions and 1,248 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ syntax, yet work the same in all the engine's implementations.

* __Uniform functionality__, Engine Implementations for __PHP__ , __Python__ , __Node__ , __XPCOM__ and __client-side JavaScript__

* Simple and __light-weight__ ( only one relatively small class for each implementation, no other dependencies ) `~35kB` minified, `~12kB` zipped
* Simple and __light-weight__ ( only one relatively small class for each implementation, no other dependencies ) `~37kB` minified, `~13kB` zipped

* __Fast__ , can cache templates dynamically (filesystem caching has 3 modes, `NONE` which uses only in-memory caching, `NOUPDATE` which caches the templates only once and `AUTOUPDATE` which re-creates the cached template if original template has changed, useful for debugging)

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
###ChangeLog

__1.1.7__
* minor changes for better code alignment/indentation
* minor changes in rendering

__1.1.6__
* minor changes for faster basic templates
* new function `join / j` to (flatly) string-concatenate arbitrary array (of arrays) with given separator
Expand Down
206 changes: 111 additions & 95 deletions src/js/Contemplate.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/js/Contemplate.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/php/Contemplate.min.php

Large diffs are not rendered by default.

71 changes: 41 additions & 30 deletions src/php/Contemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Contemplate
* Light-weight Template Engine for PHP, Python, Node and client-side JavaScript
*
* @version: 1.1.6
* @version: 1.1.7
* https://github.com/foo123/Contemplate
*
* @inspired by : Simple JavaScript Templating, John Resig - http://ejohn.org/ - MIT Licensed
Expand Down Expand Up @@ -334,7 +334,7 @@ public function dispose( )

class Contemplate
{
const VERSION = "1.1.6";
const VERSION = "1.1.7";

const CACHE_TO_DISK_NONE = 0;
const CACHE_TO_DISK_AUTOUPDATE = 2;
Expand All @@ -357,7 +357,7 @@ class Contemplate
private static $__preserveLines = '';
private static $__compatibility = false;
private static $__EOL = "\n";
private static $__TEOL = PHP_EOL;
private static $__TEOL = "\n"/*PHP_EOL*/;
private static $__pad = " ";
private static $__escape = true;

Expand Down Expand Up @@ -1168,7 +1168,7 @@ private static function t_include( $id )
$id = trim( $id );
if ( self::$__strings && isset(self::$__strings[$id]) ) $id = self::$__strings[$id];
$ch = $id[0];
if ( '"' === $ch || "'" === $ch ) $id = substr($id,1,-1); // quoted id
if ( ('"' === $ch || "'" === $ch) && ($ch === $id[strlen($id)-1]) ) $id = substr($id,1,-1); // quoted id

$contx = self::$__context;
/* cache it */
Expand All @@ -1181,7 +1181,7 @@ private static function t_include( $id )
$contx->partials[$id]=" " . self::parse( $tpl, self::$__leftTplSep, self::$__rightTplSep, false ) . "';" . self::$__TEOL;
self::pop_state( $state );
}
return self::pad_lines( /*isset($contx->partials[$id]) ?*/ $contx->partials[$id] /*: self::$__global->partials[$id]*/ );
return self::align( /*isset($contx->partials[$id]) ?*/ $contx->partials[$id] /*: self::$__global->partials[$id]*/ );
}

private static function t_block( $block )
Expand All @@ -1191,7 +1191,7 @@ private static function t_block( $block )
$block = trim($block[0]);
if ( self::$__strings && isset(self::$__strings[$block]) ) $block = self::$__strings[$block];
$ch = $block[0];
if ( '"' === $ch || "'" === $ch ) $block = substr($block,1,-1); // quoted block
if ( ('"' === $ch || "'" === $ch) && ($ch === $block[strlen($block)-1]) ) $block = substr($block,1,-1); // quoted block

array_push(self::$__allblocks, array($block, -1, -1, 0, self::$__openblocks[ 0 ][ 1 ], $echoed));
self::$__allblockscnt[ $block ] = isset(self::$__allblockscnt[ $block ]) ? (self::$__allblockscnt[ $block ]+1) : 1;
Expand Down Expand Up @@ -1266,15 +1266,15 @@ private static function parse_constructs( $match )
$varname = trim(array_shift($args));
$expr = trim(implode(',', $args));
if ( 20 === $m && !self::is_local_variable($varname) ) self::local_variable( $varname ); // make it a local variable
$out = "';" . self::$__TEOL . self::pad_lines( "$varname = ($expr);" ) . self::$__TEOL;
$out = "';" . self::$__TEOL . self::align( "$varname = ($expr);" ) . self::$__TEOL;
break;
case 1 /*'unset'*/:
$args = preg_replace_callback( $re_controls, $parse_constructs, $args );
$varname = $args;
if ( $varname && strlen($varname) )
{
$varname = trim( $varname );
$out = "';" . self::$__TEOL . self::pad_lines( "if (isset($varname)) unset( $varname );" ) . self::$__TEOL;
$out = "';" . self::$__TEOL . self::align( "if (isset($varname)) unset( $varname );" ) . self::$__TEOL;
}
else
{
Expand All @@ -1288,7 +1288,7 @@ private static function parse_constructs( $match )
break;
case 3 /*'if'*/:
$args = preg_replace_callback( $re_controls, $parse_constructs, $args );
$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
,"if ({$args})"
,"{"
Expand All @@ -1300,7 +1300,7 @@ private static function parse_constructs( $match )
case 4 /*'elseif'*/:
$args = preg_replace_callback( $re_controls, $parse_constructs, $args );
self::$__level--;
$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
,"}"
,"elseif ({$args})"
Expand All @@ -1311,7 +1311,7 @@ private static function parse_constructs( $match )
break;
case 5 /*'else'*/:
self::$__level--;
$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
,"}"
,"else"
Expand All @@ -1323,7 +1323,7 @@ private static function parse_constructs( $match )
case 6 /*'endif'*/:
self::$__ifs--;
self::$__level--;
$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
,"}"
,""
Expand Down Expand Up @@ -1356,7 +1356,7 @@ private static function parse_constructs( $match )
$k = trim($kv[0]); $v = trim($kv[1]);
self::local_variable( $k ); self::local_variable( $v );

$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
,"{$_o} = {$o};"
,"if (!empty({$_o}))"
Expand All @@ -1372,7 +1372,7 @@ private static function parse_constructs( $match )
$v = trim($kv[0]);
self::local_variable( $v );

$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
,"{$_o} = {$o};"
,"if (!empty({$_o}))"
Expand All @@ -1389,7 +1389,7 @@ private static function parse_constructs( $match )
/* else attached to for loop */
self::$__loopifs--;
self::$__level+=-2;
$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
," }"
,"}"
Expand All @@ -1404,7 +1404,7 @@ private static function parse_constructs( $match )
{
self::$__loops--; self::$__loopifs--;
self::$__level+=-2;
$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
," }"
,"}"
Expand All @@ -1415,7 +1415,7 @@ private static function parse_constructs( $match )
{
self::$__loops--;
self::$__level+=-1;
$out = "';" . self::pad_lines(implode(self::$__TEOL, array(
$out = "';" . self::align(implode(self::$__TEOL, array(
""
,"}"
,""
Expand All @@ -1426,7 +1426,7 @@ private static function parse_constructs( $match )
$id = trim( $args );
if ( self::$__strings && isset(self::$__strings[$id]) ) $id = self::$__strings[$id];
$ch = $id[0];
if ( '"' === $ch || "'" === $ch ) $id = substr($id,1,-1); // quoted id
if ( ('"' === $ch || "'" === $ch) && ($ch === $id[strlen($id)-1]) ) $id = substr($id,1,-1); // quoted id
self::$__extends = $id;
$out = "';" . self::$__TEOL;
break;
Expand Down Expand Up @@ -1457,7 +1457,7 @@ private static function parse_constructs( $match )
break;
case 18 /*'continue'*/:
case 19 /*'break'*/:
$out = "';" . self::$__TEOL . self::pad_lines( 18===$m ? 'continue;' : 'break;' ) . self::$__TEOL;
$out = "';" . self::$__TEOL . self::align( 18===$m ? 'continue;' : 'break;' ) . self::$__TEOL;
break;
}
return $out . preg_replace_callback( $re_controls, $parse_constructs, $rest );
Expand Down Expand Up @@ -2088,7 +2088,7 @@ private static function parse( $tpl, $leftTplSep, $rightTplSep, $withblocks=true
// replace tpl separators
if ( "\v" === $tag[strlen($tag)-1] )
{
$tag = substr($tag,0,-1) . self::pad_lines( self::$__tplEnd );
$tag = substr($tag,0,-1) . self::align( self::$__tplEnd );
}
if ( "\t" === $tag[0] )
{
Expand Down Expand Up @@ -2207,7 +2207,7 @@ private static function create_cached_template( $id, $contx, $filename, $classna
$sblocks .= $EOL . self::$TT_BlockCode->render(array(
'BLOCKNAME' => $blocks[$b][0]
,'BLOCKMETHODNAME' => "_blockfn_" . $blocks[$b][0]
,'BLOCKMETHODCODE' => self::pad_lines($blocks[$b][1], 1)
,'BLOCKMETHODCODE' => self::align($blocks[$b][1], 1)
));

$renderCode = self::$TT_RCODE->render(array(
Expand All @@ -2221,9 +2221,9 @@ private static function create_cached_template( $id, $contx, $filename, $classna
'PREFIXCODE' => $prefixCode
,'TPLID' => $id
,'CLASSNAME' => $classname
,'EXTENDCODE' => self::pad_lines($extendCode, 1)
,'EXTENDCODE' => self::align($extendCode, 1)
,'BLOCKS' => $sblocks
,'RENDERCODE' => self::pad_lines($renderCode, 2)
,'RENDERCODE' => self::align($renderCode, 2)
));

return file_put_contents( $filename, $class );
Expand Down Expand Up @@ -2373,17 +2373,28 @@ private static function localized_date( $format, $timestamp )
return $localised_datetime;
}

private static function pad_lines( $lines, $level=null )
private static function align( $s, $level=null )
{
if ( null === $level ) $level = self::$__level;
if ( null === $level ) $level = self::$__level;

if ($level>=0)
$l = strlen($s);
if ( $l && (0 < $level) )
{
$pad = str_repeat(self::$__pad, $level);
$lines = $pad . (implode( self::$__TEOL . $pad, preg_split(self::$NEWLINE, $lines) ));
$alignment = str_repeat(self::$__pad, $level);
$aligned = $alignment;
for($i=0; $i<$l; $i++)
{
$c = $s[$i];
/*if ( "\r" === $c ) continue;
else*/if ( "\n" === $c ) $aligned .= /*self::$__TEOL*/"\n" . $alignment;
else $aligned .= $c;
}
}

return $lines;
else
{
$aligned = $s;
}
return $aligned;
}
}

Expand Down
Loading

0 comments on commit 79eeda9

Please sign in to comment.