M
M
magic_healthy_hair2018-11-05 20:33:47
JavaScript
magic_healthy_hair, 2018-11-05 20:33:47

How to display smarty in js?

Hello, can someone tell me how to display the numbers output from the database through smarty in js?
tried like this

{literal}
<script>
$.ajax({
url: 'useraccount.php',
dataType: 'json',
method: 'post',
success: function(data)
{
  var all_views = JSON.parse(data);

new Chart(document.getElementById("myChart"),
{"type":"doughnut",
"data":{"labels":["Red","Blue","Yellow"],
"datasets":[{"label":"My First Dataset",

//сюда {$total_views}

"data":[0,0,{$total_views}],
"backgroundColor":["rgb(255, 99, 132)",
"rgb(54, 162, 235)",
"rgb(255, 205, 86)"]}]}});
}
});
</script>
{/literal}

but inside {literal} it doesn't output smarty, and without {literal} - error 500..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
magic_healthy_hair, 2018-11-05
@magic_healthy_hair

it seems to work like this

{literal}
<script>
new Chart(document.getElementById("myChart"),
{"type":"doughnut",
"data":{"labels":["Red","Blue","Yellow"],
"datasets":[{"label":"My First Dataset",
"data":[0,0,{/literal}{$total_views}{literal}],
"backgroundColor":["rgb(255, 99, 132)",
"rgb(54, 162, 235)",
"rgb(255, 205, 86)"]}]}});
</script>
{/literal}

D
Denis, 2018-11-05
@sidni

In fact, you don’t need to use {literal} every time, just transfer the bracket to a new line like

{
"type":"doughnut",
"data":{
             "labels":["Red","Blue","Yellow"],
          }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question