Answers.Body: <p>The Twig <a href="https://craftcms.com/docs/templating/cache" rel="nofollow">{% cache %}</a> will only cache HTML output, not variables.  You could, however, store the value you want to check against in <a href="https://straightupcraft.com/craft-plugins/lj-cookies" rel="nofollow">a</a> <a href="https://straightupcraft.com/craft-plugins/cookies" rel="nofollow">cookie</a> or into the browser's local storage.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-21T19:19:32.833Z
Answers.LastActivityDate: 2016-01-21T19:19:32.833Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>You can accomplish this by using the set/endset tags to create a variable from the cached output:</p>
  
  <pre><code>{% set finalVariable %}
      {% cache %}
          {# heavy lifting (like queries) here #}
          {# output the contents of your variable here #}
          your output here or {{ yourOutputHere }}
      {% endcache %}
  {% endset %}
  </code></pre>
  
  <p>In the above example the <code>finalVariable</code> will be set to the value of the cached content.</p>
  
  <p>Or, in the case of the code example used in the question, something like this could be used:</p>
  
  <pre><code>{% set id %}{% spaceless %}
      {% cache using key "cacheId" %}
          1
      {% endcache %}
  {% endspaceless %}{% endset %}
  </code></pre>
  
  <p>It's a very basic example that would probably never be used, but the <code>id</code> variable would be set to a string of <code>1</code> from the <code>cacheId</code> cache. </p>
  
  <p>Note: The spaceless tags are just used to remove the whitespace.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-14T07:54:06.063Z
Answers.Text: You also need to use `{%-` instead of `{%` to remove whitespace inbetween the Twig tags.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-13T23:46:53.850Z
Answers.LastActivityDate: 2016-04-13T23:46:53.850Z
Answers.UserDisplayName: Aaron Waldon
Body: <p>I would like to cache a variable and compare the cached variable with a parameter from the query string.</p>
  
  <p>Is it possible to somehow cache a variable ?</p>
  
  <pre><code>{% cache using key "cacheId" %}
    {% set id = 1 %}
  {% endcache %}
  </code></pre>
  
Tags: <cache>
Title: Is it possible to cache variables using the {% cache %} tag?
ThreadText: # Is it possible to cache variables using the {% cache %} tag?
  
  (Asked by: username_0 on 2016-01-21)
  
  I would like to cache a variable and compare the cached variable with a parameter from the query string.
  
  Is it possible to somehow cache a variable ?
  
  ```
  {% cache using key "cacheId" %}
    {% set id = 1 %}
  {% endcache %}
  
  ```
  
  **Tags:** cache
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-01-21. Score: 3 | Accepted answer
  
  The Twig [{% cache %}](https://craftcms.com/docs/templating/cache) will only cache HTML output, not variables. You could, however, store the value you want to check against in [a](https://straightupcraft.com/craft-plugins/lj-cookies) [cookie](https://straightupcraft.com/craft-plugins/cookies) or into the browser's local storage.
  
  
  ---
  
  ### Answer by username_2 on 2016-04-13. Score: 5
  
  You can accomplish this by using the set/endset tags to create a variable from the cached output:
  
  ```
  {% set finalVariable %}
      {% cache %}
          {# heavy lifting (like queries) here #}
          {# output the contents of your variable here #}
          your output here or {{ yourOutputHere }}
      {% endcache %}
  {% endset %}
  
  ```
  
  In the above example the `finalVariable` will be set to the value of the cached content.
  
  Or, in the case of the code example used in the question, something like this could be used:
  
  ```
  {% set id %}{% spaceless %}
      {% cache using key "cacheId" %}
          1
      {% endcache %}
  {% endspaceless %}{% endset %}
  
  ```
  
  It's a very basic example that would probably never be used, but the `id` variable would be set to a string of `1` from the `cacheId` cache.
  
  Note: The spaceless tags are just used to remove the whitespace.
  
  > **Comments:**
  > **username_3**: You also need to use `{%-` instead of `{%` to remove whitespace inbetween the Twig tags.  
  
  ---
  
  