Serdar Saygılı
Serdar Saygılı

Reputation: 471

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.

Upvotes: 10

Views: 7515

Answers (2)

data
data

Reputation: 2823

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];

Upvotes: 5

LazyOne
LazyOne

Reputation: 165511

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

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

Upvotes: 10

Related Questions