Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

smarty_internal_method_gettemplatevars.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, $searchParents = true)
  33. {
  34. if (isset($varName)) {
  35. $_var = $this->_getVariable($data, $varName, $_ptr, $searchParents, false);
  36. if (is_object($_var)) {
  37. return $_var->value;
  38. } else {
  39. return null;
  40. }
  41. } else {
  42. $_result = array();
  43. if ($_ptr === null) {
  44. $_ptr = $data;
  45. }
  46. while ($_ptr !== null) {
  47. foreach ($_ptr->tpl_vars AS $key => $var) {
  48. if (!array_key_exists($key, $_result)) {
  49. $_result[$key] = $var->value;
  50. }
  51. }
  52. // not found, try at parent
  53. if ($searchParents) {
  54. $_ptr = $_ptr->parent;
  55. } else {
  56. $_ptr = null;
  57. }
  58. }
  59. if ($searchParents && isset(Smarty::$global_tpl_vars)) {
  60. foreach (Smarty::$global_tpl_vars AS $key => $var) {
  61. if (!array_key_exists($key, $_result)) {
  62. $_result[$key] = $var->value;
  63. }
  64. }
  65. }
  66. return $_result;
  67. }
  68. }
  69. /**
  70. * gets the object of a Smarty variable
  71. *
  72. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
  73. * @param string $varName the name of the Smarty variable
  74. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
  75. * @param bool $searchParents search also in parent data
  76. * @param bool $errorEnable
  77. *
  78. * @return \Smarty_Variable
  79. */
  80. public function _getVariable(Smarty_Internal_Data $data, $varName, Smarty_Internal_Data $_ptr = null, $searchParents = true, $errorEnable = true)
  81. {
  82. if ($_ptr === null) {
  83. $_ptr = $data;
  84. }
  85. while ($_ptr !== null) {
  86. if (isset($_ptr->tpl_vars[$varName])) {
  87. // found it, return it
  88. return $_ptr->tpl_vars[$varName];
  89. }
  90. // not found, try at parent
  91. if ($searchParents) {
  92. $_ptr = $_ptr->parent;
  93. } else {
  94. $_ptr = null;
  95. }
  96. }
  97. if (isset(Smarty::$global_tpl_vars[$varName])) {
  98. // found it, return it
  99. return Smarty::$global_tpl_vars[$varName];
  100. }
  101. /* @var \Smarty $smarty */
  102. $smarty = isset($data->smarty) ? $data->smarty : $data;
  103. if ($smarty->error_unassigned && $errorEnable) {
  104. // force a notice
  105. $x = $$varName;
  106. }
  107. return new Smarty_Undefined_Variable;
  108. }
  109. }