Adding a conditional to mustache/php
Consider:
<?php
/**
* Single variation display
*
* This is a javascript-based template for single variations (see https://codex.wordpress.org/Javascript_Reference/wp.template).
* The values will be dynamically replaced after selecting attributes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
<div class="woocommerce-variation-custom-text-field">
GTIN: <span class="sku">{{{ data.variation.wccaf_gtin }}}</span>
</div>
<div class="woocommerce-variation-custom-text-field">
MPN: <span class="sku">{{{ data.variation.wccaf_mpn }}}</span>
</div>
</script>
<script type="text/template" id="tmpl-unavailable-variation-template">
<p><?php _e( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ); ?></p>
</script>
다음과 같은 조건을 구현하려면 어떻게 해야 합니까?{{{ data.variation.wccaf_gtin }}}
공백/공백 값을 반환한 다음 "GTIN 사용 불가" 에코를 선택합니다.
What I have tried:
I have read this on wiki:
Template with section tag:
{{#x}} Some text {{/x}} Here, when x is a Boolean value then the section tag acts like an if conditional
So I tried
<div class="woocommerce-variation-custom-text-field">
GTIN: <span class="sku">{{#repo}}{{ data.variation.wccaf_gtin }}{{/repo}}{{^repo}}N/A{{/repo}}</span>
</div>
which doesn't work.
But I'm completely new to mustache and I need some guidance.
이 템플릿은 콧수염 템플릿이 아닙니다. 설명에 따르면 WordPress 사용자 정의입니다.wp.template
해를 보다
논리 태그는 없지만 평가 태그는 있습니다.<# #>
.
From quick google here is an example how would you write a check using them:
<# if ( data.trueValue ) { #>
<p> I am only output if <code>data.trueValue</code> is true.
<# } #>
Via https://lkwdwrd.com/wp-template-js-templates-wp
According to Mustache Documentation
// if exist
{{#repo}}
<b>{{name}}</b>
{{/repo}}
// if not exist
{{^repo}}
No repos :(
{{/repo}}
You may try this
<div class="woocommerce-variation-custom-text-field">
GTIN: <span class="sku">{{#data.variation.wccaf_gtin}}{{ data.variation.wccaf_gtin }}{{/data.variation.wccaf_gtin}}{{^data.variation.wccaf_gtin}}N/A{{/data.variation.wccaf_gtin}}</span>
</div>
Try the same using ints-jst
Code:
<div id="try-jst">
<div class="woocommerce-variation-description">
<js-t> print(data.variation.variation_description);</js-t>
</div>
<div class="woocommerce-variation-price">
<js-t> print(data.variation.price_html);</js-t>
</div>
<div class="woocommerce-variation-availability">
<js-t> print(data.variation.availability_html);</js-t>
</div>
<div class="woocommerce-variation-custom-text-field">
GTIN:
<span class="sku">
<js-t>
if(!!data.variation.wccaf_gtin) {
print(data.variation.wccaf_gtin);
} else {
print("unavailable");
}
</js-t>
</span>
</div>
<div class="woocommerce-variation-custom-text-field">
MPN:
<span class="sku">
<js-t>
if(!!data.variation.wccaf_mpn) {
print(data.variation.wccaf_mpn);
} else {
print("unavailable");
}
</js-t>
</span>
</div>
</div>
Script:
<script>
/* var data = {
variation: {
variation_description: 'variation_description',
price_html: 'price_html',
availability_html: 'availability_html',
wccaf_gtin: '',
wccaf_mpn: '123',
}
}; */
// jQuery version
compile($('#try-jst')[0]);
run();
// Plain Javascript
/**
* compile(document.getElementById('try-jst'));
* run();
*/
</script>
Output:
<div id="try-jst">
<div class="woocommerce-variation-description">variation_description</div>
<div class="woocommerce-variation-price">price_html</div>
<div class="woocommerce-variation-availability">availability_html</div>
<div class="woocommerce-variation-custom-text-field"> GTIN: <span class="sku">unavailable</span></div>
<div class="woocommerce-variation-custom-text-field"> MPN: <span class="sku">123</span></div>
</div>
So you can give it a try.
ReferenceURL : https://stackoverflow.com/questions/54964240/adding-a-conditional-to-mustache-php
'programing' 카테고리의 다른 글
문자열이 아닌 숫자로 CSS 최상위 값을 얻습니까? (0) | 2023.10.31 |
---|---|
CSV로 내보낼 때 열에 선행 0을 유지하려면 어떻게 해야 합니까? (0) | 2023.10.31 |
TensorFlow에서 tf.app.flags의 목적은 무엇입니까? (0) | 2023.10.31 |
Android ndk/jni와 함께 C++ 사용 (0) | 2023.10.31 |
코디네이터 레이아웃의 도구 모음 아래에 보기 추가 (0) | 2023.10.31 |