-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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 new json blade directive #21004
Conversation
Create a new concern for a new json statement
Use new CompilesJson concern
👍 for making easier to interact with Vue components |
I'd very much like this too. |
What use case do you have for writing JSON in HTML btw? |
@GrahamCampbell often, you'll find yourself in situations where you want to pass some server-side string/array/collection/whatever to your JavaScript. There are a few different ways to achieve that and outputing JSON is the most straightforward way. |
It would be rather nice if this method automatically called jsonSerialize on Collections and Models. |
So this is like a built in replacement for this package? |
@eberkund no, this blade directive is just a handy way to output json; the package you mention has other functionalities. |
@hackel Eloquent models and Collections implement |
This should be used with extreme caution due to XSS exploits. The previous method of putting json on the page is: I would suggest that the default value be 15 as done in symfony JsonResponse:
|
I don't think the new default would be an issue. I could do a pull request if @taylorotwell agrees. |
@taylorotwell @themsaid Please consider @ryantology's warning above. IMO, this is the only safe default here. Right now, the As with most other Blade features, this feature should offer a safe default, and offer the flexibility to employ less-safe variants to people who explicitly want that (and thus probably know what they are doing). This is already possible with the second |
This PR introduces a new
@json
directive for Blade. The purpose behind this new directive is to make it easier to inject PHP data structures into JS code while being IDE error free.Alternatives to this approach like
{!! $json !!}
or{!! json_encode($data) !!}
raise syntax errors in some IDEs (PhpStorm) but@json($data)
doesn't.Usage example:
let foo = @json($foo);