Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
{try} and {finally} blocks for nested conditional code

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  

Do you find these new tags to be a good idea?
Yes
0%
 0%  [ 0 ]
No
0%
 0%  [ 0 ]
Total Votes : 0

Author Message
manixrock
Smarty n00b


Joined: 16 Aug 2010
Posts: 1

PostPosted: Mon Aug 16, 2010 8:48 am    Post subject: {try} and {finally} blocks for nested conditional code Reply with quote

Since HTML markup almost always requires both an opening and a closing tag, and such code is sometimes dependent upon a condition, it is too often that code duplication for the opening and closing tags occurs:

Code:

{if $someContition && $someObject == 37 && $addAsLink}
<a href="http://example_com/">
{/if}
some text
{if $someContition && $someObject == 37 && $addAsLink}
</a>
{/if}


And for cases when you have multiple cases for the surrounding code, or nested cases and conditions, the code becomes very ugly and hard to read. What I'm proposing is a tag that avoids such code duplication:

Code:

{try $someContition && $someObject == 37 && $addAsLink}
<a href="http://example_com/">
{finally}some text{/finally}
</a>
{/try}


The {try} block will print normally if the condition is true, but if false will only print any contained {finally} blocks. This emulates standard try/finally code behavior of languages, and should be familiar to most developers.

Nesting is possible:

Code:

{try $someContition && $someObject == 37 && $addAsLink}
<a href="http://example_com/">
  {try $highlight}
  <b>
  {finally}some text{/finally}
  </b>
  {/try}
</a>
{/try}


Sometimes you need to alternate between different conditions, and even have a default value (if all else fails):

Code:

{try $someContition && $someObject == 37 && $addAsLink}
<a href="http://example_com/">
{catchtry $hard_highlight}
<b>
{catchtry $simple_highlight}
<i>
{catch}
<span class="simple">
{finally}some text{/finally}
</span>
{/catch}
</i>
{/catchtry}
</b>
{/catchtry}
</a>
{/try}


The three new tags {try}, {catchtry} and {catch} are the functional equivalent of {if}, {elseif} and {else}.

I think this would save a lot of code, and make markup cleaner and more functional.

As for implementation, it should not be too hard if done correctly. I could provide assistance if anyone is interested.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7062
Location: Lincoln Nebraska, USA

PostPosted: Mon Aug 16, 2010 5:15 pm    Post subject: Reply with quote

that would be confusing has hell to a typical html designer. you can implement the try stuff yourself if you really want it. everything here can be accomplished with if/else blocks, and maybe some help from includes or template inheritance.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    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