Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Something like {myif} {myelse} {/myif}

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


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue May 13, 2003 9:12 am    Post subject: Something like {myif} {myelse} {/myif} Reply with quote

I just thought about implementing a custom if/then/else clause. Perhaps it would be usefull for some authorization checking like this:
Code:

  {isAuthorized module="News"}
     ...
  {otherwise}
     ...
  {/isAuthorized}


This could be more readable than
Code:

  {isAuthorized module="News"}
    ...
  {/isAuthorized}
  {isNotAuthorized module="News"}
    ...
  {/isNotAuthorized}
 


But I am still thinking about if it's a good idea or not Confused

Any opinions? How whould you do something like this?


Btw: {if isAuthorized(...)} {else} {/if} is not the same because I could declare {isAuthorized} as not cachable (see my nocache patch)!
Back to top
View user's profile Send private message
user2222
Smarty Rookie


Joined: 18 Apr 2003
Posts: 7

PostPosted: Mon Jun 16, 2003 9:04 am    Post subject: Reply with quote

But why don't you use the IF_ELSEIF tags
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Jun 16, 2003 9:31 am    Post subject: Reply with quote

Hi andre.

Does this not do it for you?
Code:
{if isAuthorized->module("news")}
{else}
{/if}

or:
Code:
{if "news"|isAuthorized:"module"}
{else}
{/if}


For the fun of it, how about this (not serious) trick (err, hack):
Code:
{section name=auth loop=isAuthorized->module("news")}
{sectionelse}
{/section}


I remember there was a thread awhile back about adding {switch} and in retrospect I think that would be a more useful contruct than private conditional types. I especially think "otherwise" is a bad idea since it is non-standard verbage. You're template users are going to have to remember when to use else and when to use otherwise, etc. Sounds like it shouldn't be too hard to remember, but life is troublesome enough, yes? (nb. in your case: isAuthorizedElse)

Well, just my 2c anyways!
Back to top
View user's profile Send private message
bok
Smarty n00b


Joined: 16 Jun 2003
Posts: 2
Location: Cape Town, South Africa

PostPosted: Mon Jun 16, 2003 8:05 pm    Post subject: Reply with quote

I was very uncreative when I needed to implement something like this. I just wrote a block function that generated an IF statement into the compiled template, and a standard smarty function that inserted the else into the right place, no error checking or smarty handling though, but it suited my purpose.

-bok
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
boots
Administrator


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

PostPosted: Tue Jun 17, 2003 12:10 am    Post subject: Reply with quote

@bok: that's cool too, but it supposes that the block function knows what to put in place for the else condition whereas I *think* that andre is trying to keep that user controlled.
Back to top
View user's profile Send private message
bok
Smarty n00b


Joined: 16 Jun 2003
Posts: 2
Location: Cape Town, South Africa

PostPosted: Thu Jun 19, 2003 9:42 pm    Post subject: Reply with quote

Maybe, but I think the reason I wrote the function is the same as andre's, access control.

So something like:
Code:
{access acl="x"}
   // do something if they have 'x' access level
{accesselse}
   // do this if they dont
{/access}

suited me perfectly.

-bok
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Jun 23, 2003 7:28 am    Post subject: Reply with quote

Sorry, for not answering such a long time... I was on vacations Very Happy Very Happy

I think I'll try bok's suggestion. It sounds best for my purpose Wink
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Jun 23, 2003 4:40 pm    Post subject: Reply with quote

I gotta throw in my best-practices 2c Smile

This "isAuthorized" stuff does not belong in the template. The template should only be concerned about what to display, not what authorizations someone in the application has. So, a better approach:

{if $display_edit_button}
...
{/if}

Whereas the application has to set the $display_edit_button boolean value depending on this persons authorization (you can use an object method too, same thing.)

Monte
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Wed Jun 25, 2003 6:27 am    Post subject: Reply with quote

We already had this discussusion, Monte. You remember? Wink
Back to top
View user's profile Send private message
silicate
Smarty n00b


Joined: 09 Sep 2003
Posts: 4
Location: Toronto, Ontario, Canada

PostPosted: Tue Jan 06, 2004 3:05 am    Post subject: Reply with quote

Hey,

I was looking for a plug-in that emulates the switch statement in php. This is as close as I can come it appears.

Is there an example of this access implementation around?

Later,

Silicate.
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jan 07, 2004 12:13 am    Post subject: Reply with quote

i wrote two plugins some time ago, that do something similar:
[php:1:63b21b0f86]
<?php

/**
* Smarty {pages}{/pages} block plugin
*
* Type: block function<br>
* Name: pages<br>
* Purpose: container for {page}...{/page} blocks
* author: messju mohr <messju@lammfellpuschen.de>
* @param array
* Params: pageno: integer or null (null)
* @param string contents of the block
* @param Smarty
* @return string
*/
function smarty_block_pages($params, $content, &$smarty, &$pages)
{
if (is_null($content) && !array_key_exists('pageno', $params)) {
$smarty->trigger_error("{pages}: parameter 'pageno' not given");


}
return $content;
}

?>


<?php

/**
* Smarty {page}{/page} block plugin
*
* Type: block function<br>
* Name: page<br>
* Purpose: element inside {pages}...{/pages} block
* author: messju mohr <messju@lammfellpuschen.de>
* @param array
* Params: pageno: integer or null (null)
* @param string contents of the block
* @param Smarty
* @return string
*/
function smarty_block_page($params, $content, &$smarty, &$repeat)
{
if (is_null($content)) {
/* handle block open tag */

/* find corresponding {pages}-block */
for ($i=count($smarty->_tag_stack)-1; $i>=0; $i--) {
list($tag_name, $tag_params) = $smarty->_tag_stack[$i];
if ($tag_name=='pages') break;
}

if ($i<0) {
/* {pages} not found */
$smarty->tigger_error("{page}: block not inside pages}-context");

return;
}

if (isset($tag_params['_done']) && $tag_params['_done']) {
/* another {page} was already found */
$repeat = false;
return;
}

if (isset($params['pageno']) && ($params['pageno']!=$tag_params['pageno'])) {
/* page doesn't match */
$repeat = false;
return;
}

/* page found */
$smarty->_tag_stack[$i][1]['_done'] = true;
return;

} else {
/* handle block close tag */
return $content;

}

}

?>
[/php:1:63b21b0f86]

used like:
Code:

{pages pageno=$pageno}

{page pageno=1}
foo-1
{/page}

{page pageno=2}
foo-2
{/page}

{page}
default-case
{/page}

{/pages}


they where thought for multi-page forms or an installation-wizard-in-one-template or something like that but maybe they can be used for other switches.

"pages" could be seen as "switch" and "page" could be seen as "case".

feel free to use them.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dabase
Smarty n00b


Joined: 26 Jul 2003
Posts: 4

PostPosted: Wed Feb 18, 2004 12:54 pm    Post subject: Reply with quote

user2222 wrote:
But why don't you use the IF_ELSEIF tags


someone may not allow php code exept template plugins, this includes fuction calls to a api too. To use lagacy apis anyway, they have to be wrapped by functions, blocks etc in such a case.


greets
Back to top
View user's profile Send private message
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