1

For a twig template file, I want to separate the js content from my HTML content, so I have created a .js file

with this content:

  {% if form.vars.data is not null %} 
  function downloadInvoice() {
    window.location = '{{ path('ziiweb_billing_bill', {'id': form.vars.data.id}) }}';
  }
  {% endif %}

  function updateTotals() {
      $('#service_download_pdf').css("pointer-events", "none");
      $('#service_download_pdf').attr("disabled", "true");

that Im adding using this line:

<script type="text/javascript" src="/bundles/ziiwebbilling/js/service_form.js"></script>

but get this error:

SyntaxError: expected expression, got '%' {% if form.vars.data is not null %}

1 Answer 1

1

You cannot put twig into your js files. However to pass twig variables to js files, you need to do have a script inside your twig file like this:

twig file

<script>
     //var data = {{ form.vars.data|default('') }};
     // I think it's better to pass the url directly
     {% if form.vars.data is not null %}
          var url = '{{ path('ziiweb_billing_bill', {'id': form.vars.data.id}) }}';
     {% endif %}
</script>

<script type="text/javascript" src="/bundles/ziiwebbilling/js/service_form.js"></script>

service_form.js

  if (typeof url === 'undefined') {
      // If the url exists
  }

Furthermore, there is a bundle that exposes routes https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

At the end, you should not define a js function if a test is valid, however you should check when running the function

Sign up to request clarification or add additional context in comments.

Comments

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.