μ΄κ²μ λ§€μ° μ μ©νλ©° λΌμ΄λΈλ¬λ¦¬μ κΈ°λ³Έμ μΌλ‘ μ 곡λλ κ²μ΄ κ°μ₯ μ’μ κ²μ΄λΌκ³ μκ°ν©λλ€.
{{#switch state}}
{{#case "page1" "page2"}}toolbar{{/case}}
{{#case "page1" break=true}}page1{{/case}}
{{#case "page2" break=true}}page2{{/case}}
{{#case "page3" break=true}}page3{{/case}}
{{#default}}page0{{/default}}
{{/switch}}
μ΄κ²μ νμ€ λμ°λ―Έλ₯Ό μ¬μ©νμ¬ (ν΄ν€ λ°©μμΌλ‘) λ¬μ±ν μ μλ κ²μ λλ€. μ°λ¦¬λ μ 3μκ° λμ°λ―Έ APIλ₯Ό μ¬μ©νμ¬ κ°μ₯ μ ν©ν λ°©μμΌλ‘ νμν λμ°λ―Έλ₯Ό ꡬνν μ μλλ‘ νλ λμ λ΄μ₯ λμ°λ―Έλ₯Ό μλμ μΌλ‘ κ°λ³κ² μ μ§νκ³ μ ν©λλ€.
μ΄ νμ΄μ§λ νμ¬ "νΈλ€λ° μ€μμΉ λμ°λ―Έ"μ λν 3λ²μ§Έ Google κ²μ κ²°κ³Όμ
λλ€. κ²μ μμ§μμ μ¬κΈ°λ‘ μ€λ μ¬λλ€μ μν΄ {{#case "page1"}}
μ κ°μ λ¨μΌ κ° case
μ μ μ§μ νλ Chris Montroisμ ꡬνμ΄ μμ΅λλ€ .
Handlebars.registerHelper("switch", function(value, options) {
this._switch_value_ = value;
var html = options.fn(this); // Process the body of the switch block
delete this._switch_value_;
return html;
});
Handlebars.registerHelper("case", function(value, options) {
if (value == this._switch_value_) {
return options.fn(this);
}
});
λ€μμ {{#case "page1" "page2"}}
μ κ°μ΄ λ€μν κ°μ μ μ μ§μνλ κ°μ λ case
λμ°λ―Έμ
λλ€.
Handlebars.registerHelper("case", function() {
// Convert "arguments" to a real array - stackoverflow.com/a/4775938
var args = Array.prototype.slice.call(arguments);
var options = args.pop();
var caseValues = args;
if (caseValues.indexOf(this._switch_value_) === -1) {
return '';
} else {
return options.fn(this);
}
});
μμ£Ό μ’κ³ μ§§κ³ λ¬μ½€ν©λλ€. λλ κ³Όκ±°μ ν¨μ¬ λ κΈ΄ κ²μ λ§λ¬μ κ²μ λλ€.
μΆκ°ν λͺ κ°μ§ νλͺ©μ {{#default}} {{/default}}
λ° {{#case "value" break=true}}
μμ΅λλ€.
@stevenvachon μμ²λλ‘ ;)
module.exports = {
switch: function(value, options) {
this._switch_value_ = value;
this._switch_break_ = false;
var html = options.fn(this);
delete this._switch_break_;
delete this._switch_value_;
return html;
},
case: function(value, options) {
var args = Array.prototype.slice.call(arguments);
var options = args.pop();
var caseValues = args;
if (this._switch_break_ || caseValues.indexOf(this._switch_value_) === -1) {
return '';
} else {
if (options.hash.break === true) {
this._switch_break_ = true;
}
return options.fn(this);
}
},
default: function(options) {
if (!this._switch_break_) {
return options.fn(this);
}
}
};
@Billy- μ΄κ²μ μ΄λμ ν¬ν¨ν΄μΌ ν©λκΉ?
@jimkoul μ΄κ²μ λμ°λ―Έ κΈ°λ₯μ΄λ―λ‘ μ€μ μ λ°λΌ λ€λ¦ λλ€.
λ΄ μ€μ (Gulp + gulp-hb μ¬μ©)μ μμ κ°μ΄ λμ°λ―Έ κΈ°λ₯μ λ΄λ³΄λ΄λ js νμΌμ λν glob ν¨ν΄ μ§μ μ μ§μνλ―λ‘ λ€μκ³Ό κ°μ΅λλ€.
// ...
.pipe(handlebars({
helpers: 'handlebars/helpers/*.js'
})
// ...
μ¬μ ν νμ€νμ§ μμ κ²½μ° λμ°λ―Έ κΈ°λ₯μ΄ νΈλ€λ°μμ μλνλ λ°©μκ³Ό μ¬μ© μ€μΈ νΈλ€λ° ꡬνκ³Ό ν¨κ» μ΄λ₯Ό ꡬννλ λ°©λ²μ λν΄ μ½κ°μ μ°κ΅¬λ₯Ό μννμμμ€.
μ€μμΉ μ΄λ¦μ ν΅ν΄ λ³μλ₯Ό μ λ¬ν μ μκ³ κ°κ°μ ν΅ν΄ μμ ν {{assign}}
λ₯Ό μμ±ν νμκ° μλ€λ©΄ μ’μ κ²μ
λλ€. κ·Έλ κ² νλ©΄ μ΅μ
μ΄ λ³μ κ°μ΄ λ©λλ€.
λν λ/μλ¬Έμκ° μΆ©μ‘±λ λ λ³μκ° μ¬λ°λ₯΄κ² λ³κ²½λλλΌλ μ μλμ§ μμ λ©μμ§κ° νμλ©λλ€.
μ΄κ²μ λ΄κ° λμ°λ―Έλ₯Ό μν΄ κ°μ§κ³ μλ κ²μ λλ€.
"use strict";
Handlebars.registerHelper("switch", function(value, options) {
this._switch_value_ = value;
this._switch_break_ = false;
var html = options.fn(this);
delete this._switch_break_;
delete this._switch_value_;
return html;
});
Handlebars.registerHelper("case", function(value, options) {
var args = Array.prototype.slice.call(arguments);
var options = args.pop();
var caseValues = args;
if (this._switch_break_ || caseValues.indexOf(this._switch_value_) === -1) {
return '';
} else {
if (options.hash.break === true) {
this._switch_break_ = true;
}
return options.fn(this);
}
});
Handlebars.registerHelper("default", function(options) {
if (!this._switch_break_) {
return options.fn(this);
}
});
μ΄κ²μ λ΄ hbs νμΌμμλ κ²μ λλ€.
{{#assign "testParam"}}foo{{/assign}}
{{#switch testParam}}
{{#case "boo"}}{{#assign "testParam"}}case1 has been met{{/assign}}{{/case}}
{{#case "foo" break=true}}{{#assign "testParam"}}case2 has been met{{/assign}}{{/case}}
{{#case "tried" break=true}}{{#assign "testParam"}}case3 has been met{{/assign}}{{/case}}
{{#case "bwahahaha" break=true}}{{#assign "testParam"}}case4 has been met{{/assign}}{{/case}}
{{#default break=true}}{{#assign "testParam"}}nothing matched{{/assign}}{{/default}}
{{/switch}}
{{#ttpartial "testSwitch.content"}}
{{testParam}}
{{/ttpartial}}
λλ μμ κ²½μ° μ€ νλκ° μΆ©μ‘±λλ κ²½μ°μλ μμ μ΄λ€ μ΄μ λ‘ μΈν΄ νμ κΈ°λ³Έκ°μ΄ μ°μ μ μ©λ©λλ€.
λ΄κ° μ¬μ©νλ κ²μ λ€μκ³Ό κ°μ΅λλ€.
Handlebars.registerHelper('switch', function(name, value, options) {
this['_switch_value_' + name] = value;
this['_switch_break_' + name] = false;
var html = options.fn(this);
delete this['_switch_break_' + name];
delete this['_switch_value_' + name];
return html;
});
Handlebars.registerHelper('case', function(name, value, options) {
var args = Array.prototype.slice.call(arguments);
var options = args.pop();
var caseValues = args;
if ( this['_switch_break_' + name] || caseValues.indexOf(this['_switch_value_' + name]) === -1) {
return '';
} else {
this['_switch_break_' + name] = true;
return options.fn(this);
}
});
Handlebars.registerHelper('default', function(name, options) {
if ( !this['_switch_break_' + name] ) {
return options.fn(this);
}
});
μ€μμΉμ μ΄λ¦μ΄ μ§μ λμ΄ μμΌλ―λ‘ λΆκΈ°ν μ μμ΅λλ€(imbrication? λ¨μ΄κ° ββνμ€νμ§ μμ...)
λν μ‘°κ±΄μ΄ μΆ©μ‘±λλ©΄ νμ breakκ° μνλ©λλ€.
μ¦:
{{#switch "1" "aaa"}}
{{#case "1" "aaa"}}
{{#switch "2" "bbb"}}
{{#case "2" "bbb"}}ok{{/case}}
{{/switch}}
{{/case}}
{{#default "1"}}nok{{/default}}
{{/switch}}
λλ μ΄κ²μ΄ μ΄λ»κ² μλνλμ§ μ΄ν΄νμ§ λͺ»νκ³ μλ€κ³ νμ ν©λλ€. κ·Έλμ νΌλμ λλ € μ£μ‘ν©λλ€. νμ§λ§... each
λ΄μμ μ€μμΉκ° μλνλ κ²μ΄ κ°λ₯ν©λκΉ? ν΄λ΄€λλ° κ³μ μ€λ₯κ° λλ€μ.
λ΄κ° κ°μ§ κ²μ λ€μκ³Ό κ°μ΅λλ€.
{{#each columns}}
{{#switch this}}
{{#case 'foo'}}
{{/case}}
{{#case 'bar'}}
{{/case}}
{{/switch}}
{{/each}}
νμ§λ§ λλ μ΄κ²μ μ»κ³ μλ€ :
Uncaught TypeError: Cannot create property '_switch_value_' on string 'name'
this
μ(λ) μμν κ²κ³Όλ λ€λ₯Έ κ²μΈ κ² κ°μ§λ§ μ λͺ¨λ₯΄κ² μ΅λλ€.
switch
λ₯Ό λ€μκ³Ό κ°μ΄ λ³κ²½νμ¬ μλνκ² ν κ² κ°μ΅λλ€.
Handlebars.registerHelper({
switch: function(value, options) {
return options.fn({
_switch_value_: value,
_switch_break_: false
});
},
//...
});
κ·Έλ¬λ λλ λ€λ₯Έ κ²½μ°μ λν΄ λκ°λ₯Ό μλ κ² κ°μμ. μ¦, μ΄ κ°μ νμ options
μ°κ²°ν μ μμΌλ©° μλν κ²μ΄λΌκ³ μκ°ν©λλ€.
@Billy- @a-le?
@infinityplusone λΆννλ λλ μλ λμ νΈλ€ λ°λ₯Ό μ λλ‘ μ¬μ©νμ§ μμμΌλ―λ‘ λ΄κ° μμ±ν μ΄νλ‘ λμ°λ―Έ ꡬνμ΄ ν¬κ² λ³κ²½λμμ μ μλ€κ³ μκ°ν©λλ€. μλ§λ μμλλ‘ μλνμ§ μμκΈ° λλ¬ΈμΌ κ²μ λλ€. μ λͺ¨λ₯΄κ² μ§λ§ μ€λ§μ λλ € μ£μ‘ν©λλ€.
@infinityplusone λ΄ λ²μ μ μ¬μ©νλ κ²½μ° μ€μμΉμ "μ΄λ¦"μ μ§μ ν΄μΌ ν©λλ€.
λ°λΌμ κ·νμ μλ₯Ό λ€λ©΄ λ€μκ³Ό κ°μ΅λλ€.
{{#each columns}}
{{#switch "myswitch" this}}
{{#case "myswitch" 'foo'}}
{{/case}}
{{#case "myswitch" 'bar'}}
{{/case}}
{{/switch}}
{{/each}}
μ€μ²©λ μ€μμΉ λΈλ‘μ©
Handlebars.__switch_stack__ = [];
Handlebars.registerHelper( "switch", function( value, options ) {
Handlebars.__switch_stack__.push({
switch_match : false,
switch_value : value
});
var html = options.fn( this );
Handlebars.__switch_stack__.pop();
return html;
} );
Handlebars.registerHelper( "case", function( value, options ) {
var args = Array.from( arguments );
var options = args.pop();
var caseValues = args;
var stack = Handlebars.__switch_stack__[Handlebars.__switch_stack__.length - 1];
if ( stack.switch_match || caseValues.indexOf( stack.switch_value ) === -1 ) {
return '';
} else {
stack.switch_match = true;
return options.fn( this );
}
} );
Handlebars.registerHelper( "default", function( options ) {
var stack = Handlebars.__switch_stack__[Handlebars.__switch_stack__.length - 1];
if ( !stack.switch_match ) {
return options.fn( this );
}
} );
{{#switch state}}
{{#case "page1" "page2"}}page 1 or 2{{/case}}
{{#case "page3"}}page3{{/case}}
{{#case "page4"}}page4{{/case}}
{{#case "page5"}}
{{#switch s}}
{{#case "3"}}s = 3{{/case}}
{{#case "2"}}s = 2{{/case}}
{{#case "1"}}s = 1{{/case}}
{{#default}}unknown{{/default}}
{{/switch}}
{{/case}}
{{#default}}page0{{/default}}
{{/switch}}
var data = {
state : 'page5',
s : '1'
};
var html = template( data );
μ΄κ²μ μ΄λ€κ°μ?
ν€λ₯Ό κ°μΌλ‘ λ³ννλ €λ κ²½μ°μλ§.
````μλ°μ€ν¬λ¦½νΈ
module.exports = function(μ
λ ₯, μΌμ΄μ€, κ°) {
const caseArray = case.split(',')
const valueArray = values.split(',')
μμ μΈλ±μ€ = caseArray.indexOf(μ
λ ₯)
λ°ν κ° λ°°μ΄[μΈλ±μ€]
};
νΈλ€λ°
{{μ€μμΉ "Y" "Y,N,D", "μ, μλμ€, μμ λ¨"}}
````
κ°μ₯ μ μ©ν λκΈ
μ΄ νμ΄μ§λ νμ¬ "νΈλ€λ° μ€μμΉ λμ°λ―Έ"μ λν 3λ²μ§Έ Google κ²μ κ²°κ³Όμ λλ€. κ²μ μμ§μμ μ¬κΈ°λ‘ μ€λ μ¬λλ€μ μν΄
{{#case "page1"}}
μ κ°μ λ¨μΌ κ°case
μ μ μ§μ νλ Chris Montroisμ ꡬνμ΄ μμ΅λλ€ .λ€μμ
{{#case "page1" "page2"}}
μ κ°μ΄ λ€μν κ°μ μ μ μ§μνλ κ°μ λcase
λμ°λ―Έμ λλ€.