|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
stonez Smarty n00b
Joined: 04 Sep 2003 Posts: 2
|
Posted: Thu Sep 04, 2003 1:36 pm Post subject: Generate a random number within smarty template? |
|
|
Is it possible to generate a random number within smarty template not in .php file.
Stonez |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
|
Back to top |
|
stonez Smarty n00b
Joined: 04 Sep 2003 Posts: 2
|
Posted: Thu Sep 04, 2003 2:11 pm Post subject: |
|
|
Thanks for the reply!
I tried to use {random in=$varIn out=$varOut assign=yourVar}
in the theme.html
but it didn't work..
What is the correct way to use this plugin?
Stonez |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Fri Sep 05, 2003 8:26 am Post subject: |
|
|
Then you assigned $varIn / $varOut wrong.
The following examples will work:
Code: | {random in=1 out=10} ---> will output random number between 1 and 10
{random in=1 out=10 assign="yourVar"} ---> will assign template var "yourVar" which you can use later as {$yourVar} |
Code: | {assign var="varIn" value=1}
{assign var="varOut" value=10}
{random in=$varIn out=$varOut assign="yourVar"}
The random number is {$yourVar} |
|
|
Back to top |
|
petyuska Smarty n00b
Joined: 30 Jan 2019 Posts: 1
|
Posted: Wed Jan 30, 2019 12:45 am Post subject: |
|
|
The mentioned URL is not accessable anymore.
I find it at web.archive.org
https://web.archive.org/web/20150908195247/http://smarty.incutio.com/?page=RandomNumberPlugin
Code: |
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: random
* Purpose: output a random number between $varIn and $varOut:
* {random in=$varIn out=$varOut}
* If you want to assign the random number to a variable
* instead of displaying it, you must write:
* {random in=$varIn out=$varOut assign=yourVar}
* Where yourVar can be anything. Then you'll get
* $yourVar equal to a random number between $varIn and $varOut.
* Author: Philippe Morange
* Modified: 25-03-2003
* -------------------------------------------------------------
*/
function smarty_function_random($params, &$smarty)
{
extract($params);
srand((double) microtime() * 1000000);
$random_number = rand($in, $out);
if (isset($assign)) {
$smarty->assign($assign, $random_number);
}
else {
return $random_number;
}
}
|
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Jan 30, 2019 12:43 pm Post subject: |
|
|
Besides, the function have a serious issue with extract(), and the behavior of Smarty::assign() has been changed since.
Don't use it. Assign all values to template explicitly. |
|
Back to top |
|
|
|
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
|