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

{if} plugin? (block efficiency)

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
Lyrion
Smarty n00b


Joined: 02 Sep 2004
Posts: 2

PostPosted: Thu Sep 02, 2004 9:52 am    Post subject: {if} plugin? (block efficiency) Reply with quote

Hi,

i want to write a block plugin, which tests for a condition, and depending on result does or does not output the content of the block.

The problem is, when the block function gets called at the end of the block, all the content has already been processed by Smarty (which takes unnecessary time, if i dont want to output it).

Should i rather make a function which sets a variable, and then continue testing with {if} {/if} ?

Example of what I want the block to do:
Code:
{CheckUserRights file="secrets" action="viewkillerofkennedy"}
Huge amount of massive secrets here...
{/CheckUserRights}

I am a beginner with Smarty, so please dont beat me, if the example isn't syntactically correct, I just made this up.

The function itself checks the (already logged on) user against mySQL, and if he has the privilege to view (or do, click, whatever) something. Could be used to hide a "edit" button if youre not administrator or whatever.

If there is a lot of stuff (and other Smarty-Tags) in within the block, the time would be wasted, if i decided in the first call, that i dont want to display it at all...

I read about the &repeat variable, but cant see how this would help me.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Sep 02, 2004 2:00 pm    Post subject: Reply with quote

An easier and more common approach is to do the work in PHP, then assign a flag for the template to use:

Code:
{if $show_edit_button}
   ... show the button ...
{/if}


The presentation layer (template) only needs to decide whether to show the button or not. It does not need to be involved in user rights (application logic.)
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


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

PostPosted: Thu Sep 02, 2004 2:02 pm    Post subject: Reply with quote

yes, &$repeat is what you want here. just like here

[php:1:45254ecd4d]
function smarty_block_CheckUserRights($params, $content, $smarty, &$repeat) {
if (!user_has_permissions()) {
$repeat=false;
}
return $content;
}
[/php:1:45254ecd4d]

normally the block function is called twice. once with empty content at the beginning and once with the processed content at the end.

if you set $repeat to false on the first call (at the beginning) then the function doesn't get called a second time and the contents between the start and the end-tag are processed zero times. i think this is just what you want.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Lyrion
Smarty n00b


Joined: 02 Sep 2004
Posts: 2

PostPosted: Thu Sep 02, 2004 3:20 pm    Post subject: Reply with quote

thank you, messju

i obviously misunderstood what &$repeat is or how it works.
its clear now Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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