<p>I'm curious to know your use case for this, but have you tried using Twig's <code>do</code> tag?</p>
<p><a href="https://twig.symfony.com/doc/2.x/tags/do.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/tags/do.html</a></p>
<p>i.e. <code>{% do craft.app.session.set(&quot;mySession&quot;, &quot;someValue&quot;) %}</code></p>

CC BY-SA 4.0
2020-08-24T18:15:22.887Z
Thanks, clearly it’s the Twig docs I need to spend more time in.
mitrol
CC BY-SA 4.0
2020-08-24T17:46:55.340Z
2020-08-24T17:46:55.340Z
James Smith
<p>apologies if this has been asked, I don’t seem to be able to find the solution.</p>
<p>What is the correct way to call craft.app.session.set in a Twig template (or any other setter function for that matter)? I can’t find a working example in the docs for anything other than getters.</p>
<p>To expand, this causes an error:</p>
<pre><code>{# grab initial value, it is empty #}
{% set mySession = craft.app.session.get(&quot;mySession&quot;) %}
{{ &quot;mySession = #{mySession}&quot; }}

{# set value to 'someValue' - this will cause the error #}
{% craft.app.session.set(&quot;mySession&quot;, &quot;someValue&quot;) %}

{# grab value again, it should now be 'someValue' #}
{% set mySession = craft.app.session.get(&quot;mySession&quot;) %}
{{ &quot;mySession = #{mySession}&quot; }}
</code></pre>
<p>However this works as expected:</p>
<pre><code>{# grab initial value, it is empty #}
{% set mySession = craft.app.session.get(&quot;mySession&quot;) %}
{{ &quot;mySession = #{mySession}&quot; }}

{# set value to 'someValue' #}
{% set somePointlessVariable = craft.app.session.set(&quot;mySession&quot;, &quot;someValue&quot;) %}

{# grab value again, it’s now 'someValue' #}
{% set mySession = craft.app.session.get(&quot;mySession&quot;) %}
{{ &quot;mySession = #{mySession}&quot; }}
</code></pre>
<p>Surely I don’t need to create some pointless variable just to use the setter function?</p>

<templating><classes>
Calling a setter via craft.app in Twig
# Calling a setter via craft.app in Twig

(Asked by: username_0 on 2020-08-24)

apologies if this has been asked, I don’t seem to be able to find the solution.
What is the correct way to call craft.app.session.set in a Twig template (or any other setter function for that matter)? I can’t find a working example in the docs for anything other than getters.
To expand, this causes an error:
{# grab initial value, it is empty #}
{% set mySession = craft.app.session.get("mySession") %}
{{ "mySession = #{mySession}" }}

{# set value to 'someValue' - this will cause the error #}
{% craft.app.session.set("mySession", "someValue") %}

{# grab value again, it should now be 'someValue' #}
{% set mySession = craft.app.session.get("mySession") %}
{{ "mySession = #{mySession}" }}

However this works as expected:
{# grab initial value, it is empty #}
{% set mySession = craft.app.session.get("mySession") %}
{{ "mySession = #{mySession}" }}

{# set value to 'someValue' #}
{% set somePointlessVariable = craft.app.session.set("mySession", "someValue") %}

{# grab value again, it’s now 'someValue' #}
{% set mySession = craft.app.session.get("mySession") %}
{{ "mySession = #{mySession}" }}

Surely I don’t need to create some pointless variable just to use the setter function?

**Tags:** templating, classes

---

## 1 answers

### Answer by username_1 on 2020-08-24. Score: 3 | Accepted answer

I'm curious to know your use case for this, but have you tried using Twig's `do` tag?

<https://twig.symfony.com/doc/2.x/tags/do.html>

i.e. `{% do craft.app.session.set("mySession", "someValue") %}`

> **Comments:**
> **username_0**: Thanks, clearly it’s the Twig docs I need to spend more time in.  

---

