S
S
Sergey2017-02-15 15:06:01
JavaScript
Sergey, 2017-02-15 15:06:01

How to output an array of data via ajax to the Smarty template engine from php?

It is not possible to output an array of data to the foreach construct in the Smarty template engine.
Found out that it is necessary to use {literal}{/literal}. but when wrapping js in these tags, js doesn't work at all.
The site is made in mvc, that is, everything listed below is in different files.

In firebug I get the array

{"success":1,"message":"\u041a\u043e\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u0435 \u043d","$resultComment":[{"kurs_id":"7","comment":"dcfvg","user_id":"777"}]}Debug:
[success] => 1
[message] => Comment added
[$resultComment] => Array
(
[0] => Array
(
[kurs_id] => 7
[comment] => dcfvg
[user_id] => 777
)

)

)


You need to pass it to the .tpl file in the foreach


{foreach $resultComment as $item name=comment}

{$item["user_id"]}

{$item["comment"]}



{if $smarty.foreach.comment. iteration mod 1 == 0}

{/if}
{/foreach}


Using js, ajsx (file - main.js).

function addTOComment(itemId){
var comment = $('#comment').val();
var postData = {comment: comment};

$.ajax({
type: 'GET',
async: true,
url: "/comment/addtocomment/" + itemId + '/',
data: postData,
dataType: 'json',
success: function(data){
if(data['success']){

$('#addcom').prepend(data);

alert(data['message']);

} else {
alert(data['message']);

}
}
} );
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Skripchenko, 2017-06-22
@dskripchenko

Do you understand what you are asking?
If you need to dynamically render a template, then do it on the server, and return already rendered html in response to ajax, all that remains is to paste it in the right place.
And before you ask a question, try to at least a little delve into the topic.

S
Sergey, 2018-07-07
@sergey444

I figured it out myself, I sent json from the server and I need html, I mean dataType in ajax. Well, I created a tpl on the server, which I then sent to #addcom.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question