1

I've had this issue a few times & wanted to know if there is maybe a better way to deal with the following issue:

Say I have this if on a page:

{% if entry.isEditable %}
    <a href="{{ entry.cpEditUrl }}" target="_blank" id="editEntry" class="cta">Edit this entry</a>
{% endif %}

On an editable entry, the button appears if logged in. If logged out the button does not appear.

If I visit a page that is not an entry (just a template group) when logged in or not, I get this error:

Impossible to access an attribute ("isEditable") on a NULL variable ("")

The only successful way I've seen to so far to make this behave is as follows:

{% if entry is defined %}
    {% if entry.isEditable is defined %}
        {% if entry.isEditable %}
            <a href="{{ entry.cpEditUrl }}" target="_blank" id="editEntry" class="cta">Edit this entry</a>
        {% endif %}
    {% endif %}
{% endif %}

1 Answer 1

5

You could combine the first three:

{% if entry is defined and entry and entry.isEditable %}

If the first or second part of this logic fails, it will not check the third part so no error will be thrown. The second part is for the rare cases that a variable with the name entry exists but is null.

If entry is defined, it's always an entry (or at least it should be), so you don't have to check if isEditable is defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.