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

check for boolean value (from object)

 
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 -> General
View previous topic :: View next topic  
Author Message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Fri Apr 25, 2003 8:38 pm    Post subject: check for boolean value (from object) Reply with quote

Hi!

I have an element in my template, which should be displayed after a special user-right is checked. Only some users may see this element. In PHP I could check this right like that:

Code:
if($user->checkRight("display_element_xy")) {
    echo "something secret";
}

(checkRight(...) returns TRUE/FALSE)

That's what I postet yesterday, the question I have now - how should I implement this with smarty?

should I use "register_object" - as mentioned there? This would mean something like:

PHP:
Code:
$smarty->register_object("user",$user);
$smarty->display("index.tpl");



TEMPLATE:

Code:
{user->checkRight p1="display_feature_xy" assign="output"}
{if $output == 'TRUE'}
something secret...
{/if}



I do not like this because it means a lot of "business-logic" in the templates.

A better way would be:

PHP:
Code:
$smarty->assign('display_element_xy', $user->checkRight("display_element_xy"));
$smarty->display("index.tpl");



TEMPLATE:

Code:
{if $display_element_xy == 'TRUE'}
something secret...
{/if}



But somehow I'm not very lucky about it, what do you think would be the best way to separate presentation from business-logic? The first way is too much ligic in my template, the second way builts more dependencies between php-script and template, but perhaps its the best way, what do you think about it? I'm not very experienced in using smarty yet, so I don't really know what would be the better way.

Is there another way to check for a boolean value as using {if $value == 'TRUE'}?

Thank you!

regards
Andreas

________
vapor genie vaporizer

Last edited by andreas on Fri Feb 04, 2011 9:00 am; edited 2 times in total
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Apr 25, 2003 9:07 pm    Post subject: Reply with quote

maybe you can weaken the depencies a bit:

use a hash-array with all right-names as keys an values as true.

$smarty->assign('display_element', $user->rights_map());
with rights_map returning an "array('ab'=>true, 'xy'=>true)"

and in smarty you do:
{if $display_element.xy} you can xy(/if}
{if !$display_element.ab} you cannot ab (/if}
and even
{if $display_element.$foo} you can {$foo} (/if}
with $foo being a right assinged sometimes before.

i don't know if that fits for you.



(implied in the example above, but:)
to the second question:
{if $display_element_xy == 'TRUE'} is redundant
do simply {if $display_element_xy}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Sat Apr 26, 2003 8:36 am    Post subject: Reply with quote

Hm it's a nice idea, but I think I will have too many rights, I only want to check one right from database.

I would like to use something like:

{if checkRight("display_xy") }
display XY...
{/if}

checkRight would be a registered function, but I think I have to fetch the return-Value, assign as outpu and than check this.

But isn't it possible to get the right checked as above? Perhaps per own plugin? I do not want to assign the return-value in my plugin. Or is this the only possibility if I don't want to assign it from php?

________
iolite vaporizer


Last edited by andreas on Fri Feb 04, 2011 9:00 am; edited 2 times in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sat Apr 26, 2003 8:53 am    Post subject: Reply with quote

hmmm. Looks like your last example is the same as using

{if $user->checkRight("display_xy") }

{/if}

is that not what you want?
Back to top
View user's profile Send private message
andre
Smarty Pro


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

PostPosted: Sat Apr 26, 2003 9:44 am    Post subject: Reply with quote

Quote:

{if $user->checkRight("display_xy") }

{/if}

Even if this is not the best way to do rights checking I prefer exactly this way too.
Back to top
View user's profile Send private message
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 -> General 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