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

Generate a random number within smarty template?

 
Post new topic   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
stonez
Smarty n00b


Joined: 04 Sep 2003
Posts: 2

PostPosted: Thu Sep 04, 2003 1:36 pm    Post subject: Generate a random number within smarty template? Reply with quote

Is it possible to generate a random number within smarty template not in .php file.

Question

Stonez
Back to top
View user's profile Send private message
andre
Smarty Pro


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

PostPosted: Thu Sep 04, 2003 1:43 pm    Post subject: Reply with quote

You can find a random number plugin here:
http://smarty.incutio.com/?page=RandomNumberPlugin
Back to top
View user's profile Send private message
stonez
Smarty n00b


Joined: 04 Sep 2003
Posts: 2

PostPosted: Thu Sep 04, 2003 2:11 pm    Post subject: Reply with quote

Very Happy 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
View user's profile Send private message
andre
Smarty Pro


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

PostPosted: Fri Sep 05, 2003 8:26 am    Post subject: Reply with quote

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
View user's profile Send private message
petyuska
Smarty n00b


Joined: 30 Jan 2019
Posts: 1

PostPosted: Wed Jan 30, 2019 12:45 am    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Wed Jan 30, 2019 12:43 pm    Post subject: Reply with quote



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
View user's profile Send private message
Display posts from previous:   
Post new topic   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