-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Clarify @json usage in element attributes #5060
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
Conversation
I feel like this goes more towards correct Vue usage, rather than something specific to the Blade directive. |
@browner12 I used the Vue component for the example because I thought Vue is the most common use case for data attributes in Laravel applications, and the example Vue component is already included in every new Laravel install. However, I specifically referenced There are basically two ways to seed data on a page for javascript: set a variable, or place it in an element. I think including a correct example of the latter can only help. |
are you sure that single quotes are required? my guess is it has to do with correctly escaping characters when output to HTML. Are you overriding the default encoding options of |
@browner12 yes, single quotes are required because the json output uses double quotes. For example, compare the output:
The second one will fail because you end up with And no, I'm not overriding the encoding options. The So quotes within the string are escaped with their If you want double quotes on the attribute, then you have to use something like |
I guess that's why I'm confused. If you're using the default encoding, you won't run into the issue with the double quotes, correct? |
The default encoding will encode the quotes inside the string. So this description: will be encoded to
So the only way to include the json object (made up of escaped attributes and strings) inside an html attribute value is to surround it with single quotes:
Because json object has literal double quotes, unescaped. Using double quotes would terminate the attribute value early. |
ahh, got it. thanks for clearing that up for me. |
No problem! I was confused too, hence the PR. |
If I tried using double quotes instead of single quotes around
attr='@json'
, I'm sure somebody else will.