Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

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.

like image 318
ptrcao Avatar asked Mar 02 '19 23:03

ptrcao


1 Answers

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

like image 152
Rarst Avatar answered Sep 27 '22 01:09

Rarst