Skip to content

Commit cdcecec

Browse files
committed
added: inline helpers and nested conditions
1 parent 201f5b0 commit cdcecec

8 files changed

Lines changed: 421 additions & 321 deletions

File tree

changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ IN DEVELOPMENT ======= 1.5.0
1212
- added: .webm into the config['static-accepts']
1313
- added: in views - into the repository, model, user, session, get, post, global, config can assign some value: @{repository.name = 'total.js'}
1414
- added: in views - supports @{foreach [property] in [array]} ... @{end}
15+
- added: in views - supports nested conditions
16+
- added: in views - supports inline helpers
1517
- added: controller.throw400([problem])
1618
- added: controller.throw401([problem])
1719
- added: controller.throw403([problem])

internal.js

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,32 +1712,25 @@ function view_parse(content, minify) {
17121712
content = removeComments(compressCSS(compressJS(content, 0, framework), 0, framework));
17131713

17141714
var DELIMITER = '\'';
1715+
var SPACE = ' ';
17151716
var builder = 'var $EMPTY=\'\';var $length=0;var $source=null;var $tmp=index;var $output=$EMPTY';
17161717
var command = view_find_command(content, 0);
17171718

17181719
if (command === null)
17191720
builder += '+' + DELIMITER + compressHTML(content, minify).replace(/\\\'/g, '\\\\\'').replace(/\'/g, '\\\'').replace(/\n/g, '\\n') + DELIMITER;
17201721

17211722
var old = null;
1722-
var condition = 0;
1723-
var foreach = 0;
1724-
var is = false;
17251723
var newCommand = '';
17261724
var tmp = '';
17271725
var index = 0;
1726+
var counter = 0;
1727+
var functions = [];
1728+
var functionsName = [];
1729+
var isFN = false;
1730+
var builderTMP = '';
17281731

17291732
while (command !== null) {
17301733

1731-
1732-
/**
1733-
* @todo Whats is this?
1734-
*/
1735-
1736-
/*
1737-
if (condition === 0 && builder !== '')
1738-
builder += '+';
1739-
*/
1740-
17411734
if (old !== null) {
17421735
var text = content.substring(old.end + 1, command.beg);
17431736
if (text !== '') {
@@ -1756,49 +1749,56 @@ function view_parse(content, minify) {
17561749

17571750
var cmd = content.substring(command.beg + 2, command.end);
17581751

1759-
if (cmd.substring(0, 8) === 'foreach ') {
1760-
foreach = 1;
1752+
if (cmd.substring(0, 7) === 'helper ') {
1753+
1754+
builderTMP = builder;
1755+
builder = 'function ' + cmd.substring(7).trim() + '{var $output=$EMPTY';
1756+
isFN = true;
1757+
functionsName.push(cmd.substring(7, cmd.indexOf('(', 7)).trim());
1758+
1759+
} else if (cmd.substring(0, 8) === 'foreach ') {
1760+
1761+
counter++;
17611762

17621763
if (cmd.indexOf('foreach var ') !== -1)
1763-
cmd = cmd.replace(' var ', ' ');
1764+
cmd = cmd.replace(' var ', SPACE);
17641765

1765-
newCommand = (cmd.substring(8, cmd.indexOf(' ', 8)) || '').trim();
1766+
newCommand = (cmd.substring(8, cmd.indexOf(SPACE, 8)) || '').trim();
17661767
index = cmd.indexOf('[');
1768+
17671769
if (index === -1)
1768-
index = cmd.lastIndexOf(' ');
1769-
var source = cmd.substring(index).trim();
1770-
builder += ';$source=' + source + ';if ($source && $source.length > 0){$length=$source.length;';
1771-
builder += 'for(var i=0;i<$length;i++){'
1772-
builder += 'index = i;var ' + newCommand + '=$source[i];$output+=$EMPTY';
1770+
index = cmd.lastIndexOf(SPACE);
1771+
1772+
builder += '+(function(){var $source=' + cmd.substring(index).trim() + ';if (!($source instanceof Array) || source.length === 0)return $EMPTY;var $length=$source.length;var $output=$EMPTY;var index=0;for(var i=0;i<$length;i++){index = i;var ' + newCommand + '=$source[i];$output+=$EMPTY';
1773+
17731774
} else if (cmd === 'end') {
1774-
builder += ';}}index=$tmp;$output+=$EMPTY';
1775-
newCommand = '';
1775+
1776+
if (isFN && counter <= 0) {
1777+
counter = 0;
1778+
builder += ';return $output;}';
1779+
functions.push(builder);
1780+
builder = builderTMP;
1781+
builderTMP = '';
1782+
} else {
1783+
counter--;
1784+
builder += '}return $output;})()';
1785+
newCommand = '';
1786+
}
1787+
17761788
} else if (cmd.substring(0, 3) === 'if ') {
1777-
if (view_parse_plus(builder))
1778-
builder += '+';
1779-
condition = 1;
1780-
builder += '(' + cmd.substring(3) + '?';
1781-
is = true;
1789+
builder += ';if (' + cmd.substring(3) + '){$output+=$EMPTY';
17821790
} else if (cmd === 'else') {
1783-
condition = 2;
1784-
builder += ':';
1785-
is = true;
1791+
builder += '} else {$output+=$EMPTY';
17861792
} else if (cmd === 'endif') {
1787-
if (condition === 1)
1788-
builder += ':$EMPTY';
1789-
condition = 0;
1790-
builder += ')';
1791-
is = true;
1793+
builder += '}$output+=$EMPTY'
17921794
} else {
1793-
1794-
tmp = view_prepare(command.command, newCommand);
1795+
tmp = view_prepare(command.command, newCommand, functionsName);
17951796

17961797
if (tmp.length > 0) {
17971798
if (view_parse_plus(builder))
17981799
builder += '+';
17991800
builder += tmp;
18001801
}
1801-
18021802
}
18031803

18041804
old = command;
@@ -1811,18 +1811,19 @@ function view_parse(content, minify) {
18111811
builder += '+' + DELIMITER + compressHTML(text, minify).replace(/\\\'/g, '\\\\\'').replace(/\'/g, '\\\'').replace(/\n/g, '\\n') + DELIMITER;
18121812
}
18131813

1814-
var fn = '(function(self,repository,model,session,get,post,url,global,helpers,user,config,functions,index,sitemap,output){var controller=self;' + builder + ';return $output;})';
1814+
var fn = '(function(self,repository,model,session,get,post,url,global,helpers,user,config,functions,index,sitemap,output){' + (functions.length > 0 ? functions.join('') : '') + ';var controller=self;' + builder + ';return $output;})';
18151815
return eval(fn);
18161816
}
18171817

1818+
18181819
function view_parse_plus(builder) {
18191820
var c = builder[builder.length - 1];
18201821
if (c !== '!' && c !== '?' && c !== '+' && c !== '.' && c !== ':')
18211822
return true;
18221823
return false;
18231824
}
18241825

1825-
function view_prepare(command, dynamicCommand) {
1826+
function view_prepare(command, dynamicCommand, functions) {
18261827

18271828
var a = command.indexOf('.');
18281829
var b = command.indexOf('(');
@@ -2011,7 +2012,7 @@ function view_prepare(command, dynamicCommand) {
20112012
case 'password':
20122013
return 'self.$' + exports.appendModel(command);
20132014
default:
2014-
return 'helpers.' + view_insert_call(command);
2015+
return functions.indexOf(name) === -1 ? command[0] === '!' ? command.substring(1) + '.toString()' : command + '.toString().encode()' : command + '.toString()';
20152016
}
20162017

20172018
return command;
@@ -2540,9 +2541,21 @@ Template.prototype.parse = function(html) {
25402541
var minify = self.controller.config['allow-compress-html'];
25412542

25422543
if (indexBeg === -1 && html.indexOf('@{foreach') !== -1)
2543-
return { is: true, template: view_parse(compressHTML(html.trim()), minify) };
2544+
return {
2545+
is: true,
2546+
template: view_parse(compressHTML(html.trim()), minify)
2547+
};
25442548

25452549
var searchEnd = '@{end}';
2550+
2551+
if (indexBeg === -1) {
2552+
searchBeg = '<!--';
2553+
searchEnd = '-->';
2554+
indexBeg = html.indexOf(searchBeg);
2555+
if (indexBeg !== -1)
2556+
console.log('OBSOLETE: <!-- @{model} --> (replace to @{foreach} @{model} @{end}) in template:', self.name);
2557+
}
2558+
25462559
var indexEnd = indexBeg !== -1 ? html.lastIndexOf(searchEnd) : -1;
25472560
var beg = '';
25482561
var end = '';

minify/total.js/internal.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/app.framework.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ var framework = require('../index');
22
var http = require('http');
33
framework.run(http, false, 8001);
44

5-
/*
65
setTimeout(function() {
76
utils.request('http://127.0.0.1:8001/views/', ['get'], function() {
87
framework.stop();
98
});
10-
}, 500);
11-
*/
9+
}, 500);

0 commit comments

Comments
 (0)