<p>Testing for the <a href="http://buildwithcraft.com/docs/routing" rel="nofollow">pre-populated variables</a> is probably the only way to do this. But as you're asking for a single (global) var to test against, I believe you're looking for a way to have clean, dry code in your shared code block without using your posted snippet again and again.</p>

<p>How about doing something like this:</p>

<pre><code>{% set entry = entry is defined ? entry : null %}
{% set category = category is defined ? category : null %}
</code></pre>

<p>Then your two variables <code>entry</code> and <code>category</code> will always be defined, which allows you to have very short <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow">ternary syntax</a> in your print expressions.</p>

<pre><code>&lt;div class="myDefaultClass{{ entry ? ' myEntryClass' }}"&gt;&lt;/div&gt;
</code></pre>

<p>Another thing you could do is to set a new var <code>model</code>, which holds either the Entry Model or the Category Model.</p>

<pre><code>{% set model = entry is defined ? entry : category is defined ? category : null %}
{% set entry = entry is defined ? entry : null %}
{% set category = category is defined ? category : null %}

&lt;h1&gt;{{ model.title }}&lt;/h1&gt;
</code></pre>

CC BY-SA 3.0
2015-11-12T14:36:01.390Z
Nice answer @carlcs. I think you need to use the full ternary instead of the twig shorthand though, otherwise it will return a boolean.  `entry is defined ? entry : null`
Fyrebase
CC BY-SA 3.0
2015-11-12T15:14:04.290Z
In my testings `entry is defined ? entry : null` and `entry is defined ?: null` did the same. I was surprised when I first noticed this, maybe it's a bug?!
carlcs
CC BY-SA 3.0
2015-11-13T00:05:50.943Z
Things may have changed. I had to go with the full ternary to get it to work.
Fyrebase
CC BY-SA 3.0
2015-11-13T00:08:27.567Z
Corrected the answer. Thanks for testing and reporting the issue, @Fyrebase!
carlcs
CC BY-SA 3.0
2015-03-07T18:28:18.913Z
2015-11-13T00:07:28.250Z
2015-11-13T00:07:28.250Z
carlcs
<p>It'd be nice to see an example of your HTML block. From your comments it sounds like you may be using the same HTML block for <code>Entry</code> and <code>Category</code> pages but need to know which variable to use in the code.</p>

<p>Assuming you know that your code is running on the Entry or Category pages, and that the fields across both your Entries and Categories for this part of the page are the same, you can place your common HTML in an include and pass both the <code>entry</code> and <code>category</code> variable to that include with the same variable name.  In the example below, we're handing off the <code>entry</code> and <code>category</code> variables and using the variable name <code>object</code> in the include template:</p>

<p><strong>_entry.html</strong></p>

<pre><code>{% include '_partials/shared-template' with { object : entry } %}
</code></pre>

<p><strong>_category.html</strong></p>

<pre><code>{% include '_partials/shared-template' with { object : category } %}
</code></pre>

<p><strong>_partials/shared-template</strong></p>

<pre><code>&lt;div&gt;
    {{ object.fieldName }}

    {% for item in object.someOtherFieldName %}
      {{ item }}
    {% endfor %}
&lt;/div&gt;
</code></pre>

<p>If some of the field names are different, you may need to use some conditional logic or reconsider what variables are being passed to the included template.  You could pass dissimilar variables under the same variable names to a common template in the following way:</p>

<p><strong>index.html</strong></p>

<pre><code>{% if entry is defined %}
    {% include '_partials/shared-template' with { 
        title : entry.title,
        someOtherVariable: entry.fancyFieldOne
    } %}
{% endif %}

{% if category is defined %}
    {% include '_partials/shared-template' with { 
        title : category.title,
        someOtherVariable: category.fancyFieldTwo
    } %}
{% endif %}
</code></pre>

<p><strong>_partials/shared-template</strong></p>

