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.x] Add js tag #10181

Closed
wants to merge 2 commits into from
Closed

Conversation

godismyjudge95
Copy link
Contributor

A few times I have found the need to directly pass a variable for use in JS. Granted this could be done with the current {{ variable | to_json }} modifier, but I wanted an easier way.

This PR adds a {{ js }} tag for this convenience.

Example Data:

$us_states = [
    'AL' => 'Alabama',
    'AK' => 'Alaska',
];

Example Template:

{{ js:us_states }}

{{ js name="states" :from="us_states" }}

<script>
{{ js:us_states script="false" const="false" }}
</script>

Example Output:

<script>const us_states = {"AL":"Alabama","AK":"Alaska"};</script>

<script>const states = {"AL":"Alabama","AK":"Alaska"};</script>

<script>
let us_states = {"AL":"Alabama","AK":"Alaska"};
</script>

@godismyjudge95 godismyjudge95 changed the title [5.x] Add Js tag [5.x] Add js tag May 23, 2024
@godismyjudge95 godismyjudge95 changed the title [5.x] Add js tag [5.x] Add js tag May 23, 2024
@jackmcdade
Copy link
Member

I'm on the fence with this one. At the very least I don't think the name of the tag is the best fit because i can't reasonably guess as to what it would be doing. My first guess would be rendering a JS file of that name. I don't know that i'd have ever guess by looking at the template code that it was writing a script tag and converting a var to a js object. 🤔

Ultimately this is exactly the same as:

<script>const us_states = {{ us_states | to_json }}</script>

Right?

@jasonvarga
Copy link
Member

We appreciate the PR but feel like it's a bit overkill and would probably be an "issue magnet" with people wanting to add params to tweak behavior in the future etc.

It's already super easy to output a script tag with JS. You can already do what you need to with the to_json modifier, and even Laravel's Js facade in Antlers.

<script>var app = {{$ Js::from($my_array) $}};</script>

Of course if this is really important to you, you can add that tag to your own projects or bundle it as an addon.

Thanks for understanding!

@jasonvarga jasonvarga closed this May 23, 2024
@godismyjudge95 godismyjudge95 deleted the add-js-tag branch May 24, 2024 17:48
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.

3 participants