Reputation: 423
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>
How do I implement a conditional, such that IF {{{ data.variation.wccaf_gtin }}}
returns blank/empty value, THEN echo "GTIN unavailable"?
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.
Upvotes: 6
Views: 1663
Reputation: 531
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.
Upvotes: 1
Reputation: 3158
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>
Upvotes: 2
Reputation: 2395
That is not a Mustache template, as comment notes it's WordPress custom wp.template
solution.
It doesn't have any logic tags, but has evaluate tags <# #>
.
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
Upvotes: 3