0

I'm using Phalcon and Volt template engine .

This index.volt and I'm going to pass an array to JavaScript

<script type="text/javascript" >

 var usersList= {{ array_from_volt }} ;

</javascript>

But it doesn't work .

in PHP we used to do like this But it doesn't work for volt

<script type="text/javascript" >

 var usersList=  <?php echo json_encode(array_from_php); ?>;

</javascript>  

So, How can I Pass an Array to javascript from volt ?

1 Answer 1

1

In your PHP code, use setvar to set a variable that Volt can reference.

$this->view->setVar('array_name', $your_array);

If you need the array json encoded, you can:

$this->view->setVar('array_name', json_encode($your_array));

Then in your Volt file, you can set the javascript variable as follows:

<script>
    var usersList = [{{ array_name }}];
</script>

(note syntax not verified... YMMV)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.