shared.literal_compiler_param.php 1000 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsShared
  7. */
  8. /**
  9. * evaluate compiler parameter
  10. *
  11. * @param array $params parameter array as given to the compiler function
  12. * @param integer $index array index of the parameter to convert
  13. * @param mixed $default value to be returned if the parameter is not present
  14. * @return mixed evaluated value of parameter or $default
  15. * @throws SmartyException if parameter is not a literal (but an expression, variable, …)
  16. * @author Rodney Rehm
  17. */
  18. function smarty_literal_compiler_param($params, $index, $default=null)
  19. {
  20. // not set, go default
  21. if (!isset($params[$index])) {
  22. return $default;
  23. }
  24. // test if param is a literal
  25. if (!preg_match('/^([\'"]?)[a-zA-Z0-9]+(\\1)$/', $params[$index])) {
  26. throw new SmartyException('$param[' . $index . '] is not a literal and is thus not evaluatable at compile time');
  27. }
  28. $t = null;
  29. eval("\$t = " . $params[$index] . ";");
  30. return $t;
  31. }