Answers.Body: <p>Sounds like you need a good <strong>cookie</strong> solution to keep track of which background has been chosen for each visitor.</p>
  
  <p>I'd recommend using the <a href="https://github.com/lewisjenkins/craft-lj-cookies" rel="nofollow">LJ Cookies plugin</a> to easily set &amp; retrieve the filename of the background image.</p>
  
  <p>(I don't believe the <code>cache</code> tag will be useful to you in this case.)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T20:23:29.253Z
Answers.Text: Thanks for the recommendation Lindsey. I was hoping there would be a native way to accomplish this without having to resort to a plug-in.
Answers.UserDisplayName: Adam Snetman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T21:50:25.823Z
Answers.Text: The closest thing to a native way of handling this would be to write a small plugin with a [template hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail), and let your template hook get & set the cookie for you. But honestly, that's practically the same thing, so I'd still recommend going with the lightweight LJ Cookies plugin.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T19:00:14.003Z
Answers.LastActivityDate: 2015-01-06T19:00:14.003Z
Answers.UserDisplayName: Lindsey D
Body: <p>I'm working on a site where the site background is chosen randomly from among the entries in an "artwork" channel. I'm using the following code in my <code>_layout.html</code> template to get the url for the chosen image:</p>
  
  <pre><code>{# choose a random entry for the site background #}
  {% set randomArtwork = craft.entries.section('artwork').order('RAND()').first() %}
  
  {# set the url for the background image #}
  {% set backgroundImageUrl = randomArtwork.artImageBkg.first().url %}
  </code></pre>
  
  <p>The chosen image is then displayed by adding an inline style to the body tag like this:</p>
  
  <pre><code>&lt;body{% if backgroundImage is defined %} style="background-image: url({{ backgroundImageUrl }});"{% endif %}&gt;
  </code></pre>
  
  <p>Right now, this code runs on every page request and a different image is displayed on every page load on the site.</p>
  
  <p>However, I'd like to be able to run this code only once per user, per site visit so that the background image will change on repeat visits to the site, but on a single visit, the user will see one persistent image.</p>
  
  <p>How can I go about doing this in Craft?</p>
  
  <p>As an alternative, I also thought to use Craft's <code>{% cache %}</code> tags to set my template variables on a timed basis so that visitors in a particular hour would only see one persistent image, but I couldn't figure out how to get this to work.</p>
  
  <p>When I tried to use the <code>{% cache %}</code> tags on individual templates, I got a warning that a template that extends another (in this case, my <code>_layout.html</code> template) can't have a body.</p>
  
  <p>And adding the <code>{% cache %}</code> tags to my <code>_layout.html</code> template wasn't affecting the persistence of the background image.</p>
  
  <p>Any ideas?</p>
  
Tags: <templating><variable><session>
Title: How can I set a persistent template variable to use across multiple pages in one site visit?
ThreadText: # How can I set a persistent template variable to use across multiple pages in one site visit?
  
  (Asked by: username_0 on 2015-01-06)
  
  I'm working on a site where the site background is chosen randomly from among the entries in an "artwork" channel. I'm using the following code in my `_layout.html` template to get the url for the chosen image:
  
  ```
  {# choose a random entry for the site background #}
  {% set randomArtwork = craft.entries.section('artwork').order('RAND()').first() %}
  
  {# set the url for the background image #}
  {% set backgroundImageUrl = randomArtwork.artImageBkg.first().url %}
  
  ```
  
  The chosen image is then displayed by adding an inline style to the body tag like this:
  
  ```
  <body{% if backgroundImage is defined %} style="background-image: url({{ backgroundImageUrl }});"{% endif %}>
  
  ```
  
  Right now, this code runs on every page request and a different image is displayed on every page load on the site.
  
  However, I'd like to be able to run this code only once per user, per site visit so that the background image will change on repeat visits to the site, but on a single visit, the user will see one persistent image.
  
  How can I go about doing this in Craft?
  
  As an alternative, I also thought to use Craft's `{% cache %}` tags to set my template variables on a timed basis so that visitors in a particular hour would only see one persistent image, but I couldn't figure out how to get this to work.
  
  When I tried to use the `{% cache %}` tags on individual templates, I got a warning that a template that extends another (in this case, my `_layout.html` template) can't have a body.
  
  And adding the `{% cache %}` tags to my `_layout.html` template wasn't affecting the persistence of the background image.
  
  Any ideas?
  
  **Tags:** templating, variable, session
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-01-06. Score: 1
  
  Sounds like you need a good **cookie** solution to keep track of which background has been chosen for each visitor.
  
  I'd recommend using the [LJ Cookies plugin](https://github.com/lewisjenkins/craft-lj-cookies) to easily set & retrieve the filename of the background image.
  
  (I don't believe the `cache` tag will be useful to you in this case.)
  
  > **Comments:**
  > **username_0**: Thanks for the recommendation username_1. I was hoping there would be a native way to accomplish this without having to resort to a plug-in.  
  > **username_1**: The closest thing to a native way of handling this would be to write a small plugin with a [template hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail), and let your template hook get & set the cookie for you. But honestly, that's practically the same thing, so I'd still recommend going with the lightweight LJ Cookies plugin.  
  
  ---
  
  