1 | 1 | var handlebars = require('handlebars'); |
|
2 | /** |
||
3 | * Aufruf in templates per |
||
4 | * |
||
5 | * {{#ifCond value="1" comp="2" mode='lt'}} |
||
6 | * "1" ist kleiner "2" |
||
7 | * {{else}} |
||
8 | * "1" ist nicht kleiner "2" |
||
9 | * {{/ifCond}} |
||
10 | * |
||
11 | * mögliche "mode"s: |
||
12 | * - eq == |
||
13 | * - lt < |
||
14 | * - gt > |
||
15 | * - lte <= |
||
16 | * - gte >= |
||
17 | * - neq != |
||
18 | * |
||
19 | */ |
||
20 | 1 | handlebars.registerHelper('ifCond', function(options) { |
|
21 | var |
||
22 | 36 | modes = ['eq', 'lt', 'gt', 'lte', 'gte', 'neq'], |
|
23 | 36 | mode = ( (typeof options.hash.mode != 'undefined') && (modes.indexOf(options.hash.mode) != -1) ) ? options.hash.mode : 'eq' |
|
24 | ; |
||
25 | |||
26 | 36 | switch (mode) { |
|
27 | case 'lt' : |
||
28 | 6 | if (options.hash.value < options.hash.comp) return options.fn(this); |
|
0 ignored issues
–
show
|
|||
29 | 4 | break; |
|
30 | case 'gt' : |
||
31 | 6 | if (options.hash.value > options.hash.comp) return options.fn(this); |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
32 | 4 | break; |
|
33 | case 'lte' : |
||
34 | 6 | if (options.hash.value <= options.hash.comp) return options.fn(this); |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
35 | 2 | break; |
|
36 | case 'gte' : |
||
37 | 6 | if (options.hash.value >= options.hash.comp) return options.fn(this); |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
38 | 2 | break; |
|
39 | case 'neq' : |
||
40 | 6 | if (options.hash.value != options.hash.comp) return options.fn(this); |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
41 | 2 | break; |
|
42 | case 'eq' : |
||
43 | default : |
||
44 | 6 | if (options.hash.value == options.hash.comp) return options.fn(this); |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
45 | 4 | break; |
|
46 | } |
||
47 | 18 | return options.inverse(this); |
|
48 | }); |
||
49 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.