Check if page exists, is published and has content
There are 2 page settings. Both are set.
- "featured_page_1" is set to "some-page", a published page with content.
- "featured_page_2" is set to "hidden-page", a hidden page with content.
Hidden pages are like non-existent pages, except they can still be selected in the theme settings, so you must use unless == blank
to make sure the page exists and is published, before you access it.
Liquid
<h4>Good way, using == blank check</h4>
{% unless settings.featured_page_1 == blank or pages[settings.featured_page_1] == blank or pages[settings.featured_page_1].content == blank %}
{{ pages[settings.featured_page_1].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endunless %}
<h4>Good way, using != blank check</h4>
{% if settings.featured_page_1 != blank and pages[settings.featured_page_1] != blank and pages[settings.featured_page_1].content != blank %}
{{ pages[settings.featured_page_1].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endif %}
<h4>Another good way, using empty? check</h4>
{% unless settings.featured_page_1 == blank or pages[settings.featured_page_1].empty? or pages[settings.featured_page_1].content == blank %}
{{ pages[settings.featured_page_1].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endunless %}
<h4>Shortcut way</h4>
{% if settings.featured_page_1 != blank and pages[settings.featured_page_1].content != blank %}
{{ pages[settings.featured_page_1].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endif %}
<h4>Good way, using == blank check</h4>
{% unless settings.featured_page_2 == blank or pages[settings.featured_page_2] == blank or pages[settings.featured_page_2].content == blank %}
{{ pages[settings.featured_page_2].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endunless %}
<h4>Good way, using != blank check</h4>
{% if settings.featured_page_2 != blank and pages[settings.featured_page_2] != blank and pages[settings.featured_page_2].content != blank %}
{{ pages[settings.featured_page_2].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endif %}
<h4>Another good way, using empty? check</h4>
{% unless settings.featured_page_2 == blank or pages[settings.featured_page_2].empty? or pages[settings.featured_page_2].content == blank %}
{{ pages[settings.featured_page_2].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endunless %}
<h4>Expecting non-existent or hidden resource to be nil so falsy, but pages['anything'] will always be truthy</h4>
{% if settings.featured_page_2 != blank and pages[settings.featured_page_2] %}
{{ pages[settings.featured_page_2].content }}<br>
{% else %}
<i>No content to output.</i><br>
{% endif %}
Output
Good way, using == blank check
Lorem ipsum.
Good way, using != blank check
Lorem ipsum.
Another good way, using empty? check
Lorem ipsum.
Shortcut way
Lorem ipsum.
Good way, using == blank check
No content to output.
Good way, using != blank check
No content to output.
Another good way, using empty? check
No content to output.
Expecting non-existent or hidden resource to be nil so falsy, but pages['anything'] will always be truthy
No content to output.