Answers.Body: <p>I don't think <code>same as</code> is what you're looking for in this case. Try the <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="nofollow">Twig <code>attribute</code> function</a> instead...</p>
  
  <p>If you're dealing with the global scope, it would be something like this:</p>
  
  <pre><code>{{ attribute(_context, 'myVariable') is defined ? 'Variable exists' : 'Variable does not exist' }}
  </code></pre>
  
  <p>You can always use <code>_context</code> to reference your current scope.</p>
  
  <p>If you're testing to see if an object/array <strong>contains</strong> a property, it's practically identical:</p>
  
  <pre><code>{{ attribute(myObject, 'myProperty') is defined ? 'Property exists' : 'Property does not exist' }}
  </code></pre>
  
  <p>Building off of that...</p>
  
  <ul>
  <li><code>attribute(x, y) is defined</code> will return a boolean, whether or not it exists.</li>
  <li><code>{% set value = attribute(x, y) %}</code> will return the value (assuming it exists).</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-10T18:31:29.607Z
Answers.LastActivityDate: 2015-06-10T18:31:29.607Z
Answers.UserDisplayName: Lindsey D
Answers.Body: <p>After a Slack conversation with @carlcs, we discovered another slick solution...</p>
  
  <pre><code>{{ 'myVariable' in _context|keys }}
  </code></pre>
  
  <ul>
  <li>The <a href="http://twig.sensiolabs.org/doc/templates.html#containment-operator" rel="nofollow"><code>in</code> operator</a> works much like PHP's <code>in_array</code> function.</li>
  <li>The <a href="http://twig.sensiolabs.org/doc/filters/keys.html" rel="nofollow"><code>keys</code> filter</a> works just like PHP's <code>array_keys</code> function.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-10T18:41:18.530Z
Answers.LastActivityDate: 2015-06-10T18:41:18.530Z
Answers.UserDisplayName: Lindsey D
Body: <p>Is there a way—using Twig—to check if a variable is set based on a string of the same name?</p>
  
  <p>In this example below, <code>editLink.object</code> is a string with the value of "entryAlias". I want to check to see if a variable, <code>entryAlias</code>, has been set and to see if it's an entry.</p>
  
  <pre><code>{% if "#{editLink.object}" is same as(editLink.object) %}
      it workes
  {% else %}
      not so much
  {% endif %}
  </code></pre>
  
  <p>I've looked into <code>same as</code> and <code>is</code>, but I think the problem I'm having is converting the string to an object. Also, I'm pretty sure this can just be done using PHP, but I'd like to see if it can be done within Craft's Twig so this can be cached as part of a template.</p>
  
Tags: <templating><entry>
Title: Checking for an object, based on a string, using Twig
ThreadText: # Checking for an object, based on a string, using Twig
  
  (Asked by: username_0 on 2015-06-10)
  
  Is there a way—using Twig—to check if a variable is set based on a string of the same name?
  
  In this example below, `editLink.object` is a string with the value of "entryAlias". I want to check to see if a variable, `entryAlias`, has been set and to see if it's an entry.
  
  ```
  {% if "#{editLink.object}" is same as(editLink.object) %}
      it workes
  {% else %}
      not so much
  {% endif %}
  
  ```
  
  I've looked into `same as` and `is`, but I think the problem I'm having is converting the string to an object. Also, I'm pretty sure this can just be done using PHP, but I'd like to see if it can be done within Craft's Twig so this can be cached as part of a template.
  
  **Tags:** templating, entry
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-06-10. Score: 2 | Accepted answer
  
  I don't think `same as` is what you're looking for in this case. Try the [Twig `attribute` function](http://twig.sensiolabs.org/doc/functions/attribute.html) instead...
  
  If you're dealing with the global scope, it would be something like this:
  
  ```
  {{ attribute(_context, 'myVariable') is defined ? 'Variable exists' : 'Variable does not exist' }}
  
  ```
  
  You can always use `_context` to reference your current scope.
  
  If you're testing to see if an object/array **contains** a property, it's practically identical:
  
  ```
  {{ attribute(myObject, 'myProperty') is defined ? 'Property exists' : 'Property does not exist' }}
  
  ```
  
  Building off of that...
  
  * `attribute(x, y) is defined` username_0 return a boolean, whether or not it exists.
  * `{% set value = attribute(x, y) %}` username_0 return the value (assuming it exists).
  
  
  ---
  
  ### Answer by username_1 on 2015-06-10. Score: 3
  
  After a Slack conversation with @carlcs, we discovered another slick solution...
  
  ```
  {{ 'myVariable' in _context|keys }}
  
  ```
  
  * The [`in` operator](http://twig.sensiolabs.org/doc/templates.html#containment-operator) works much like PHP's `in_array` function.
  * The [`keys` filter](http://twig.sensiolabs.org/doc/filters/keys.html) works just like PHP's `array_keys` function.
  
  
  ---
  
  