There is such a line

<input type="text" value="{$categoryID}" name="cat"> 

Is it possible to somehow access the $ categoryID variable from the php code?

UP

smarty.php

 $smarty->assign( "categoryID", $categoryID); 

cat.html

 ... код и include файла smarty.php {php} $foo = $smarty->get_template_vars('$categoryID'); print_r($foo); {/php} 
  • you in pkhp the code threw it there $smarty->assign('categoryID, $catId) . Or do you want to extract and modify a previously added variable? - teran
  • @teran, yes, you need to change the previously added - sbaikov
  • try getTemplateVars() - teran

1 answer 1

To get the values ​​of previously assigned template variables in Smarty 3, use the getTemplateVars() method. When called without parameters, returns the entire array of assigned variables, and when specifying the variable name as a parameter, returns its value, respectively.

 <?php $smarty->assign('foo', 'bar'); $foo = $smarty->getTemplateVars('foo'); $all_tpl_vars = $smarty->getTemplateVars(); 

For earlier versions ( Smarty 2.x ) get_template_vars()

 $smarty = new Smarty; $smarty->assign('foo', 123); $foo = $smarty->get_template_vars('foo'); print_r($foo); 

UPD

Since the question was revolved, the decision changes somewhat. The above code does not refer to the template, but php code. In the case of accessing a variable from a template inside the {php} tags, the code will be as follows:

 {php} print_r($this->get_template_vars('foo')) {/php} 

Naturally, the work is already going on inside smarti itself, since what should be addressed to $this .

Well, in general, the tag {php} excluded from Smarty3, because do not interfere with logic and representation, for it is bad.

  • already tried $ foo = $ smarty-> get_template_var ('categoryID'); print_r ($ foo); But in the middle of the page the template crashes - sbaikov
  • @sbaikov and where do you have underscores in the function name? - teran
  • head already spinning. On the machine, I started writing a function from modx. I tried it now $ foo = $ smarty-> getTemplateVars ('categoryID'); print_r ($ foo); still crashing - sbaikov
  • @sbaikov smarty which version 2 or 3? - teran
  • version 2.6.10 .... - sbaikov