View previous topic :: View next topic |
Author |
Message |
mwaack Smarty Rookie
Joined: 01 Mar 2017 Posts: 7
|
Posted: Wed Sep 12, 2018 11:21 am Post subject: fetch prints result |
|
|
When u make an error in the template and user fetch() to store the Result, its always printing.
code looks like:
Code: | try {
$output = $smarty->fetch('file');
} catch (Exception $ex) {
die();
}
print $output; |
And the Template:
Code: | {$object->property} |
"property" is protected, so it throws an error.
If so, the template is still displayed with the error Message.
Expected is that nothing is displayed because "die();" |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Sep 12, 2018 11:37 am Post subject: |
|
|
Error, not exception.
If you want to catch errors as exceptions, you gotta set up the wrapper. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Sep 12, 2018 11:40 am Post subject: |
|
|
Code: | try
{
set_error_handler(function($s, $m, $f, $l, $c = null) { throw new \ErrorException($m, 0, $s, $f, $l); });
// Work
}
catch(\ErrorException $e)
{
error_log("Bad stuff happened.");
}
finally
{
restore_error_handler();
} |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Sep 12, 2018 11:41 am Post subject: |
|
|
Adding to that, if the best you can do is die() there's no good reason to catch in first place.
And display_errors should be 0 on production site. |
|
Back to top |
|
mwaack Smarty Rookie
Joined: 01 Mar 2017 Posts: 7
|
Posted: Wed Sep 12, 2018 11:57 am Post subject: |
|
|
The code is just an example, i have no die();
I have tested:
Code: | try {
set_error_handler(function ($s, $m, $f, $l, $c = null) {
throw new \ErrorException($m, 0, $s, $f, $l);
});
$output = $this->smarty->fetch($this->controller->templateFile, $this->controller->cacheId, $this->controller->compileId);
print $output;
} catch (\ErrorException $e) {
debug("ERROR");
} finally {
restore_error_handler();
} |
Fail: The $output is still displayed. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Sep 12, 2018 7:57 pm Post subject: |
|
|
And why it is displayed?
Did you try actually debug your code? |
|
Back to top |
|
mwaack Smarty Rookie
Joined: 01 Mar 2017 Posts: 7
|
Posted: Thu Sep 13, 2018 5:21 am Post subject: Thats it |
|
|
Thats the problem, why its displayed when i use "fetch" and not "display"
I Know the Error, but i want to catch it and display another Template, not the current.
The Error comes when i try to access a property that is protected.
Also other Errors, always fetch() do display the result with Error-Message.
From the Doku (https://www.smarty.net/docs/en/api.fetch.tpl)
Quote: | This returns the template output instead of displaying it.
|
But not when an error occurs. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Sep 13, 2018 10:06 am Post subject: |
|
|
Run your script in the debugger, and see, at which point it prints anything.
May be there's other interceptors that break your wrapper. |
|
Back to top |
|
|