[Symfony3, twgig - template]
return x; - constantly returns a string like "<span>710</span>₽<div><span>319.5000</span>₽</div>" , and I need html, tags for generating content, how can I render them?

  <script type="text/javascript"> function searchCertificateAction() { var template = "<div class='pr-one'>" + "<div class='img'>" + "<img title='${name}' src='${image}'>" + "</div>" + "<div class='pr-name'>" + "${name}" + "</div>" + "<div class='price sale-price'>"+ "${price_result}"+ "</div>" + "<div class='pay'><span>В корзину</span></div>" + "</div>"; var empty = "<p>No response...</p>"; $("#certificate_search_certificate input[type = 'submit']").on('click', function (e) { e.preventDefault(); $.ajax({ type: "POST", dataType: 'json', data: $('#certificate_search_certificate').serialize(), url: '{{ path("app.client.catalog", {"category_parent": 'all', "category_children": 'all'}) }}', success: function (data) { var dataParse = JSON.parse(data.certificate_response); var resultHtml = "#products"; $(resultHtml).empty(); if (dataParse.length !== 0) { for (var i = 0; i < dataParse.length; i++) { $.tmpl(template, { "id": dataParse[i].id, "name": dataParse[i].name, "price": dataParse[i].price, "image": dataParse[i].image, "total_price": dataParse[i].total_price, "price_result": function (total_price = dataParse[i].total_price, price = dataParse[i].price) { if (total_price === undefined) { var x = "<div><span>" + price + "</span>₽</div>"; return x; } else { var x = "<span>" + price + "</span>₽<div><span>" + total_price + "</span>₽</div>"; return x; } } }).appendTo(resultHtml); } } else { $.tmpl(empty, {}).appendTo(resultHtml); } } }); }); } searchCertificateAction(); </script> 
  • and a line like "<span>710</span>₽<div><span>319.5000</span>₽</div>" is it not html or what? - teran
  • it is also displayed in html, only instead of <> - krakozyabry. - Maxim Strutinskiy
  • krakozyabry it &gt; and &lt; ? :) what is the $.tmpl() function you have? - teran
  • if you use ancient jquery templates then something like in the template you can write not ${price_result} but {{html price_result}} , check if you can write a response. - teran
  • yes, ancient jQuery =), I can't use "{{}}" because I use twig template engine. So I address my action on the backend {{ path("app.client.catalog", {"category_parent": 'all', "category_children": 'all'}) }} - Maxim Strutinskiy

1 answer 1

Found a solution, maybe not the best but I do not see the others (at the moment). Added a check before rendering a template in which the desired template is selected.

 <script type="text/javascript"> function searchCertificateAction() { var template_true = "<p>true</p>" + "<div class='pr-one'>" + "<div class='img'>" + "<img title='${name}' src='${image}'>" + "</div>" + "<div class='pr-name'>" + "${name}" + "</div>" + "<div class='price sale-price'>" + "<div><span>" + " ${price} " + "</span>₽</div>" + "</div>" + "<div class='pay'><span>В корзину</span></div>" + "</div>"; var template_false = "<p>false</p>" + "<div class='pr-one'>" + "<div class='img'>" + "<img title='${name}' src='${image}'>" + "</div>" + "<div class='pr-name'>" + "${name}" + "</div>" + "<div class='price sale-price'>" + "<span>" + "${price}" + "</span>₽<div><span>" + "${total_price}" + "</span>₽</div>" + "</div>" + "<div class='pay'><span>В корзину</span></div>" + "</div>"; var empty = "<p>No response...</p>"; $("#certificate_search_certificate input[type = 'submit']").on('click', function (e) { e.preventDefault(); $.ajax({ type: "POST", dataType: 'json', data: $('#certificate_search_certificate').serialize(), url: '{{ path("app.client.catalog", {"category_parent": 'all', "category_children": 'all'}) }}', success: function (data) { var dataParse = JSON.parse(data.certificate_response); var resultHtml = "#products"; $(resultHtml).empty(); if (dataParse.length !== 0) { for (var i = 0; i < dataParse.length; i++) { var template; if (dataParse[i].total_price === undefined) { template = template_true; } else { template = template_false; } $.tmpl(template, { "id": dataParse[i].id, "name": dataParse[i].name, "price": dataParse[i].price, "image": dataParse[i].image, "total_price": dataParse[i].total_price, }).appendTo(resultHtml); } } else { $.tmpl(empty, {}).appendTo(resultHtml); } } }); }); } searchCertificateAction(); </script>