đưa cái này lên một bậc, cho những người sống ở rìa.
ý chính : https://gist.github.com/akhoury/9118682
Demo : Đoạn mã dưới đây
Tay lái trợ giúp: {{#xif EXPRESSION}} {{else}} {{/xif}}
một người trợ giúp để thực hiện một câu lệnh IF với bất kỳ biểu thức nào
- EXPRESSION là một chuỗi thoát đúng
- Có, bạn CẦN thoát khỏi chuỗi ký tự hoặc chỉ thay thế dấu ngoặc đơn và dấu ngoặc kép
- bạn có thể truy cập bất kỳ chức năng hoặc tài sản toàn cầu tức là
encodeURIComponent(property)
- ví dụ này giả sử bạn đã chuyển ngữ cảnh này tới tay lái của bạn
template( {name: 'Sam', age: '20' } )
, thông báo age
là string
, chỉ để tôi có thể demo parseInt()
sau trong bài đăng này
Sử dụng:
<p>
{{#xif " name == 'Sam' && age === '12' " }}
BOOM
{{else}}
BAMM
{{/xif}}
</p>
Đầu ra
<p>
BOOM
</p>
JavaScript: (nó phụ thuộc vào người trợ giúp khác - tiếp tục đọc)
Handlebars.registerHelper("xif", function (expression, options) {
return Handlebars.helpers["x"].apply(this, [expression, options]) ? options.fn(this) : options.inverse(this);
});
Tay lái trợ giúp: {{x EXPRESSION}}
Một người trợ giúp để thực hiện các biểu thức javascript
- EXPRESSION là một chuỗi thoát đúng
- Có, bạn CẦN thoát khỏi chuỗi ký tự hoặc chỉ thay thế dấu ngoặc đơn và dấu ngoặc kép
- bạn có thể truy cập bất kỳ chức năng hoặc tài sản toàn cầu tức là
parseInt(property)
- ví dụ này giả sử bạn đã chuyển ngữ cảnh này tới tay lái của bạn
template( {name: 'Sam', age: '20' } )
, age
là string
cho mục đích demo, nó có thể là bất cứ điều gì ..
Sử dụng:
<p>Url: {{x "'hi' + name + ', ' + window.location.href + ' <---- this is your href,' + ' your Age is:' + parseInt(this.age, 10)"}}</p>
Đầu ra:
<p>Url: hi Sam, http://example.com <---- this is your href, your Age is: 20</p>
JavaScript:
Điều này có vẻ hơi lớn vì tôi đã mở rộng cú pháp và nhận xét trên hầu hết các dòng cho mục đích rõ ràng
Handlebars.registerHelper("x", function(expression, options) {
var result;
// you can change the context, or merge it with options.data, options.hash
var context = this;
// yup, i use 'with' here to expose the context's properties as block variables
// you don't need to do {{x 'this.age + 2'}}
// but you can also do {{x 'age + 2'}}
// HOWEVER including an UNINITIALIZED var in a expression will return undefined as the result.
with(context) {
result = (function() {
try {
return eval(expression);
} catch (e) {
console.warn('•Expression: {{x \'' + expression + '\'}}\n•JS-Error: ', e, '\n•Context: ', context);
}
}).call(context); // to make eval's lexical this=context
}
return result;
});
Handlebars.registerHelper("xif", function(expression, options) {
return Handlebars.helpers["x"].apply(this, [expression, options]) ? options.fn(this) : options.inverse(this);
});
var data = [{
firstName: 'Joan',
age: '21',
email: 'joan@aaa.bbb'
}, {
firstName: 'Sam',
age: '18',
email: 'sam@aaa.bbb'
}, {
firstName: 'Perter',
lastName: 'Smith',
age: '25',
email: 'joseph@aaa.bbb'
}];
var source = $("#template").html();
var template = Handlebars.compile(source);
$("#main").html(template(data));
h1 {
font-size: large;
}
.content {
padding: 10px;
}
.person {
padding: 5px;
margin: 5px;
border: 1px solid grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.min.js"></script>
<script id="template" type="text/x-handlebars-template">
<div class="content">
{{#each this}}
<div class="person">
<h1>{{x "'Hi ' + firstName"}}, {{x 'lastName'}}</h1>
<div>{{x '"you were born in " + ((new Date()).getFullYear() - parseInt(this.age, 10)) '}}</div>
{{#xif 'parseInt(age) >= 21'}} login here:
<a href="http://foo.bar?email={{x 'encodeURIComponent(email)'}}">
http://foo.bar?email={{x 'encodeURIComponent(email)'}}
</a>
{{else}} Please go back when you grow up. {{/xif}}
</div>
{{/each}}
</div>
</script>
<div id="main"></div>
Moar
Nếu bạn muốn truy cập phạm vi cấp trên, thì phạm vi này hơi khác một chút, biểu thức là THAM GIA của tất cả các đối số, cách sử dụng: giả sử dữ liệu ngữ cảnh trông như thế này:
// data
{name: 'Sam', age: '20', address: { city: 'yomomaz' } }
// in template
// notice how the expression wrap all the string with quotes, and even the variables
// as they will become strings by the time they hit the helper
// play with it, you will immediately see the errored expressions and figure it out
{{#with address}}
{{z '"hi " + "' ../this.name '" + " you live with " + "' city '"' }}
{{/with}}
Javascript:
Handlebars.registerHelper("z", function () {
var options = arguments[arguments.length - 1]
delete arguments[arguments.length - 1];
return Handlebars.helpers["x"].apply(this, [Array.prototype.slice.call(arguments, 0).join(''), options]);
});
Handlebars.registerHelper("zif", function () {
var options = arguments[arguments.length - 1]
delete arguments[arguments.length - 1];
return Handlebars.helpers["x"].apply(this, [Array.prototype.slice.call(arguments, 0).join(''), options]) ? options.fn(this) : options.inverse(this);
});