<p>Your <code>entry_pos</code> is an <a href="https://docs.craftcms.com/v2/templating/elementcriteriamodel.html" rel="nofollow noreferrer">Element Criteria Model</a>. You want to get the first (and probably only) category from this ElementCriteriaModel, and then compare its title to <code>'staff'</code>:</p>

<pre><code>{% set entry_pos = entry.position.first %}
{% if entry_pos.title == 'staff' %}
  ...
{% endif %}
</code></pre>

<p>This will give you an error if there is no <code>entry.position.first</code>, as <code>entry_pos</code> will be <code>null</code>, so you should defend against that somehow. One possility is:</p>

<pre><code>{% if entry_pos and entry_pos.title == 'staff' %} ...
</code></pre>

<p>Another is to use twigs useful <a href="https://twig.symfony.com/doc/2.x/templates.html#other-operators" rel="nofollow noreferrer">null-coalescing operator</a>:</p>

<pre><code>{% if (entry_pos.title ?? '') == 'staff' %} ...
</code></pre>

<p>With Craft 3, you will use <code>one</code> instead of <code>first</code>, but otherwise everything is the same.</p>

CC BY-SA 3.0
2018-04-12T16:14:10.520Z
Would parentheses be needed in the compound conditional statement (above)?  For some reason, the if/then statement is returning 'yes' to both 'staff' and 'faculty'.
buck1112
CC BY-SA 3.0
2018-04-13T11:06:37.183Z
I'm not sure if the ()'s are needed - depends on twig operator precedence.
Marion Newlevant
CC BY-SA 3.0
2018-04-20T21:02:31.133Z
Thanks.  That did the job - I added 'lower' to it to fix the remainder of the problem.  {% if entry_pos and (entry_pos.title|lower != 'staff') %}
buck1112
CC BY-SA 3.0
2018-04-12T15:35:54.443Z
2018-04-12T15:35:54.443Z
Marion Newlevant
<p>I'm new to Craft and am trying to set up a condition that will compare the value of a category (entry.position).  However, I get this error message:</p>

<pre><code>Object of class Craft\ElementCriteriaModel could not be converted to string
</code></pre>

<p>I'm not really sure how to do a textual comparison.  The 'position' field is, in my case, a category.  How would a comparison be achieved?</p>

<p>I've tried this:</p>

<pre><code>{% set entry_pos = entry.position %}
{#   {%  craft.categories.slug('position') %} #}                 
{{ entry_pos }}

{% if entry.position != 'staff' %}
    {% if entry.UBFUserName != '' %}
        {% set name_var = entry.UBFUserName %}
            {{ craft.aitest.AiTestVariable(name_var) }}
        {% else %}
    {% endif %}      
{% endif %}
</code></pre>

<templating>
Compare Value to Category Title In Template
# Compare Value to Category Title In Template

(Asked by: username_0 on 2018-04-12)

I'm new to Craft and am trying to set up a condition that will compare the value of a category (entry.position).  However, I get this error message:
Object of class Craft\ElementCriteriaModel could not be converted to string

I'm not really sure how to do a textual comparison.  The 'position' field is, in my case, a category.  How would a comparison be achieved?
I've tried this:
{% set entry_pos = entry.position %}
{#   {%  craft.categories.slug('position') %} #}                 
{{ entry_pos }}

{% if entry.position != 'staff' %}
    {% if entry.UBFUserName != '' %}
        {% set name_var = entry.UBFUserName %}
            {{ craft.aitest.AiTestVariable(name_var) }}
        {% else %}
    {% endif %}      
{% endif %}

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2018-04-12. Score: 2 | Accepted answer

Your entry_pos is an Element Criteria Model. You want to get the first (and probably only) category from this ElementCriteriaModel, and then compare its title to 'staff':
{% set entry_pos = entry.position.first %}
{% if entry_pos.title == 'staff' %}
  ...
{% endif %}

This will give you an error if there is no entry.position.first, as entry_pos will be null, so you should defend against that somehow. One possility is:
{% if entry_pos and entry_pos.title == 'staff' %} ...

Another is to use twigs useful null-coalescing operator:
{% if (entry_pos.title ?? '') == 'staff' %} ...

With Craft 3, you will use one instead of first, but otherwise everything is the same.

> **Comments:**
> **username_0**: Would parentheses be needed in the compound conditional statement (above)?  For some reason, the if/then statement is returning 'yes' to both 'staff' and 'faculty'.  
> **username_1**: I'm not sure if the ()'s are needed - depends on twig operator precedence.  
> **username_0**: Thanks.  That did the job - I added 'lower' to it to fix the remainder of the problem.  {% if entry_pos and (entry_pos.title|lower != 'staff') %}  

---

