0

I'm trying to get this code working

{% if article_id is defined %}
    {% set entry = craft.entries.id(article_id).first() %}
{% endif %}

The code itself is unimportant, what's relevant is that I'm currently working outside of the context where I would rendering an article. This particular code happens to be broken, and I would like to fix it via inspecting {{ article_id }}. Except I can't, because:

Template Error

A template that extends another one cannot have a body.

---

{% extends "_base" %}

{% if article_id is defined %}
    {% set entry = craft.entries.id(article_id).first() %}
    {{ article_id }}
{% endif %}

Now there's a variety of ways that I could solve this issue, but I'm wondering what's the best option. Ideally it would be something like {{ craft.log(article_id) }} - ie. a function would still produce the Template Error, but at also let me see the value / other information about article_id (either in the error window, or on the javascript console log).

How should I implement the logging behavior that I'm looking for?

1
  • For the record, I solved the issue with the code above before I even posted this question. It was just the example I had on hand
    – kai
    Mar 3, 2016 at 1:50

1 Answer 1

2

If you have devMode enabled, then you have access to Twig's dump method that lets you inspect variables.

{{ dump(article_id) }}

Or, if you're feeling more adventurous, there is a debugging library called Kint that there is a Craft plugin for to help debug templates.

4
  • Dump is a very useful, but it still doesn't let me access the information when there's a Template Error ,which is my main issue. Suppressing, or writing to, the Template Error page is the main thing that I need
    – kai
    Mar 3, 2016 at 1:34
  • Oh... maybe I'm not following. You want to still be able to write to the page (or a log file) even when there's a Twig error?
    – Brad Bell
    Mar 3, 2016 at 1:37
  • Yes, I want to be able to either write to the Twig Error page, a log file, the browser log, or similar. I just looked at Kint (which is pretty nice) and it looks like it displays the errors with {{ render }} style code. It occurs to me that if it displayed with the {% render %}{% endrender %} style, it could bypass this particular Template Error. (sorry if that's not more clear!)
    – kai
    Mar 3, 2016 at 1:48
  • Hrm... I'm not sure it's possible do to what you're looking for then. When a Twig error occurs, an exception is thrown and the rest of the Twig template isn't processed, so there'd be no chance for you (or a plugin) to do anything else from Twig. The best you could do is have a plugin that listened for Yii's onError and onException events.
    – Brad Bell
    Mar 3, 2016 at 2:17

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.