Re: [SMARTY] how to use a function inside brackets?

From: Date: Tue, 18 Mar 2003 15:43:34 +0000
Subject: Re: [SMARTY] how to use a function inside brackets?
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-8715@lists.php.net to get a copy of this message
Ok so if I understood right, here's how I did it: In function.random.php: $random_number = rand($in, $out); if (isset($assign)) { $smarty->assign($assign, $random_number); } else { return $random_number; } In template: {random in=1 out=3 assign=random_number} {include file="menu_element.tpl" menu_img="svg$random_number"} Is it what you meant Messju? I was not sure on how to code the function to reflect what you said... But it works like this, maybe it's a little cleaner than before. Philippe. messju mohr <messju@lammfellpuschen.de> le 17/03/2003 16:21:23 Veuillez répondre à messju mohr <messju@lammfellpuschen.de> Pour : Philippe MORANGE/EST/DOSI/BANQUE_INDOSUEZ/FR@BANQUE INDOSUEZ cc : smarty-general@lists.php.net (ccc : Philippe MORANGE/EST/DOSI/BANQUE_INDOSUEZ/FR) Objet : Re: [SMARTY] how to use a function inside brackets? On Mon, Mar 17, 2003 at 03:57:54PM +0100, philippe.morange@ca-indosuez.com wrote: > Hi, I don't if this has been already discussed, but I didn't find related material > about this... > I made a custom function that outputs a random number between two numbers. > For example, if I do {random in=5 out=10} it will output 8... > > what I want to do is display a random image. > My images are numbered like that: > img1.gif > img2.gif > img3.gif > > In HTML I do <img src="img{random in=1 out=3}.gif"> and it works fine. > > But I am using this in a file I include (I specify the image name): > {include file="menu_element.tpl" menu_img="Image.gif" > menu_text="Network diagram"} > > How could I use my random function in that case? > I can't do the following: > {include file="menu_element.tpl" menu_img="img{random in=1 out=3}.gif" > menu_text="Network diagram"} > > I found a workaround: > {random in=1 out=3 outputToVar=true} > {include file="menu_element.tpl" menu_img="img$random_number.gif" > menu_text="Network diagram"} > And in my function, if outputToVar is set to true I assign the result to a var instead of outputting it... > > My workaround works, but I thinks it kind of sucks... call your "outputToVar" "assign" and give it the name of the variable to assign to. then it works like {include}, {insert} and maybe a few other built-in php-function i cannot remember at the moment. i don't think it sucks then, but fits pretty well to smarty. alternatetivly you can: {capture name="bla"}{your_function}{/capture} {assign var=foo value=$smarty.capture.bla} which sucks more. have fun messju > Do you have any suggestion of how I should do that? > I really would be great to ba able to use curly brackets inside curly brackets !!! > > Thanks, > Philippe.

« previous php.smarty.general (#8715) next »