0

Is it possible to pass variable from php script to javascript (twig for example, but it may be plain html with of course) on first page loading? I use vue cli and I can pass variable using ajax requests. At my previous application with knockout.js i just wrote something like

<script>
    window.__initialServerData = {{ data|raw }};        
    console.log(__initialServerData);        
</script>  

1 Answer 1

1

Yes, you absolutely can render a value in a PHP variable into javascript. Here is an example:

<?php
    $variable = 'value';
?>

<script>
    var my_val = '<?=$variable?>';
    console.log(my_val);
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.