Trou­bleshoot­ing com­mon errors in Twig

Is your template throwing errors instead of displaying your content? What do those error messages mean? In this hangout we'll take a look at a few common errors in Twig and various tools we have to help identify and troubleshoot those errors as we design and develop websites with Craft CMS.

Show notes:

In this hangout, we reviewed a few common errors that may appear a bit bizarre at first. We take a look at the language the error messages are using and various tools we have available to help troubleshoot and solve these errors quickly.

Trying to output a variable that doesn't exist

Template output:

{{ nonExistentVariable }}

Error message:

Variable "nonExistentVariable" does not exist


Template output:

{{ entry.nonExistentField }}

Error message:

Craft\EntryModel and its behaviors do not have a method or closure named "nonExistentField".


Resources:

Trying to output an object or array directly as a string

Template output:

{{ entry.heroImage }}

Error message:

Object of class Craft\ElementCriteriaModel could not be converted to string


Resources:

Adding code to an extended template where it doesn't belong

Template output:

{% extends '_layout' %}	

<h1>Content outside of block tags that will throw error</h1>

{% block content %}
	...
{% endblock %}

<style>
	body { ... }
</style>

Error message:

A template that extends another one cannot have a body.


Resources:

Level up in Craft CMS with practical examples, snippets, and patterns.
Craft The Planet emails are sent out several times a week.