1

So I am trying to work with twig templates and I want to inject my project configuration in my js so I can use it.

I can do this

{% set admin_email = craft.app.projectConfig.get.email.fromEmail %}

And this gives me access to {{ admin_email }} anywhere in the document, just like <?php echo $admin_email ?> would do.

But I want to access these settings inside my JS as well. I have some tokens and other things stored inside there.

I tried doing this:

{% js %}
    let admin_email = {{ admin_email }};
    console.log(admin_email);
    alert(admin_email);
{% endjs %}

But in vain. How do I get the settings inside my JS variables?

  • What's the reason you're getting the e-mail from projectConfig and not craft.systemSettings.email.fromEmail? – dmatthams Jun 16 at 11:17
  • It keeps giving me warning that is deprecated. This is the new way of getting it. – Rohan Jun 16 at 11:19
  • That's interesting, did not know that! – dmatthams Jun 16 at 11:22
1
{% js %}
    let admin_email = "{{ admin_email }}";
    console.log(admin_email);
    alert(admin_email);
{% endjs %}

Missed the "" around the assignment. :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.