You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_method_gettemplatevars.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Smarty Method GetTemplateVars
  4. *
  5. * Smarty::getTemplateVars() method
  6. *
  7. * @package Smarty
  8. * @subpackage PluginsInternal
  9. * @author Uwe Tews
  10. */
  11. class Smarty_Internal_Method_GetTemplateVars
  12. {
  13. /**
  14. * Valid for all objects
  15. *
  16. * @var int
  17. */
  18. public $objMap = 7;
  19. /**
  20. * Returns a single or all template variables
  21. *
  22. * @api Smarty::getTemplateVars()
  23. * @link http://www.smarty.net/docs/en/api.get.template.vars.tpl
  24. *
  25. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
  26. * @param string $varName variable name or null
  27. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
  28. * @param bool $searchParents include parent templates?
  29. *
  30. * @return mixed variable value or or array of variables
  31. */
  32. public function getTemplateVars(Smarty_Internal_Data $data, $varName = null, Smarty_Internal_Data $_ptr = null,
  33. $searchParents = true)
  34. {
  35. if (isset($varName)) {
  36. $_var = $this->_getVariable($data, $varName, $_ptr, $searchParents, false);
  37. if (is_object($_var)) {
  38. return $_var->value;
  39. } else {
  40. return null;
  41. }
  42. } else {
  43. $_result = array();
  44. if ($_ptr === null) {
  45. $_ptr = $data;
  46. }
  47. while ($_ptr !== null) {
  48. foreach ($_ptr->tpl_vars AS $key => $var) {
  49. if (!array_key_exists($key, $_result)) {
  50. $_result[ $key ] = $var->value;
  51. }
  52. }
  53. // not found, try at parent
  54. if ($searchParents && isset($_ptr->parent)) {
  55. $_ptr = $_ptr->parent;
  56. } else {
  57. $_ptr = null;
  58. }
  59. }
  60. if ($searchParents && isset(Smarty::$global_tpl_vars)) {
  61. foreach (Smarty::$global_tpl_vars AS $key => $var) {
  62. if (!array_key_exists($key, $_result)) {
  63. $_result[ $key ] = $var->value;
  64. }
  65. }
  66. }
  67. return $_result;
  68. }
  69. }
  70. /**
  71. * gets the object of a Smarty variable
  72. *
  73. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
  74. * @param string $varName the name of the Smarty variable
  75. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
  76. * @param bool $searchParents search also in parent data
  77. * @param bool $errorEnable
  78. *
  79. * @return \Smarty_Variable
  80. */
  81. public function _getVariable(Smarty_Internal_Data $data, $varName, Smarty_Internal_Data $_ptr = null,
  82. $searchParents = true, $errorEnable = true)
  83. {
  84. if ($_ptr === null) {
  85. $_ptr = $data;
  86. }
  87. while ($_ptr !== null) {
  88. if (isset($_ptr->tpl_vars[ $varName ])) {
  89. // found it, return it
  90. return $_ptr->tpl_vars[ $varName ];
  91. }
  92. // not found, try at parent
  93. if ($searchParents && isset($_ptr->parent)) {
  94. $_ptr = $_ptr->parent;
  95. } else {
  96. $_ptr = null;
  97. }
  98. }
  99. if (isset(Smarty::$global_tpl_vars[ $varName ])) {
  100. // found it, return it
  101. return Smarty::$global_tpl_vars[ $varName ];
  102. }
  103. if ($errorEnable && $data->_getSmartyObj()->error_unassigned) {
  104. // force a notice
  105. $x = $$varName;
  106. }
  107. return new Smarty_Undefined_Variable;
  108. }
  109. }