0

i am trying to use the value of a php variable as javascript variable within my zend framework 3 based project. But, it is not rendering properly. Sample code is given below that i tried.

<?php
$this->inlineScript()->captureStart();
echo <<<JS

function abc(){
var a='<?php echo $name?>';
......
}

JS;
$this->inlineScript()->captureEnd();
?>

Here, it is printing as it instead of rendering the value of $name. Is there any way to do so like we can do using within plain script tag like below.

<script type='text/javascript'>
function abc(){
var a='<?php echo $name?>'; 
}
</script>
akond
  • 15,865
  • 4
  • 35
  • 55
Debashis
  • 566
  • 2
  • 14
  • 34
  • 1
    Have you tried `var a="{$name}";` (take special not of `"` double quotes, [as used here](https://stackoverflow.com/a/45428981/1155833)). Not sure (haven't tested) if this works, but give it a shot ;) As a side-note: not quite sure what you're trying to achieve, but doing this would be something to avoid in my opinion. – rkeet Oct 27 '17 at 14:38
  • Thanks that worked. But remember, php functions will not work under that tag. so, we can assign any php variable on the page if we want to use the php functions and then can use that variable within javascript. – Debashis Oct 27 '17 at 16:13
  • Rather than using any of the solutions described in the comments above (that actually work), I would consider having a better PHP/JS separation. Explanation comes in my answer. – Thomas Dutrion Oct 28 '17 at 16:29

1 Answers1

1

Try not to mix PHP and JS too much... Here your code is echoed as you are using an echo where you directed a stream (your string containing js and PHP).

Try something more readable:

<?php $this->inlineScript()->captureStart() ?>
function abc() {
    var a='<?= $name ?>';
    ......
}
<?php $this->inlineScript()->captureEnd() ?>

Here the PHP instruction will be executed as it is not in a heredoc

Also, this is not a Zend Framework related question but rather a PHP question. Please take some times to read the PHP documentation linked in this post, and not only the Heredoc section... PHP has plenty of ways of dealing with strings!

Thomas Dutrion
  • 1,844
  • 1
  • 11
  • 9
  • Did you test it? Even though i tested it while posing my question but retested after your answer and it's not working. If you see view source of page, you will see it is printing like as it is instead of rendering the value of $name. – Debashis Nov 09 '17 at 08:35
  • Definitely 100% sure of myself: it's in the doc (https://docs.zendframework.com/zend-view/helpers/head-script/#capturing-scripts) and also I just tried it on the skeleton and no problem with that. You must be doing something wrong. – Thomas Dutrion Nov 09 '17 at 09:01
  • the ref. that you shared is fine but did you check using inline script as suggessted here: https://docs.zendframework.com/zend-view/helpers/inline-script/#capturing-scripts ? – Debashis Nov 09 '17 at 09:18
  • I did indeed, if you look at how it works, InlineScript extends HeadScript, so that's why I referenced HeadScript. I did also try it on the ZendSkeletonApplication and it worked well. – Thomas Dutrion Nov 09 '17 at 10:08