I can't set data's in twig from Ajax response. ${id}
I render content on front-end with jQuery template (use Ajax response), and I can't set data's in this template, twig give error:
Unexpected character "$".
Hear my JavaScript:
function ajaxSearchForm() {
var template =
"<tr>" +
"<td>${id}</td>" +
"<td>${dpId}</td>" +
"<td>${name}</td>" +
"<td>${price}</td>" +
"<td>${discount}</td>" +
"<td>${description}</td>" +
"<td>${image}</td>" +
"<td>${companyName}</td>" +
"<td>${categoryName}</td>" +
"<td><a class='btn btn-sm btn-default edit_link'></a><i class='fa fa-cart-plus' aria-hidden='true'></i>" +
"{{ render(controller('MainBundle:Certificate:addCertificate', {'certificate': " + ${id} ", 'cart': 1215'} )) }} " +
"</td>" +
"</tr>";
var empty =
"<tr>" +
"<th colspan='10'>No result...</th>" +
"</tr>";
$('#form_search_certificate button').on('click', function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "{{ path('app.admin.search.certificate')}}",
dataType: 'json',
data: $('#form_search_certificate').serialize(),
success: function (data) {
$("#search_result").empty();
var dataParse = JSON.parse(data.result_search);
if (dataParse.length !== 0) {
for (var i = 0; i < dataParse.length; i++) {
$.tmpl(template, {
"id": dataParse[i].id,
"dpId": dataParse[i].dp_id,
"name": dataParse[i].name,
"price": dataParse[i].price,
"discount": dataParse[i].discount.value,
"description": dataParse[i].description,
"image": dataParse[i].image,
"companyName": dataParse[i].company.name,
"categoryName": dataParse[i].category.name,
"cart_id": data.cart_id
}).appendTo("#search_result");
}
} else {
$.tmpl(empty, {}).appendTo("#search_result");
}
}
});
});
}
ajaxSearchForm();
I found the whey How set data in my ajax url:
In Action, wich rendering this template must have field cat_id
(i get hem from return render()
).
Then in ajax template i get this cat_id
and set in url:
url: '{{ path("app.admin.search.certificate", {"cartId": cart_id}) }}',
And all is work !
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments