Error Handling in Smarty 3

1,095 views
Skip to first unread message

Martin Spütz

unread,
May 10, 2010, 6:00:55 AM5/10/10
to smarty-d...@googlegroups.com
Hello!

If there is an error, I don’t want Smarty to display anything.

If I have a template without any include it does work fine. But is the
error in the included file, smarty still displays the parent
template(s). Keep in mind that I'm using fetch() and not display().

I'll give you some code.

test.php
try {
$tpl = new Smarty();
$tpl->setTemplateDir("path-to-templates");
$tpl->fetch("test1.tpl");
} catch (Exception $e) {
print $e->getMessage();
// btw: don't use print $e; it will give you the page somehow (maybe
some issue with __toString())
}

test1.tpl:
<h1>Header</h1>
<p>bla bla bla</p>
{include file="test2.tpl"}
<h6>Footer</h6>

test2.tpl:
<h2>H2lkjaslkj asdklfjaslkdjfaslkjd</h2>
trigger an error
{lkasjs }


Smarty gives me:

<h1>Header</h1>
<p>bla bla bla</p>
Syntax Error in template "path-to-templates/test2.tpl" on line 3
"{lkasjs }" - Unexpected " }", expected one of: "}" , " "


Is there any way to suppress the html?

Greetings
Martin

--
You received this message because you are subscribed to the Google Groups "Smarty Developers" group.
To post to this group, send email to smarty-d...@googlegroups.com.
To unsubscribe from this group, send email to smarty-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/smarty-developers?hl=en.

Monte Ohrt

unread,
May 10, 2010, 9:51:22 AM5/10/10
to smarty-d...@googlegroups.com
template errors are handled with the PHP error handler instead of
exceptions, since they are not considered fatal errors. You can redirect
PHP errors as per usual.

Koen

unread,
May 19, 2010, 12:02:48 PM5/19/10
to Smarty Developers
I'm having the same issue here...

Any solution yet?

Thue Janus Kristensen

unread,
May 19, 2010, 3:00:50 PM5/19/10
to smarty-d...@googlegroups.com
A solution is to use $smarty->fetch instead of $smarty->display:

$html = $LIBsetup->smarty->fetch("test.tpl");   
echo $html;

Regards, Thue

uwe.tews

unread,
May 19, 2010, 4:07:52 PM5/19/10
to Smarty Developers
The problem is that Smarty may have open output buffers with content
which gets flushed when the script terminates.

Solution:

try {
$html = $tpl->fetch();
}
catch (Exception $e) {
while (1 < ob_get_level()) {
ob_end_clean();
}
}


while (1 < ob_get_level()) does output any echo before the try,

while (0 < ob_get_level()) will suppress also echo before try if not
yet flushed.


Uwe
> > smarty-develop...@googlegroups.com<smarty-developers%2Bunsu...@googlegroups.com>
> > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/smarty-developers?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Smarty Developers" group.
> > To post to this group, send email to smarty-d...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > smarty-develop...@googlegroups.com<smarty-developers%2Bunsu...@googlegroups.com>
> > .

Thue Janus Kristensen

unread,
May 20, 2010, 6:40:35 AM5/20/10
to smarty-d...@googlegroups.com
I missed that fetch was already used in the original template :(.

Shouldn't Uwe's code be added inside the official smarty fetch function?

Regards, Thue

Martin Spütz

unread,
May 21, 2010, 3:46:13 AM5/21/10
to smarty-d...@googlegroups.com
On 20.05.2010 12:40, Thue Janus Kristensen wrote:
> I missed that fetch was already used in the original template :(.
>
> Shouldn't Uwe's code be added inside the official smarty fetch function?

Besides that you can easily change it yourself:

> --- libraries/Smarty/Smarty.php (revision 132)
> +++ libraries/Smarty/Smarty.php (working copy)
> @@ -329,12 +329,21 @@
> $_gmt_mtime = '';
> }
> }
> - // return redered template
> - if (isset($this->autoload_filters['output']) || isset($this->registered_filters['output'])) {
> - $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_template->getRenderedTemplate(), $this, $_template);
> - } else {
> - $_output = $_template->getRenderedTemplate();
> +
> + try {
> + // return redered template
> + if (isset($this->autoload_filters['output']) || isset($this->registered_filters['output'])) {
> + $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_template->getRenderedTemplate(), $this, $_template);
> + } else {
> + $_output = $_template->getRenderedTemplate();
> + }
> + } catch (Exception $e) {
> + while (1 < ob_get_level()) {
> + ob_end_clean();
> + }
> + throw $e;
> }

Greetings
Martin

--
You received this message because you are subscribed to the Google Groups "Smarty Developers" group.
To post to this group, send email to smarty-d...@googlegroups.com.

Thue Janus Kristensen

unread,
May 21, 2010, 6:24:45 AM5/21/10
to smarty-d...@googlegroups.com
This is buggy if the fetch is already inside a ob_start(). You need to start by using ob_get_level() before the try, and then only call ob_end_clean() until that level.

Regards, Thue
Reply all
Reply to author
Forward
0 new messages