Skip to content

Instantly share code, notes, and snippets.

@faffyman
Created March 28, 2012 15:34
Show Gist options
  • Save faffyman/2227378 to your computer and use it in GitHub Desktop.
Save faffyman/2227378 to your computer and use it in GitHub Desktop.
Simple Smarty Plugin for generating a random integer
<?php
/**
* Smarty plugin wrapper for PHP's rand function
*
* @package Smarty
* @subpackage PluginsFunction
*/
/**
* Smarty {app_rand} function plugin
*
* Type: function<br>
* Name: app_rand<br>
* Date: Feb 17, 2011<br>
* Purpose: generate a random integer
* Examples: <img src="/images/image{app_rand low=1 high=10}.gif" width="400" height="23"">
* Output: <img src="/images/image1.gif" width="400" height="23"">
*
* @author Tim Swann <https://github.com/faffyman/>
* @version 1.0
* @param array $params parameters
* Input:<br>
* - low = lowest number
* - high = highest number
* @return string/int
*/
function smarty_function_app_rand($params, $template)
{
$low = 0;
$high = 99;
foreach($params as $_key => $_val) {
switch ($_key) {
case 'low':
case 'high':
$$_key = intval($_val);
break;
default:
throw new SmartyException ("app_rand: extra attribute not allowed ", E_USER_NOTICE);
break;
}
}
if (empty($high)) {
trigger_error("app_rand: missing 'high' number parameter", E_USER_NOTICE);
return;
}
$nRandom = rand($low,$high);
return $nRandom;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment