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

Block PlugIn returns twice

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


Joined: 15 Jun 2015
Posts: 2

PostPosted: Mon Jun 15, 2015 6:26 pm    Post subject: Block PlugIn returns twice Reply with quote

Hi,

one of my blog functions returns twice and i dont know why.
If i return the $content it only shows returns one time, such as it should be, but when i try to return the mysql query it shows up twice ... maybe someone could help me. The Query works fine ...

Code:

function smarty_block_title($params, $content, &$smarty, &$repeat){

    $query  = 'SELECT translation FROM **** WHERE parentID = '. $params['id'] .' AND locale = "'. $params['lang'] .'" AND type = 0 LIMIT 1';
    $sql    = mysql_query($query);
    $row    = mysql_fetch_array($sql);
   
    if($row == false) return $content;
    else return $row['translation'];
   
};


Code:

{title id="{$mainslider[nr].parentID}" lang="{$langcode}"}{$mainslider[nr].title}{/title}


Just as i said $row['translations'] shows up twice when calling this ...
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1555

PostPosted: Mon Jun 15, 2015 9:45 pm    Post subject: Reply with quote

1. $langcode = '"; DROP TABLE ***; --';
2. http://php.net/mysql_fetch_array
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Mon Jun 15, 2015 9:59 pm    Post subject: Reply with quote

Block plugin function get called on the opening and closing tag.
You must check the $repeat parameter and return output only when it is false.

See http://www.smarty.net/docs/en/plugins.block.functions.tpl
And example 18.5
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Mon Jun 15, 2015 10:08 pm    Post subject: Reply with quote

Also don't use "{ .. }" when passing parameter.

Code:
{title id=$mainslider[nr].parentID lang=$langcode}{$mainslider[nr].title}{/title}


It has bad performance and forces parameter conversion to strings which might not be right in all cases.
Back to top
View user's profile Send private message
GodHunter
Smarty n00b


Joined: 15 Jun 2015
Posts: 2

PostPosted: Tue Jun 16, 2015 6:13 am    Post subject: Reply with quote

Thanks for your help and tips, i will change the "{}" tags if i will output something. It also works to change the return by calling

Code:
if(!$repeat) return $row['translation'];


Works fine 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