Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If/else blocks: `{{value=='x'}} <<markup>> {{:value=='x'}} <<alterna… #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Scrubbed interpolation: `{{%unsafe_value}}`
* Name-spaced variables: `{{=User.address.city}}`
* If/else blocks: `{{value}} <<markup>> {{:value}} <<alternate markup>> {{/value}}`
* If/else blocks: `{{value=='x'}} <<markup>> {{:value=='x'}} <<alternate markup>> {{/value=='x'}}` ( === == > < >= <= != )
* If not blocks: `{{!value}} <<markup>> {{/!value}}`
* Object/Array iteration: `{{@object_value}} {{=_key}}:{{=_val}} {{/@object_value}}`
* Multi-line templates (no removal of newlines required to render)
Expand Down
54 changes: 53 additions & 1 deletion t.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
(function() {

var blockregex = /\{\{(([@!]?)(.+?))\}\}(([\s\S]+?)(\{\{:\1\}\}([\s\S]+?))?)\{\{\/\1\}\}/g,
valregex = /\{\{([=%])(.+?)\}\}/g;
valregex = /\{\{([=%])(.+?)\}\}/g,
conregex = /([^=><]+?)([!=>< ]+)(.+)$/;

function t(template) {
this.t = template;
Expand All @@ -39,6 +40,21 @@
}
return vars;
}

function get_value_con(vars, key){
var v = null;
if(key.charAt(0) == "'" || key.charAt(0) == '"'){
v = key.substr(1);
if(v.charAt(v.length - 1) == "'" || v.charAt(v.length - 1) == '"') {
v = v.substr(0,v.length -1);
}
}else if(!isNaN(key.charAt(0))){
v = Number(key);
}else{
v = get_value(vars, key);
}
return v;
}

function render(fragment, vars) {
return fragment
Expand All @@ -51,6 +67,42 @@
// handle if not
if (meta == '!') {
return render(inner, vars);
}else{
var consp = conregex.exec(key);
if(consp){
var l = get_value_con(vars,consp[1]);
var r = get_value_con(vars,consp[3]);
var op = consp[2].replace(/\s/g,'');
var conV = false;
switch(op){ //do not use the eval(), it is not safe
case '==':
conV = (l == r);
break;
case '===':
conV = (l === r);
break;
case '>':
conV = (l > r);
break;
case '<':
conV = (l < r);
break;
case '>=':
conV = (l >= r);
break;
case '<=':
conV = (l <= r);
case '!=':
conV = (l != r);
break;
}

if(conV){
return render(inner, vars);
}else{
//not support
}
}
}
// check for else
if (has_else) {
Expand Down
3 changes: 1 addition & 2 deletions t.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions t_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
<h1>{{=greeting}}</h1>
<h2>{{=user.display_name}}</h2>

{{user.display_name=='Jason'}}
<p>display_name equal Jason </p>
{{/user.display_name=='Jason'}}

{{'Jason'==user.display_name}}
<p>Jason equal display_name</p>
{{/'Jason'==user.display_name}}

{{'David'!=user.display_name}}
<p>David not equal display_name</p>
{{/'David'!=user.display_name}}

{{user.address}}
<address>{{%user.address}}</address>
{{/user.address}}
Expand Down