<pre><code>&lt;div&gt;
    {{ title }}

    {% for item in someOtherVariable %}
      {{ item }}
    {% endfor %}
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2015-03-07T19:41:16.160Z
2015-03-07T19:41:16.160Z
Ben Parizek
<p>Is there another way of detecting if a page is an 'entry' or a 'category' than the following? Maybe some global variable that I do not know of?</p>

<pre><code>{% if entry is defined %}
  &lt;!-- Code here --&gt;
{% endif %}
{% if category is defined %}
  &lt;!-- Code here --&gt;
{% endif %}
</code></pre>

<p>I have a html code block that is included in both entry pages and category pages hence the need for this.</p>

<p>Thanks in advance!</p>

<templating><entry><category>
Detecting if page is a 'category' or 'entry'
# Detecting if page is a 'category' or 'entry'

(Asked by: username_0 on 2015-03-07)

Is there another way of detecting if a page is an 'entry' or a 'category' than the following? Maybe some global variable that I do not know of?
{% if entry is defined %}
  
{% endif %}
{% if category is defined %}
  
{% endif %}

I have a html code block that is included in both entry pages and category pages hence the need for this.
Thanks in advance!

**Tags:** templating, entry, category


---

## 2 answers

### Answer by username_3 on 2015-03-07. Score: 10 | Accepted answer

It'd be nice to see an example of your HTML block. From your comments it sounds like you may be using the same HTML block for Entry and Category pages but need to know which variable to use in the code.
Assuming you know that your code is running on the Entry or Category pages, and that the fields across both your Entries and Categories for this part of the page are the same, you can place your common HTML in an include and pass both the entry and category variable to that include with the same variable name.  In the example below, we're handing off the entry and category variables and using the variable name object in the include template:
_entry.html
{% include '_partials/shared-template' with { object : entry } %}

_category.html
{% include '_partials/shared-template' with { object : category } %}

_partials/shared-template

    {{ object.fieldName }}

    {% for item in object.someOtherFieldName %}
      {{ item }}
    {% endfor %}


If some of the field names are different, you may need to use some conditional logic or reconsider what variables are being passed to the included template.  You could pass dissimilar variables under the same variable names to a common template in the following way:
index.html
{% if entry is defined %}
    {% include '_partials/shared-template' with { 
        title : entry.title,
        someOtherVariable: entry.fancyFieldOne
    } %}
{% endif %}

{% if category is defined %}
    {% include '_partials/shared-template' with { 
        title : category.title,
        someOtherVariable: category.fancyFieldTwo
    } %}
{% endif %}

_partials/shared-template

    {{ title }}

    {% for item in someOtherVariable %}
      {{ item }}
    {% endfor %}


---

### Answer by username_1 on 2015-03-07. Score: 6

Testing for the pre-populated variables is probably the only way to do this. But as you're asking for a single (global) var to test against, I believe you're looking for a way to have clean, dry code in your shared code block without using your posted snippet again and again.
How about doing something like this:
{% set entry = entry is defined ? entry : null %}
{% set category = category is defined ? category : null %}

Then your two variables entry and category will always be defined, which allows you to have very short ternary syntax in your print expressions.


Another thing you could do is to set a new var model, which holds either the Entry Model or the Category Model.
{% set model = entry is defined ? entry : category is defined ? category : null %}
{% set entry = entry is defined ? entry : null %}
{% set category = category is defined ? category : null %}

{{ model.title }}

> **Comments:**
> **username_2**: Nice answer @username_1. I think you need to use the full ternary instead of the twig shorthand though, otherwise it will return a boolean.  `entry is defined ? entry : null`  
> **username_1**: In my testings `entry is defined ? entry : null` and `entry is defined ?: null` did the same. I was surprised when I first noticed this, maybe it's a bug?!  
> **username_2**: Things may have changed. I had to go with the full ternary to get it to work.  
> **username_1**: Corrected the answer. Thanks for testing and reporting the issue, @username_2!  

---

