<p>In PHP, <em>nothing</em> can throw an exception inside of a <code>__toString()</code> method, or else you'll get the error you're seeing.</p>

<p>Not only is that a questionable design decision, it also hides the underlying exception message and stack trace from you.</p>

<p>If you do this, at least you'll be able to log the underlying error and fix that:</p>

<pre><code>public function __toString()
{
    try
    {
        return $this-&gt;render();
    }
    catch (\Exception $e)
    {
        Craft::log('ERROR: '.$e-&gt;getMessage(), LogLevel::Error);
    }
}
</code></pre>

<p>Then you can look in your <code>craft/storage/runtime/logs</code> files for <code>[error]</code> to see the exception message.</p>

CC BY-SA 3.0
2014-10-08T18:22:29.380Z
Thanks! The error log revealed a path issue in how I was calling my template.  I needed to do this:

`$oldpath = craft()->path->getTemplatesPath();
$newpath = craft()->path->getPluginsPath().'myplugin/templates/';
$render = craft()->templates->render('mymodeltemplate');`

Though the html string returned renders in the browser with html entities encoded.  HTML entities still appear when passing the string through html_entity_decode() before returning the result in the __toString() function.  Which leaves me curious about what is happening between the Model and the template rendering engine.
Angela T
CC BY-SA 3.0
2014-10-09T15:47:53.807Z
@AngelaT Hey Angela, I’ve edited your question to just focus on the PHP error you were getting. Can you post your previous comment as an answer to the question, and then mark it as the accepted answer? I’ll answer the template escaping question in your [new post](http://craftcms.stackexchange.com/questions/3039/how-do-built-in-element-type-templates-work).
Brandon Kelly
CC BY-SA 3.0
2014-09-23T23:37:50.543Z
2014-09-23T23:37:50.543Z
Brad Bell
<p>At Brad's suggestion, the error log revealed a path issue in how I was calling my template. I needed to do this: </p>

<p><code>$oldpath = craft()-&gt;path-&gt;getTemplatesPath(); 
$newpath = craft()-&gt;path-&gt;getPluginsPath().'myplugin/templates/'; 
$render = craft()-&gt;templates-&gt;render('mymodeltemplate');</code> </p>

<p>Though the html string returned renders in the browser with html entities encoded. HTML entities still appear when passing the string through <code>html_entity_decode()</code> before returning the result in the <code>__toString()</code> function.  Brandon addresses that issue <a href="https://craftcms.stackexchange.com/questions/3039/how-do-i-prevent-twig-from-encoding-html">here</a>.</p>

CC BY-SA 3.0
2014-10-09T19:32:36.660Z
2014-10-09T19:32:36.660Z
2017-04-13T12:51:00.253Z
Angela T
<p>I have a model with the following functions:</p>

<pre class="lang-php prettyprint-override"><code>public function render() 
{
    $render = craft()-&gt;templates-&gt;render('myplugin/templates/mymodeltemplate');
    return $render;
}

public function __toString()
{
    return $this-&gt;render();
}
</code></pre>

<p>Calling <code>__toString()</code> gives me the following error:</p>

<p><em>Fatal error: Method Craft\MyModel::__toString() must not throw an exception in /projectpath/craft/app/vendor/twig/twig/lib/Twig/Extension/Core.php on line 0</em></p>

<p>I've found this explanation:</p>

<p><a href="http://php.net/manual/en/migration52.incompatible.php" rel="nofollow">http://php.net/manual/en/migration52.incompatible.php</a></p>

<p>But I'm not sure how to work around it and accomplish what I'm trying to do...</p>

<templating><plugin-development>
PHP error when rendering a template from __toString()
# PHP error when rendering a template from __toString()

(Asked by: username_0 on 2014-09-23)

I have a model with the following functions:
public function render() 
{
    $render = craft()->templates->render('myplugin/templates/mymodeltemplate');
    return $render;
}

public function __toString()
{
    return $this->render();
}

Calling __toString() gives me the following error:
Fatal error: Method Craft\MyModel::__toString() must not throw an exception in /projectpath/craft/app/vendor/twig/twig/lib/Twig/Extension/Core.php on line 0
I've found this explanation:
http://php.net/manual/en/migration52.incompatible.php
But I'm not sure how to work around it and accomplish what I'm trying to do...

**Tags:** templating, plugin-development

---

## 2 answers

### Answer by username_1 on 2014-09-23. Score: 3

In PHP, nothing can throw an exception inside of a __toString() method, or else you'll get the error you're seeing.
Not only is that a questionable design decision, it also hides the underlying exception message and stack trace from you.
If you do this, at least you'll be able to log the underlying error and fix that:
public function __toString()
{
    try
    {
        return $this->render();
    }
    catch (\Exception $e)
    {
        Craft::log('ERROR: '.$e->getMessage(), LogLevel::Error);
    }
}

Then you can look in your craft/storage/runtime/logs files for [error] to see the exception message.

> **Comments:**
> **username_0**: Thanks! The error log revealed a path issue in how I was calling my template.  I needed to do this:  `$oldpath = craft()->path->getTemplatesPath(); $newpath = craft()->path->getPluginsPath().'myplugin/templates/'; $render = craft()->templates->render('mymodeltemplate');`  Though the html string returned renders in the browser with html entities encoded.  HTML entities still appear when passing the string through html_entity_decode() before returning the result in the __toString() function.  Which leaves me curious about what is happening between the Model and the template rendering engine.  
> **username_2**: @AngelaT Hey Angela, I’ve edited your question to just focus on the PHP error you were getting. Can you post your previous comment as an answer to the question, and then mark it as the accepted answer? I’ll answer the template escaping question in your [new post](http://craftcms.stackexchange.com/questions/3039/how-do-built-in-element-type-templates-work).  

---

### Answer by username_0 on 2014-10-09. Score: 1

At Brad's suggestion, the error log revealed a path issue in how I was calling my template. I needed to do this:

`$oldpath = craft()->path->getTemplatesPath();
$newpath = craft()->path->getPluginsPath().'myplugin/templates/';
$render = craft()->templates->render('mymodeltemplate');`

Though the html string returned renders in the browser with html entities encoded. HTML entities still appear when passing the string through `html_entity_decode()` before returning the result in the `__toString()` function. Brandon addresses that issue [here](https://craftcms.stackexchange.com/questions/3039/how-do-i-prevent-twig-from-encoding-html).


---

