Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

try/catch for smarty (with patch)

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
Swatinem
Smarty Rookie


Joined: 26 Jan 2005
Posts: 12

PostPosted: Wed Jan 26, 2005 10:32 pm    Post subject: try/catch for smarty (with patch) Reply with quote

Well i thought this feature would be nice, since i want to make exception handling on the template level.

And i even made a "quick and dirty" implementation of this using the compiler class.

This is what the template would look like:
Code:

{try}
    before exception
    {throwsexception}
    after exception
{catch type="extendedException" assign="e1"}
    extendedException was thrown
    {$e1->getMessage()}
{catch assign="e2"}
    Exception was thrown
    {$e2->getMessage()}
{/try}


And this is the small patch that i wrote. Why can't I attach files by the way?
Code:

Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
retrieving revision 1.358
diff -u -r1.358 Smarty_Compiler.class.php
--- Smarty_Compiler.class.php   21 Jan 2005 17:46:14 -0000   1.358
+++ Smarty_Compiler.class.php   26 Jan 2005 21:44:18 -0000
@@ -508,6 +508,42 @@
                     return "<?php endforeach; endif; unset(\$_from); ?>";
                 break;
 
+            case 'try':
+                $this->_push_tag('try');
+                if(version_compare(phpversion(),"5.0.0") == -1)
+                {
+                    $this->_syntax_error("try/catch blocks are only supported with PHP versions 5 and above.", E_USER_WARNING, __FILE__, __LINE__);
+                    return;
+                }
+                return "<?php try { ?>";
+                break;
+
+            case 'catch':
+                list($_open_tag) = end($this->_tag_stack);
+                if ($_open_tag != 'try' && $_open_tag != 'catch')
+                    $this->_syntax_error('unexpected {catch}', E_USER_ERROR, __FILE__, __LINE__);
+                //else
+                    //$this->_push_tag('catch');
+                $attrs = $this->_parse_attrs($tag_args);
+                if(empty($attrs["assign"]))
+                {
+                    $this->_syntax_error("catch: missing 'assign' attribute", E_USER_ERROR, __FILE__, __LINE__);
+                }
+                if(empty($attrs["type"]))
+                {
+                    $type = "Exception";
+                }
+                else
+                {
+                    $type = $this->_dequote($attrs["type"]);
+                }
+                return "<?php } catch($type \$e) { \$this->_tpl_vars[".$attrs["assign"]."] = \$e; ?>";
+
+            case '/try':
+                $this->_pop_tag('try');
+                return "<?php } ?>";
+                break;
+
             case 'strip':
             case '/strip':
                 if ($tag_command{0}=='/') {


Maybe one of the devs can look into this if there is interest.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jan 27, 2005 2:45 am    Post subject: Reply with quote

Personally, I think it is better to do this as a plugin -- probably because I can't really see the reason to have try/catch in the context of a template. In typical usage templates don't acquire resources* and even when a resource is used/referenced by a template it is managed outside of the context of the template. It is up to you if you want to use Smarty to script objects -- but Smarty is not a replacement for PHP.

So I ask, how would try/catch fit into the normal usage pattern of a template? How are you using it?

----
*by resource I do not mean the Smarty "resource" plugin type but rather any external entity that requires management and error control/recovery.
Back to top
View user's profile Send private message
Swatinem
Smarty Rookie


Joined: 26 Jan 2005
Posts: 12

PostPosted: Thu Jan 27, 2005 6:31 am    Post subject: Reply with quote

i planned something like this:
Code:

{if !empty($smarty.post.submitted)}
   {try}
      {post->submit}
      Form has been successfully submitted
   {catch}
      An Error occured.
   {/try}
{else}
   {*print the form elements*}
{/if}

I want to have total control over the success or error messages. In particular the further links that get printed.

Another method would be to return a smarty template by the post->submit method. But that would be too unflexible.
I would have to map all the messages and links that may or may not be printed. And of course that method would require static success/error templates.
{post->submit success="successtpl" failure="failuretpl"} would be a more flexible method but then i will have a lot of small templates i have to deal with.
Another thing to note would be that i want my classes to be as smarty independent as it can be. Wink

I will try to implement try/catch as a plugin when i come back from school.
Back to top
View user's profile Send private message
jasonlustig
Smarty n00b


Joined: 10 Mar 2005
Posts: 4

PostPosted: Sun Mar 13, 2005 6:24 pm    Post subject: Reply with quote

In terms of submitting the form, really that should not be in the template code but in the php code. You can do your trying/catching in the php, and based on what happens, go to the proper template.

Jason
Back to top
View user's profile Send private message
lynweb
Smarty n00b


Joined: 18 Jan 2006
Posts: 4
Location: Norway

PostPosted: Thu Jan 19, 2006 11:15 am    Post subject: Reply with quote

I can think of a context that could have use for it.
Say you have your entire system in OOP PHP-code.
Perhaps you have a class NewsItem that has a method called getComments().
The getComments method has again calls to e.g. a DB that it queries for a resultset and generates the proper result (perhaps Comment objects).
You would asign one or several NewsItems to the template, and you would call getComments() on the objects to get the comments.

Somewhere in this code something goes wrong and throws an Exception.
I think that PHP should handle this, not the template, BUT, as it seems from my experiments, you can't get the Exception this way:
Code:

try {
     // news.tpl has call to $newsItem->getComments() that throws Exception
     $smarty->display('news.tpl');
} catch (Exception $e) {
      echo $e->getMessage();
}


From what I've tried the code never reaches the catch-part Sad
Any solutions to this problem?
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP