How to transfer a variable from a Joomla template to FC template or module?

More
6 years 10 months ago - 6 years 10 months ago #71698 by lipatovroman
Hello!
For example I created variable in Protostar template in file

/templates/protostar/index.php

How to transfer a variable from a Joomla template to FC template or module?

for example in

/modules/mod_flexitagcloud/tmpl/default.php

Explain.

I use some code in template like:
Code:
$app = JFactory::getApplication(); $menu = $app->getMenu()->getActive()->link; $menu = JRoute::_($menu); if ( strpos($menu, '/bmw') !==false) { $modcolor = " bmwstyle "; } elseif ( strpos($menu, '/audi') !==false) { $modcolor = " audistyle "; } elseif ( strpos($menu, '/mercedes') !==false) { $modcolor = " mercedesstyle "; } elseif ( strpos($menu, '/peugeot') !==false) { $modcolor = " peugeotstyle "; } else $modcolor = false;

So, I want to make it once in template and then to get $modcolor variable in several modules and some FC fields.

Of course, I can duplicate this code (above) several times in modules and FC fields.
But I'm afraid this will slow down the site.
Thanks.
Last edit: 6 years 10 months ago by ggppdk.

Please Log in or Create an account to join the conversation.

More
6 years 10 months ago - 6 years 10 months ago #71699 by ggppdk
Hello you can use Joomla session

Set variable
Code:
$app = JFactory::getApplication(); $option = $app->input->getCmd('option'); $myvarname = 'some_calculated_value'; $app->setUserState( $option . '.myvarname' , $myvarname);


Read variable
Code:
$app = JFactory::getApplication(); $option = $app->input->getCmd('option'); $myvarname = $app->getUserState( $option . '.myvarname');

Just the code that sets variable should run before the code that reads it


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...
Last edit: 6 years 10 months ago by ggppdk.

Please Log in or Create an account to join the conversation.

More
6 years 10 months ago #71704 by lipatovroman
Thank you.
It will be faster than, my code above running 3 times on a page loading?

Please Log in or Create an account to join the conversation.

More
6 years 10 months ago - 6 years 10 months ago #71708 by ggppdk

It will be faster than, my code above running 3 times on a page loading?


the code that you posted costs almost zero time
Code:
$app = JFactory::getApplication(); // zerp $menu = $app->getMenu()->getActive()->link; // almost zero because the active item has already been calculated and cached by system $menu = JRoute::_($menu); // almost zero because the link include Item id // ... then a few strpos almost zero time again

you can measure it like this
Code:
$start_microtime = microtime(true); // ... ADD your code here $time_diff_micro_seconds = round(1000000 * 10 * (microtime(true) - $start_microtime)) / 10; echo '<h2>Costed ' . sprintf(" %.2f seconds", $time_diff_micro_seconds/1000000) . ' </h2>';


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...
Last edit: 6 years 10 months ago by ggppdk.
The following user(s) said Thank You: lipatovroman

Please Log in or Create an account to join the conversation.

More
6 years 10 months ago - 6 years 10 months ago #71836 by lipatovroman

ggppdk wrote: Hello you can use Joomla session

Set variable

Code:
$app = JFactory::getApplication(); $option = $app->input->getCmd('option'); $myvarname = 'some_calculated_value'; $app->setUserState( $option . '.myvarname' , $myvarname);


Read variable
Code:
$app = JFactory::getApplication(); $option = $app->input->getCmd('option'); $myvarname = $app->getUserState( $option . '.myvarname');

Just the code that sets variable should run before the code that reads it


Hmmm...

I inserted Set variable code in Protostar index.php

and Read variable code in components/com_flexicontent/templates/downloads/item.php

It works, but only after refreshing page.

When I open page first time - $myvarname is empty, because cookies are still empty.

So, when crawlers scaning site (which not use cookies) will not see $myvarname value on page.
Last edit: 6 years 10 months ago by lipatovroman.

Please Log in or Create an account to join the conversation.

More
6 years 10 months ago #71840 by ggppdk
Hello

if it is empty then it means that the code that uses it is executed before the code that sets it

you are suppoed to creation on the first place that is used


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
Time to create page: 0.371 seconds
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Display
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline