[英]handlebars.js - declare if statement
I am a beginner and I have created a basic test page where I am using handlebars.js to display different country address formats based on the country value style number. 我是一个初学者,我创建了一个基本的测试页,在其中我使用handlebars.js显示基于国家/地区值样式号的不同国家/地区地址格式。
Here is a snippet of the code I currently have: 这是我当前拥有的代码片段:
{{# if address_country_style_type_value_is_4 }}
{{! Country Name: Algeria }}
{{! Index Value: 4 }}
{{! Address Style: 2 }}
{{# if address_line_1 }}
{{{ address_line_1 }}}<br />
{{/if}}
{{# if address_street_details }}
{{ address_street_details }}<br />
{{/if}}
{{# if address_postal_code }}
{{ address_postal_code }}
{{/if}}
{{# if address_locality }}
{{ address_locality }}
{{/if}}
{{# if address_country_name }}
<br />{{ address_country_name }}
{{/if}}
{{/if}}
{{# if address_country_style_type_value_is_5 }}
{{! Country Name: American Samoa }}
{{! Index Value: 5 }}
{{! Address Style: 7 }}
{{# if address_line_1 }}
{{{ address_line_1 }}}<br />
{{/if}}
{{# if address_street_details }}
{{ address_street_details }}<br />
{{/if}}
{{# if address_locality }}
{{ address_locality }}
{{/if}}
{{# if address_region }}
{{ address_region }}
{{/if}}
{{# if address_postal_code }}
{{ address_postal_code }}
{{/if}}
{{# if address_country_name }}
<br />{{ address_country_name }}
{{/if}}
{{/if}}
When the user selects the address_country_style_type_value_is_4, the address style #2 is displayed. 当用户选择address_country_style_type_value_is_4时,将显示地址样式#2。 The code for the display of address style #2 is repeated many times throughout my code - which I know is bad to repeat the same code over and over. 在我的代码中,用于显示地址样式2的代码重复了很多次-我知道很难一遍又一遍地重复相同的代码。
My question is how do I write the handlebars code so that I have the address style #2 written once and then called many times where needed? 我的问题是如何编写车把代码,以便将地址样式2写一次,然后在需要的地方多次调用?
That is the declare and then call the contents inside the {{# if address_country_style_type_value_is_4 }} statement. 这是声明,然后调用{{#if address_country_style_type_value_is_4}}语句中的内容。
Instead of changing the format around in the template, you could change it in Javascript: 您可以在Javascript中更改其格式,而不必在模板中更改格式:
switch(address_country_style_type) {
case 4:
address = [
address_line_1,
address_street_details,
address_postal_code + ' ' + address_locality,
address_country_name
];
break;
case 5:
address = [
address_line_1,
address_street_details,
address_locality + ' ' + address_region + ' ' + address_postal_code,
address_country_name
];
break;
}
And then just render whatever is in the address: 然后只需渲染地址中的任何内容:
{{#each address}} {{this}}<br> {{/each}}
Of course, at that point you could just combine everything ahead of time with address.join('<br>')
and then render {{{address}}}
. 当然,到那时,您可以将所有内容提前与address.join('<br>')
合并,然后呈现{{{address}}}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.