title: Play 2.0 access to request in templates
body: <p>I have the following simplified template setup:</p>
  
  <ul>
  <li>Main (template)
  
  <ul>
  <li>Homepage</li>
  <li>Details</li>
  </ul></li>
  </ul>
  
  <p>Now when a user logs in, a session attribute <code>username</code> is set so that I can figure out if a user is logged in or not. In order to help me to figure out if a user is logged in or not, I have the following session helper object:</p>
  
  <pre><code>object SessionHelper {
    val sessionKey = "username"
  
    def authenticated(implicit request: RequestHeader) = {
      request.session.get(sessionKey).exists(_ =&gt; true)
    }
  }
  </code></pre>
  
  <p>Which I can use in a template that takes an <code>implicit</code> request object, for example:</p>
  
  <pre><code>(implicit request: play.api.mvc.RequestHeader)
  ...
  @if(SessionHelper.authenticated) {
      &lt;strong&gt;Authenticated!&lt;/strong&gt;
  }
  ...
  </code></pre>
  
  <p>Since &mdash; as far as I can tell &mdash; this is the only way to get an <em>implicit</em> variable in a template, it means that everywhere I render a view, I need to explicitly "define" the implicit request variable. For example:</p>
  
  <pre><code>def index = Action { implicit request =&gt;
    Ok(views.html.index(myStuff))
  }
  </code></pre>
  
  <p>Without the <code>implicit request =&gt;</code> statement, it doesn't compile. Now while this is a little awkward, as long as I stay within any of the "sub-views" (e.g. Homepage or Details) that's fine because for each of those views I have a controller method and as such also access to the implicit <code>RequestHeader</code> instance. However, when I need to get access to the session in my template (e.g. Main) this doesn't work because it is never explicitly rendered by a controller.</p>
  
  <p>I'm not immediately seeing a way to get access to the session in a template, and a quick Google doesn't reveal much other than a couple questions on the same topic with no resolution. Any ideas?</p>
  
  <p><strong>Update:</strong> seems like this is somewhat related to <a href="https://stackoverflow.com/questions/9629250/how-to-avoid-passing-parameters-everywhere-in-play2">this question</a>.</p>