2

If I have an assigns variable, how can I pass that to the Javascript tag? The following would work in Ruby Slim, but does not seem to in Elixir's Slime.

javascript:
  window.currentUser = { username: @username }

1 Answer 1

4

You can inject a value using #{}:

javascript:
  window.currentUser = { username: #{username} }

You'll probably want to JSON encode the variable so that strings are inserted as double quoted escaped JavaScript strings. With Poison, you can do:

javascript:
  window.currentUser = { username: #{Poison.encode!(username)} }

If username is the string foo, the first one will render to:

<script>window.currentUser = { username: foo }</script>

while the second one will render to:

<script>window.currentUser = { username: "foo" }</script>
Sign up to request clarification or add additional context in comments.

1 Comment

In my case Elixir escaped HTML entities in the JSON string, so I had to additionally unescape them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.