View previous topic :: View next topic |
Author |
Message |
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Tue May 13, 2003 9:12 am Post subject: Something like {myif} {myelse} {/myif} |
|
|
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
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 |
|
user2222 Smarty Rookie
Joined: 18 Apr 2003 Posts: 7
|
Posted: Mon Jun 16, 2003 9:04 am Post subject: |
|
|
But why don't you use the IF_ELSEIF tags |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Jun 16, 2003 9:31 am Post subject: |
|
|
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 |
|
bok Smarty n00b
Joined: 16 Jun 2003 Posts: 2 Location: Cape Town, South Africa
|
Posted: Mon Jun 16, 2003 8:05 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jun 17, 2003 12:10 am Post subject: |
|
|
@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 |
|
bok Smarty n00b
Joined: 16 Jun 2003 Posts: 2 Location: Cape Town, South Africa
|
Posted: Thu Jun 19, 2003 9:42 pm Post subject: |
|
|
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Mon Jun 23, 2003 7:28 am Post subject: |
|
|
Sorry, for not answering such a long time... I was on vacations
I think I'll try bok's suggestion. It sounds best for my purpose |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Jun 23, 2003 4:40 pm Post subject: |
|
|
I gotta throw in my best-practices 2c
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Wed Jun 25, 2003 6:27 am Post subject: |
|
|
We already had this discussusion, Monte. You remember? |
|
Back to top |
|
silicate Smarty n00b
Joined: 09 Sep 2003 Posts: 4 Location: Toronto, Ontario, Canada
|
Posted: Tue Jan 06, 2004 3:05 am Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Jan 07, 2004 12:13 am Post subject: |
|
|
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 |
|
dabase Smarty n00b
Joined: 26 Jul 2003 Posts: 4
|
Posted: Wed Feb 18, 2004 12:54 pm Post subject: |
|
|
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 |
|
|