Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add a new line after the @json blade directive #21431

Closed
wants to merge 1 commit into from

Conversation

chrisbjr
Copy link
Contributor

This pull request adds a new line after the JSON encoding of the @json blade directive.

We've experienced a bug when using the @json blade directive in the following scenario:

<script>
  var foo = @json($foo)
  var bar = 2
</script>

The above output will return something like this:

<script>
  var foo = {"id": 1}    var bar = 2
</script>

Obviously, we get a syntax error with that output. Adding a new line at the end of @json fixes this problem and will return the following output:

<script>
  var foo = {"id": 1}    
  var bar = 2
</script>

@sisve
Copy link
Contributor

sisve commented Sep 28, 2017

Is this really specific to the @json directive? What if I have actual inline json? Wouldn't it be more reasonable to change so that the regex (or whatever does it) doesn't match/consume the newlines after the ending paranthes?

@themsaid
Copy link
Member

What if I'm passing it inside a method that has other params? I won't want a new line in this case.

@taylorotwell
Copy link
Member

We may want to update the docs to show putting a semi-colon after the json directive

@chrisbjr
Copy link
Contributor Author

Good points. Totally overlooked those scenarios. This won't happen with a semicolon but we've just recently switched to following StandardJS so no semicolons for us supposedly. I wonder why the new line is getting removed after declaring the blade directive in this scenario though.

var foo = @json($foo)
var bar = 2

currently doing this to go around no semicolons:

var foo = @json($foo)

var bar = 2

@franzliedke
Copy link
Contributor

Ah, the good old newline eating.

I've sent a PR to the docs with a note that explains (and warns about) this behavior. Oh, and it adds the semicolon.

@chrisbjr chrisbjr deleted the new-line-after-json-encode branch October 21, 2017 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants