简体   繁体   中英

How pass variable from template index.php to the component in Joomla?

I use joomla 3.9.24. In my template I have to pass a variable to the component (specifically com_content / article). In practice, depending on whether or not there are modules in a certain position, it must report the title in the article or not. JRequest is deprecated as suggested in Joomla pass value from component to use in plugin and module I wrote this:

in the template index.php file

title_var = 1;
...
$app = JFactory::getApplication();
$input = $app->input;
$input->set('title_var', $title_var);

in the default.php file

$app = JFactory::getApplication();
$input = $app->input;
$title_var = $input->get('title_var');

But it does not work, For example, if I insert

var_dump($input);

in both files, in the same page I will be shown the input data twice except for my title_var which in the var_dump of the component does not appear at all, as if the data did not pass.

How can i solve?

Update: the same using template parameters

in the template index.php file

$app  = JFactory::getApplication();
$params = $app->getTemplate(true)->params;
...
$variable  = $params->set('var_title', $var_title);

similar in the default.php file

$app  = JFactory::getApplication();
$paramsT = $app->getTemplate(true)->params;
...
$variable  = $paramsT->get('var_title'); #result null

You can use sessions to pas variables from templates to components..

Secondly You can use template parameters..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM