<p>I've run into a similar situation. I was able to get things working in my situation using Twig's <code>_context</code> variable. The object I needed was available to the page, but the object name was only known as a string on the page.</p>

<p>I set the object on the page, by accessing it with the string I had via the <code>_context</code> object. <code>_context</code> is a Global Variable in Twig, which contains all currently defined variables on the page. This made sure that I was accessing the form object being returned to the page (not redefining the object from the database).</p>

<pre><code>{% set submittedFormModel = (_context[entry.formField.first().handle] is defined 
                            ? _context[entry.formField.first().handle] 
                            : null) %}
</code></pre>

<p>From there, I could access the errors via Twig's <code>attribute</code> function:</p>

<pre><code>{% set errors = attribute(submittedFormModel, 'getErrors') %}

{% for error in errors %}
    ...
{% endfor %}
</code></pre>

<p>Or, via the object as normal:</p>

<pre><code>{% for error in submittedFormModel.getErrors() %}
    ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-06-28T14:01:49.753Z
2015-06-28T14:17:14.817Z
2015-06-28T14:17:14.817Z
Ben Parizek
<p>Twig's <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="nofollow">attribute function</a> allows you to set the method as a string and not the object. The object has to be an actual object, even with that function.</p>

<pre><code>{% set object = craft.entries.order('postdate').last() %}
{% set method = 'title' %}

{{ attribute(object, method) }}
</code></pre>

<p>The only workaround I can think of is to link your object-representing string to an actual object in your template. But this makes little sense as you probably wouldn't have set it as a string to begin with, hehe. Anyways:</p>

<pre><code>{% if string == "craft.entries.order('postdate').last()" %}
    {% set object = craft.entries.order('postdate').last() %}
{% endif %}

{{ attribute(object, method) }}
</code></pre>

CC BY-SA 3.0
2014-07-11T16:01:22.593Z
2014-07-11T16:01:22.593Z
carlcs
<p>I tried the <strong>attribute</strong> twig function for this but that did not work.</p>

<p>Essentially, what I'd like to achieve is the following:</p>

<pre><code>{# This string variable is a name of the object #}
{% set myForm = 'form' %}
{# I'd like to call the 'errors' method on the 'form' object #}
{% set formErrors = myForm.errors %}
</code></pre>

<p>The problem with the above code is that the <em>errors</em> method will be called on the <strong>myForm</strong> object instead of the <strong>form</strong> object.</p>

<p>Is there any way to achieve what I'm trying to do here?</p>

CC BY-SA 3.0
2014-07-11T15:14:24.043Z
Can you explain why you have a string that represents the object?
carlcs
<templating><variable>
Can I call methods on an object represented by a string variable?
# Can I call methods on an object represented by a string variable?

(Asked by: username_0 on 2014-07-11)

I tried the attribute twig function for this but that did not work.
Essentially, what I'd like to achieve is the following:
{# This string variable is a name of the object #}
{% set myForm = 'form' %}
{# I'd like to call the 'errors' method on the 'form' object #}
{% set formErrors = myForm.errors %}

The problem with the above code is that the errors method will be called on the myForm object instead of the form object.
Is there any way to achieve what I'm trying to do here?

**Tags:** templating, variable

> **Comments:**
> **username_1**: Can you explain why you have a string that represents the object?  

---

## 2 answers

### Answer by username_2 on 2015-06-28. Score: 2 | Accepted answer

I've run into a similar situation. I was able to get things working in my situation using Twig's _context variable. The object I needed was available to the page, but the object name was only known as a string on the page.
I set the object on the page, by accessing it with the string I had via the _context object. _context is a Global Variable in Twig, which contains all currently defined variables on the page. This made sure that I was accessing the form object being returned to the page (not redefining the object from the database).
{% set submittedFormModel = (_context[entry.formField.first().handle] is defined 
                            ? _context[entry.formField.first().handle] 
                            : null) %}

From there, I could access the errors via Twig's attribute function:
{% set errors = attribute(submittedFormModel, 'getErrors') %}

{% for error in errors %}
    ...
{% endfor %}

Or, via the object as normal:
{% for error in submittedFormModel.getErrors() %}
    ...
{% endfor %}


---

### Answer by username_1 on 2014-07-11. Score: 2

Twig's attribute function allows you to set the method as a string and not the object. The object has to be an actual object, even with that function.
{% set object = craft.entries.order('postdate').last() %}
{% set method = 'title' %}

{{ attribute(object, method) }}

The only workaround I can think of is to link your object-representing string to an actual object in your template. But this makes little sense as you probably wouldn't have set it as a string to begin with, hehe. Anyways:
{% if string == "craft.entries.order('postdate').last()" %}
    {% set object = craft.entries.order('postdate').last() %}
{% endif %}

{{ attribute(object, method) }}


---

