Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

random value in include

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
webgodjj
Smarty n00b


Joined: 30 Apr 2006
Posts: 2

PostPosted: Sun Apr 30, 2006 8:31 pm    Post subject: random value in include Reply with quote

Hi,

I am trying to switch a site over into Smarty. The site has 5 different headings that I assign in php like so:
[php:1:42c2dc6304]$rand_head = "includes/head".rand(1, 5).".php";
include($rand_head);[/php:1:42c2dc6304]
The files include different rollover images.
So far, I know that I can get a random value with smarty like so:
[php:1:42c2dc6304]{1|rand:6}[/php:1:42c2dc6304]
The question is how do I include this into smarty's include statement?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon May 01, 2006 1:15 am    Post subject: Reply with quote

There isn't an in-template random number generator that comes with Smarty, but you can do this many ways.

First thing to decide, is this template logic or application logic? In other words, should the random number / file path generation be happening in-template, or in the app? If in the app, you can assign it ahead of time:

[php:1:6312be6813]$smarty->assign('header', "includes/head".rand(1, 5).".tpl");[/php:1:6312be6813]

Code:
{include file=$header}


(if you really want to include a .php file directly instead of a .tpl file, drop it in the "trusted_dir" and use {include_php} instead of {include})

If you want this to happen in-template, you could write a custom modifier:

Code:
{include file="includes/head%s.tpl"|rand:1:5}


where the "rand" modifier would replace %s with a random number. This one has the benefit of re-usability, and easy to implement with sprintf().

Another way is to write a function that just spits out a random number, then assign it and use it however.

Code:
{rand min="1" max="5" assign="num"}
{include file="includes/head$num.tpl"}


I'm sure you get the idea. There isn't a function built into Smarty specifically for this. Maybe something in the wiki, you can have a look.
Back to top
View user's profile Send private message Visit poster's website
Dwza
Smarty Rookie


Joined: 09 Jan 2015
Posts: 7

PostPosted: Thu Jan 23, 2020 11:02 am    Post subject: Reply with quote

In Smarty 3 there is no Random or Rand function. Actually i could not find it for Smarty 2 either. So this must be a extra plugin? And if than its not needed.

You can handle +, -, /, *, abs, ceil, cos, exp, floor, log, log10, max, min, pi, pow, rand, round, sin, sqrt, srans and tan with {math}

The code would look like
Code:
{math equation="rand(min, max)" min="0" max="10" assign="num"}


Output is a random number from 0 to 10
This should work in Smarty 2 and 3
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   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