Body: <p>I'm working with a template in Craft 2, and in referencing Twig variables in the Javascript, they are not recognized when the page is previewed.  I get a <code>Template Error - Variable x does not exist</code></p>
  <p>Initially, I constructed the Javascript function by referencing the Twig variables defined earlier in the template like this (below).  I got the error message that the Twig variable referenced here (<code>myText</code>) does not exist:</p>
  <pre><code>{%set myText=entry.body.getPage(1)|e('html')%}
  {% set myJs %}
  function myFunction(btn) 
  {
      var FullText='{{myText}}';
  }
  {% endset %}
  {% includeJs myJs %}
  </code></pre>
  <p>When I got the template error, I changed it to this (below), which gave the error that the variable reference here (<code>entry</code>) does not exist.:</p>
  <pre><code>{% set myJs %}
  function myFunction(btn) 
  {
      var myText='{{entry.body.getPage(1)|e('html')}}';
  }
  {% endset %}
  {% includeJs myJs %}
  </code></pre>
  <p>Does this have to do with the positioning of the Javascript in the Template's code, or could it have to do with the Twig variables somehow?
  Thanks for any leads.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-12T07:52:36.437Z
Comments.Text: I guess this might be a problem with `entry.body.getPage(1)|e('html')`. Have you tried to set mytext to something simple like "Hello World"? And could you edit `Template Error - Variable x does not exist` and add the actual variable name that does not exist? This would make it easier to tell what the error is.
Comments.UserDisplayName: Max Strebel
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-12T09:38:59.940Z
Comments.Text: don't know if that's the issue but i think you're missing an `{% endset %}` tag?
Comments.UserDisplayName: Seb
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-12T18:47:04.570Z
Comments.Text: @Seb Thanks for the catch.  I corrected the example to reflect my code, which does have the `{%endset%}` tag.  It is giving the error message, even though the` {%endset%}` is there.
  
  @Max I've included the error above.  Thanks
  
  When I put 'hello world', it did work, but stopped working when I put the Twig there.
Comments.UserDisplayName: buck1112
Tags: <templating><error-message><craft2><javascript>
Title: Craft 2 - Referencing Twig Variables in Javascript - Variables Do Not Exist
ThreadText: # Craft 2 - Referencing Twig Variables in Javascript - Variables Do Not Exist
  
  (Asked by: username_0 on 2023-10-11)
  
  I'm working with a template in Craft 2, and in referencing Twig variables in the Javascript, they are not recognized when the page is previewed. I get a `Template Error - Variable x does not exist`
  
  Initially, I constructed the Javascript function by referencing the Twig variables defined earlier in the template like this (below). I got the error message that the Twig variable referenced here (`myText`) does not exist:
  
  ```
  {%set myText=entry.body.getPage(1)|e('html')%}
  {% set myJs %}
  function myFunction(btn) 
  {
      var FullText='{{myText}}';
  }
  {% endset %}
  {% includeJs myJs %}
  
  ```
  
  When I got the template error, I changed it to this (below), which gave the error that the variable reference here (`entry`) does not exist.:
  
  ```
  {% set myJs %}
  function myFunction(btn) 
  {
      var myText='{{entry.body.getPage(1)|e('html')}}';
  }
  {% endset %}
  {% includeJs myJs %}
  
  ```
  
  Does this have to do with the positioning of the Javascript in the Template's code, or could it have to do with the Twig variables somehow?
  Thanks for any leads.
  
  **Tags:** templating, error-message, craft2, javascript
  
  > **Comments:**
  > **username_1**: I guess this might be a problem with `entry.body.getPage(1)|e('html')`. Have you tried to set mytext to something simple like "Hello World"? And could you edit `Template Error - Variable x does not exist` and add the actual variable name that does not exist? This would make it easier to tell what the error is.  
  > **username_2**: don't know if that's the issue but i think you're missing an `{% endset %}` tag?  
  > **username_0**: @username_2 Thanks for the catch.  I corrected the example to reflect my code, which does have the `{%endset%}` tag.  It is giving the error message, even though the` {%endset%}` is there.  @Max I've included the error above.  Thanks  When I put 'hello world', it did work, but stopped working when I put the Twig there.  
  
  ---
  
  ## 0 answers
  
  