Accessing template parameters in component's tmpl
Moderator: General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
-
- Joomla! Fledgling
- Posts: 4
- Joined: Sun Jul 20, 2008 2:03 am
Accessing template parameters in component's tmpl
Hi,
I'm new to Joomla and I'm really amazed of its huge possibilities. Unfortunately when I started creating my custom template I found a problem and I can't find solution. I was trying on google but it's really hard to find something useful.
Basically I added parameters to the templateDetails.xml file and I can access them easily from index.php .However I would like to access this parameters from components/com_content/view/article/default.php file. Is that possible at all? I was trying $this->params->get('paramName') and it doesn't work.
Thanks
Michal
I'm new to Joomla and I'm really amazed of its huge possibilities. Unfortunately when I started creating my custom template I found a problem and I can't find solution. I was trying on google but it's really hard to find something useful.
Basically I added parameters to the templateDetails.xml file and I can access them easily from index.php .However I would like to access this parameters from components/com_content/view/article/default.php file. Is that possible at all? I was trying $this->params->get('paramName') and it doesn't work.
Thanks
Michal
- drewhemm
- Joomla! Apprentice
- Posts: 37
- Joined: Wed Jan 24, 2007 11:34 pm
- Location: Spain
- Contact:
Re: Accessing template parameters in component's tmpl
Hi Michal,
Did you ever find a solution for this issue? What I have found out by using a PHP debugger is that the template overrides contained in Joomla/templates/TEMPLATE_NAME/html are loaded BEFORE the template's params.ini file and therefore the parameters are available. I imagine this is the same for the files in Joomla/components/COMPONENT_NAME/view. I am trying to see if they can be loaded manually by using JDocument or JFactory but so far to no avail...
Regards
Andrew
Did you ever find a solution for this issue? What I have found out by using a PHP debugger is that the template overrides contained in Joomla/templates/TEMPLATE_NAME/html are loaded BEFORE the template's params.ini file and therefore the parameters are available. I imagine this is the same for the files in Joomla/components/COMPONENT_NAME/view. I am trying to see if they can be loaded manually by using JDocument or JFactory but so far to no avail...
Regards
Andrew
Website Design, Content Management, Search Engine Optimisation, Graphic Design, Flash Authoring - all in a day's work!
Footprint Media - Professional Web Services - http://www.footprintmedia.net
Footprint Media - Professional Web Services - http://www.footprintmedia.net
- drewhemm
- Joomla! Apprentice
- Posts: 37
- Joined: Wed Jan 24, 2007 11:34 pm
- Location: Spain
- Contact:
Re: Accessing template parameters in component's tmpl
Ok, I have found a workaround that can be implemented at the template level. I have created a file within my template directory called params.php - these are the contents:
Then whereever you want to grab the template parameters, you can use the following code:
In your case Michal, I would create an override file within my template directory and insert this code there so that it would not be lost when Joomla is upgraded.
This appends a JParameter object to whatever $this currently is so that you can grab the value later:
Regards
Andrew
Code: Select all
<?php defined('_JEXEC') or die('Restricted access');
/**
* @version 1.0
* @package Joomla.Framework
* @subpackage Parameter
* @copyright Copyright (C) 2010 Footprint Media. All rights reserved.
* @license GNU/GPL
* @author Andrew Hemming
* @link http://www.footprintmedia.net/
* @uses This class appends a JParameter object containing the template parameters to the object passed to the TemplateParams function
*/
class TemplateParams
{
/**
* TemplateParams
* @param $object - where to append the template parameters
*/
function TemplateParams($object) {
// find out the current template - could use a static references as this file will only have effect on the template where it resides
$app =& JFactory::getApplication();
$ini = file_get_contents(JPATH_THEMES . DS . $app->getTemplate() . DS . 'params.ini');
// Get each of the lines in the ini file
$templateParams = preg_split('#[\r\n]#', $ini);
// Create a new JParameter object
$object->templateParams = new JParameter('');
// Loop through the lines, setting each of the values as a parameter
foreach ($templateParams as $param) {
// Some of the imported lines will be empty, this filters them out
if (strlen($param) > 0) {
$array = preg_split('#=#', $param);
$object->templateParams->set($array[0], $array[1]);
}
}
}
}
Code: Select all
$app =& JFactory::getApplication();
JLoader::register( 'TemplateParams', JPATH_THEMES . DS . $app->getTemplate() . DS . 'params.php');
$templateParams = new TemplateParams($this);
This appends a JParameter object to whatever $this currently is so that you can grab the value later:
Code: Select all
$thumbnail_width = $this->templateParams->get('topstory_thumbnail_width');
Andrew
Website Design, Content Management, Search Engine Optimisation, Graphic Design, Flash Authoring - all in a day's work!
Footprint Media - Professional Web Services - http://www.footprintmedia.net
Footprint Media - Professional Web Services - http://www.footprintmedia.net
-
- Joomla! Fledgling
- Posts: 4
- Joined: Sun Jul 20, 2008 2:03 am
Re: Accessing template parameters in component's tmpl
Hi Andrew,
Yes, I've found a working solution for this but I'm not convinced that it’s the ‘right’ one. What I’ve done is I’ve created class in file \templates\template_name\read_tpl_params.php
And if I need to get parameter I do:
and
Regards
Michal
Yes, I've found a working solution for this but I'm not convinced that it’s the ‘right’ one. What I’ve done is I’ve created class in file \templates\template_name\read_tpl_params.php
Code: Select all
<?php defined('_JEXEC') or die('Restricted access');
class JAddons{
static public function getTplParams(){
$app =& JFactory::getApplication();
$cont = null;
$ini = JPATH_THEMES.DS.$app->getTemplate().DS.'params.ini';
$xml = JPATH_THEMES.DS.$app->getTemplate().DS.'templateDetails.xml';
jimport('joomla.filesystem.file');
if (JFile::exists($ini)) {
$cont = JFile::read($ini);
} else {
$cont = null;
}
return new JParameter($cont, $xml, $app->getTemplate());
}
}
?>
And if I need to get parameter I do:
Code: Select all
$app =& JFactory::getApplication();
JLoader::register('JAddons', JPATH_THEMES.DS.$app->getTemplate().DS.'read_tpl_params.php');
Code: Select all
$some_param_val = JAddons::getTplParams()->get('some_param_name');
Michal
- drewhemm
- Joomla! Apprentice
- Posts: 37
- Joined: Wed Jan 24, 2007 11:34 pm
- Location: Spain
- Contact:
Re: Accessing template parameters in component's tmpl
Nice! I've updated my class to use the JFile class, which is much more elegant than using file_get_contents()...
Website Design, Content Management, Search Engine Optimisation, Graphic Design, Flash Authoring - all in a day's work!
Footprint Media - Professional Web Services - http://www.footprintmedia.net
Footprint Media - Professional Web Services - http://www.footprintmedia.net
- toivo
- Joomla! Master
- Posts: 17426
- Joined: Thu Feb 15, 2007 5:48 am
- Location: Sydney, Australia
Re: Accessing template parameters in component's tmpl
This is an old topic but this more recent thread how to access template parameters from modules should help if anyone is searching for a solution for components:
http://forum.joomla.org/viewtopic.php?f=706&t=883134
http://forum.joomla.org/viewtopic.php?f=706&t=883134
Toivo Talikka, Global Moderator
- mamboline
- Joomla! Enthusiast
- Posts: 104
- Joined: Wed Oct 05, 2005 12:42 pm
- Location: Beograd, Serbia
- Contact:
Re: Accessing template parameters in component's tmpl
I found a way to get template parameters into the component, module or plugin from Joomla database:
You will get a JSON string in $template_parameters that you can decode into an array:
Now, you can access any parameter from this array, in example:
Regards,
Milos
Code: Select all
$template_name = "my_template";
// get template options from DB
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('params'));
$query->from($db->quoteName('#__template_styles'));
$query->where($db->quoteName('template') . ' LIKE '. $db->quote($template_name));
$db->setQuery($query);
$template_parameters = $db->loadResult();
Code: Select all
// decode JSON string into array
$template_parameters_array = json_decode($template_parameters, true);
Code: Select all
echo $template_parameters_array['templateColor'];
Milos
https://www.hotjoomlatemplates.com - Joomla templates