Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression expected in PhpStorm when using php variables in JavaScript functions

When I am using changing php variables to JavaScript variables, I am getting "expression expected" error from PhpStorm.

I cannot change the extension of the file to something.js.php because I am already using blade template so it should be blade.php

This is the first example

This is the second example

<!DOCTYPE html>
<html>
<body>
<?php $myVar = 5;?>

<script type="text/javascript">
    var myJavascriptVar = <?php echo $myVar; ?>;
    var myJavascriptSecondVar = {{$myVar;}};
    alert(myJavascriptVar + myJavascriptSecondVar);
</script>
</body>
</html>

I have added a sample html page for more clarification. In PhpStrom the

var myJavascriptVar = <?php echo $myVar; ?>;

and

 var myJavascriptSecondVar = {{$myVar;}};

statements gives expression expected error.

like image 363
Serdar Saygılı Avatar asked Aug 02 '16 11:08

Serdar Saygılı


2 Answers

That's a bug (incomplete inter-language handling) in PhpStorm.

  • https://youtrack.jetbrains.com/issue/WI-24968
  • https://youtrack.jetbrains.com/issue/WI-25739
  • possibly some another (from "Related" list) as well

Watch those tickets (star/vote/comment) to get notified on any progress. Right now they are not assigned to any specific future versions.

like image 101
LazyOne Avatar answered Nov 04 '22 01:11

LazyOne


Here are two workarounds:

1. function

function blade(_)
{
    return _;
}

var data = blade({{ $data }});
// or ES6 arrow function
var data = (_ => _)({{ $data }});

2. array

var data = [{{ $data }}].pop();
// or
var data = [{{ $data }}][0];
like image 44
data Avatar answered Nov 04 '22 03:11

data