简体   繁体   中英

How to compare variable from js and twig

I need to check statement of two variables one comes from my server and I access this from TWIG, second, comes from javascript (data table column value). I need to check like this

{% if key.orderid == 19 %}

Where 19 should be replaced with js variable.

Code for research:

// format function for row details
var fnFormatDetails = function( datatable, tr ) {
var data = datatable.fnGetData( tr );
            return [
                    '<table class="table mb-none">',
                    '<tr class="b-top-none">',
                    '<td><label class="mb-none">Produkt:</label></td>',
                    '<td>Ilość:</td>',
                    '</tr>',
                    '{% for index, key in orderProducts %}{% if key.orderid == 19 %}<tr class="b-top-none">',
                    '<td><label class="mb-none">{{ index }}</label></td>',
                    '<td>{{ key.productid }}   {{ key.amount }}</td>',
                    '</tr>{% endif %}{% endfor %}',
                    '</table>'
                ].join('');
};

Write your conditional statement in JS instead of twig.

var tmp = ['<table class="table mb-none">'];
{% for index, key in orderProducts %}
  if (19 == {{ key.orderid }}) {
    tmp.push('<tr class="b-top-none">','<td></td>','</tr>');
  }
{% endfor %}
tmp.push('</table>');
return tmp.join('');

你可以做到

<script>var test = {{ valueToGet }};<script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM