Skip to content

Commit

Permalink
v.1.1.5
Browse files Browse the repository at this point in the history
* new functions json_encode, json_decode, in_array
  • Loading branch information
Nikos M committed Apr 25, 2016
1 parent 3ffe083 commit 2ed2f95
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ __Light-weight, fast and flexible "object-oriented" template engine for PHP, Pyt
* [Dialect](https://github.com/foo123/Dialect) a simple cross-platform SQL construction for PHP, Python, Node/XPCOM/JS, ActionScript
* [Xpresion](https://github.com/foo123/Xpresion) a simple and flexible eXpression parser engine (with custom functions and variables support) for PHP, Python, Node/XPCOM/JS, ActionScript
* [GrammarTemplate](https://github.com/foo123/GrammarTemplate) versatile and intuitive grammar-based templating for PHP, Python, Node/XPCOM/JS, ActionScript
* [GrammarPattern](https://github.com/foo123/GrammarPattern) versatile grammar-based pattern-matching for Node/XPCOM/JS (IN PROGRESS)
* [Regex Analyzer/Composer](https://github.com/foo123/RegexAnalyzer) Regular Expression Analyzer and Composer for Node/XPCOM/JS, PHP, Python, ActionScript
* [RegExpX](https://github.com/foo123/RegExpX) powerful and versatile pattern matching for Node/JS (in progress)
* [DateX](https://github.com/foo123/DateX) eXtended & localised Date parsing, diffing, formatting and validation for Node/XPCOM/JS, Python, PHP, ActionScript
* [Abacus](https://github.com/foo123/Abacus) a fast combinatorics and computation library for Node/JS, PHP, Python
* [Asynchronous](https://github.com/foo123/asynchronous.js) a simple manager for async, linearised, parallelised, interleaved and sequential tasks for JavaScript
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
###ChangeLog

__1.1.5__
* new function `in_array` to test if array contains a value (similar to php)
* new functions `json_encode`, `json_decode` (similar to php)


__1.1.4__
* lazier init for faster performance
* new function `is_array` to test both if is `array` (`strict`) or is `object`
Expand Down
7 changes: 6 additions & 1 deletion manual.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
###Contemplate Manual

**version 1.1.4; platforms: PHP, Python, Node/XPCOM/JS**
**version 1.1.5; platforms: PHP, Python, Node/XPCOM/JS**


###Contents
Expand Down Expand Up @@ -47,6 +47,8 @@ $obj["key"].key2

$obj.key["key2"] // this will also work

$obj.key.key2 // this will also work

// etc..

```
Expand Down Expand Up @@ -161,6 +163,9 @@ TEMPLATE operations take place in the `current` context (which defaults to `glob
* `q( val )` wrap `val` in single-quotes
* `qq( val )` / `dq( val )` wrap `val` in double-quotes
* `is_array( val [, strict=false] )` (`php`-like) function test whether `val` is `array` (`strict`) or `object`
* `in_array( val, array )` (`php`-like) function test whether `val` is contained in `array`
* `json_encode( val )` (`php`-like) function to json-encode `val`
* `json_decode( val )` (`php`-like) function to json-decode `val`
* `addslashes( str )` addslashes (`php`-like) function
* `stripslashes( str )` stripslashes (`php`-like) function
* `sprintf( format, val1, val2, .. )` return a formatted string using `val1`, `val2`, etc..
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/contemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var contemplate_grammar = {
"concat","cc","ltrim","rtrim","trim","lowercase","uppercase",
"lcfirst","ucfirst","camelcase","snakecase","count","haskey",
"uuid","time","now","date","ldate","locale","l","nlocale","nl","xlocale","xl","nxlocale","nxl",
"inline","tpl","e","url","is_array"
"inline","tpl","e","url","is_array","in_array","json_encode","json_decode"
]}
,"plugin" : "RE::/[a-zA-Z_][a-zA-Z0-9_]*/"

Expand All @@ -97,7 +97,7 @@ var contemplate_grammar_compat = {
"%concat","%cc","%ltrim","%rtrim","%trim","%lowercase","%uppercase",
"%lcfirst","%ucfirst","%camelcase","%snakecase","%count","%haskey",
"%uuid","%time","%now","%date","%ldate","%locale","%l","%nlocale","%nl","%xlocale","%xl","%nxlocale","%nxl",
"%inline","%tpl","%e","%url","%is_array"
"%inline","%tpl","%e","%url","%is_array","%in_array","%json_encode","%json_decode"
]}
,"plugin" : "RE::/%[a-zA-Z_][a-zA-Z0-9_]*/"

Expand Down
25 changes: 22 additions & 3 deletions src/js/Contemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Contemplate
* Light-weight Template Engine for PHP, Python, Node, client-side and XPCOM/SDK JavaScript
*
* @version: 1.1.4
* @version: 1.1.5
* https://github.com/foo123/Contemplate
*
* @inspired by : Simple JavaScript Templating, John Resig - http://ejohn.org/ - MIT Licensed
Expand Down Expand Up @@ -31,7 +31,7 @@ else if ( !(name in root) ) /* Browser/WebWorker/.. */
//////////////////////////////////////////////////////////////////////////////////////

// private vars
var __version__ = "1.1.4", Contemplate,
var __version__ = "1.1.5", Contemplate,

PROTO = 'prototype', HAS = 'hasOwnProperty',
Obj = Object, Arr = Array, toString = Obj[PROTO].toString,
Expand Down Expand Up @@ -97,7 +97,8 @@ var __version__ = "1.1.4", Contemplate,
'lowercase', 'uppercase', 'ucfirst', 'lcfirst', 'sprintf',
'date', 'ldate', 'locale', 'xlocale',
'inline', 'tpl', 'uuid', 'haskey',
'concat', 'ltrim', 'rtrim', 'trim', 'addslashes', 'stripslashes', 'is_array',
'concat', 'ltrim', 'rtrim', 'trim', 'addslashes', 'stripslashes',
'is_array', 'in_array', 'json_encode', 'json_decode',
'camelcase', 'snakecase', 'e', 'url', 'nlocale', 'nxlocale'
],
$__aliases = {
Expand Down Expand Up @@ -656,6 +657,12 @@ function parse_constructs( match0, match1, match2, match3, match4, match5, match
else
out = "('[object Array]'===Object.prototype.toString.call("+args[0]+")||'[object Object]'===Object.prototype.toString.call("+args[0]+"))";
break;
case 28:
args = split_arguments(args,',');
out = '(-1<('+args[1]+').indexOf('+args[0]+'))';
break;
case 29: out = 'JSON.stringify('+args+')'; break;
case 30: out = 'JSON.parse('+args+')'; break;
default: out = 'Contemplate.' + ctrl + '(' + args + ')';
}
return prefix + out + rest.replace( re_controls, parse_constructs );
Expand Down Expand Up @@ -2166,6 +2173,18 @@ Contemplate = {
return strict ? '[object Array]' === to_string : ('[object Array]' === to_string) || ('[object Object]' === to_string);
}

,in_array: function( v, a ) {
return -1 < a.indexOf( v );
}

,json_encode: function( v ) {
return JSON.stringify( v );
}

,json_decode: function( v ) {
return JSON.parse( v );
}

,haskey: function( v/*, key1, key2, etc.. */ ) {
var to_string = toString.call( v ), args, i, tmp;
if (!v || "[object Array]" !== to_string && "[object Object]" !== to_string) return false;
Expand Down
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.

28 changes: 25 additions & 3 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.4
* @version: 1.1.5
* 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.4";
const VERSION = "1.1.5";

const CACHE_TO_DISK_NONE = 0;
const CACHE_TO_DISK_AUTOUPDATE = 2;
Expand Down Expand Up @@ -408,7 +408,8 @@ class Contemplate
'lowercase', 'uppercase', 'ucfirst', 'lcfirst', 'sprintf',
'date', 'ldate', 'locale', 'xlocale',
'inline', 'tpl', 'uuid', 'haskey',
'concat', 'ltrim', 'rtrim', 'trim', 'addslashes', 'stripslashes', 'is_array',
'concat', 'ltrim', 'rtrim', 'trim', 'addslashes', 'stripslashes',
'is_array', 'in_array', 'json_encode', 'json_decode',
'camelcase', 'snakecase', 'e', 'url', 'nlocale', 'nxlocale'
);
private static $__aliases = array(
Expand Down Expand Up @@ -860,6 +861,21 @@ public static function is_array( $v, $strict=false )
return $strict ? is_array($v) && $v === array_values($v) : is_array($v);
}

public static function in_array( $v, $a )
{
return in_array( $v, $a );
}

public static function json_encode( $v )
{
return json_encode( $v );
}

public static function json_decode( $v )
{
return json_decode( $v, true );
}

public static function haskey( $v/*, key1, key2, etc.. */ )
{
if (!$v || !is_array($v)) return false;
Expand Down Expand Up @@ -1478,6 +1494,12 @@ private static function parse_constructs( $match )
else
$out = "is_array({$args[0]})";
break;
case 28:
$args = self::split_arguments($args,',');
$out = "in_array(({$args[0]}),({$args[1]}))";
break;
case 29: $out = 'json_encode('.$args.')'; break;
case 30: $out = 'json_decode('.$args.',true)'; break;
default: $out = 'Contemplate::' . $ctrl . '(' . $args . ')';
}
return $prefix . $out . preg_replace_callback( $re_controls, $parse_constructs, $rest );
Expand Down
23 changes: 18 additions & 5 deletions src/python/Contemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Contemplate
# Light-weight Templating Engine for PHP, Python, Node and client-side JavaScript
#
# @version 1.1.4
# @version 1.1.5
# https://github.com/foo123/Contemplate
#
# @inspired by : Simple JavaScript Templating, John Resig - http://ejohn.org/ - MIT Licensed
Expand All @@ -12,7 +12,7 @@
##

# needed imports
import os, sys, re, time, datetime, calendar, math, codecs
import os, sys, re, time, datetime, calendar, math, codecs, json

# http://docs.python.org/2/library/collections.html#collections.OrderedDict
# http://code.activestate.com/recipes/576693/
Expand Down Expand Up @@ -148,7 +148,8 @@ class _G:
'lowercase', 'uppercase', 'ucfirst', 'lcfirst', 'sprintf',
'date', 'ldate', 'locale', 'xlocale',
'inline', 'tpl', 'uuid', 'haskey',
'concat', 'ltrim', 'rtrim', 'trim', 'addslashes', 'stripslashes', 'is_array',
'concat', 'ltrim', 'rtrim', 'trim', 'addslashes', 'stripslashes',
'is_array', 'in_array', 'json_encode', 'json_decode',
'camelcase', 'snakecase', 'e', 'url', 'nlocale', 'nxlocale'
]
aliases = {
Expand Down Expand Up @@ -908,6 +909,9 @@ def parse_constructs( match ):
out = "(isinstance("+args[0]+",list) if ("+args[1]+") else isinstance("+args[0]+",(list,dict)))"
else:
out = "isinstance("+args[0]+",(list,dict))"
elif 28==m:
args = split_arguments(args,',')
out = "(("+args[0]+") in ("+args[1]+"))"
else:
out = 'Contemplate.' + ctrl + '(' + args + ')'
return prefix + out + re.sub(re_controls, parse_constructs, rest)
Expand Down Expand Up @@ -1757,7 +1761,7 @@ class Contemplate:
"""

# constants (not real constants in Python)
VERSION = "1.1.4"
VERSION = "1.1.5"

CACHE_TO_DISK_NONE = 0
CACHE_TO_DISK_AUTOUPDATE = 2
Expand Down Expand Up @@ -2143,9 +2147,18 @@ def inline( tpl, reps=None, compiled=False ):
if isinstance(tpl, Contemplate.InlineTemplate): return str(tpl.render( reps ))
return Contemplate.InlineTemplate( tpl, reps, compiled )

def is_array( v, strict=False):
def is_array( v, strict=False ):
return isinstance(v,list) if strict else isinstance(v,(list,dict))

def in_array( v, a ):
return (v in a)

def json_encode( v ):
return json.dumps( v )

def json_decode( v ):
return json.loads( v )

def haskey( v, *args ):
if not v or not (isinstance(v, list) or isinstance(v, dict)): return False
argslen = len(args)
Expand Down
Loading

0 comments on commit 2ed2f95

Please sign in to comment.