Accessing template parameters in component's tmpl

Everything to do with Joomla! 1.5 templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
mbiker
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jul 20, 2008 2:03 am

Accessing template parameters in component's tmpl

Postby mbiker » Wed May 19, 2010 10:38 am

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

User avatar
drewhemm
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Jan 24, 2007 11:34 pm
Location: Spain
Contact:

Re: Accessing template parameters in component's tmpl

Postby drewhemm » Tue Jun 08, 2010 7:39 am

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
Website Design, Content Management, Search Engine Optimisation, Graphic Design, Flash Authoring - all in a day's work! :D
Footprint Media - Professional Web Services - http://www.footprintmedia.net

User avatar
drewhemm
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Jan 24, 2007 11:34 pm
Location: Spain
Contact:

Re: Accessing template parameters in component's tmpl

Postby drewhemm » Tue Jun 08, 2010 9:15 am

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:

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]);
         } 
      }
   }
}

Then whereever you want to grab the template parameters, you can use the following code:

Code: Select all

$app =& JFactory::getApplication();
JLoader::register( 'TemplateParams', JPATH_THEMES . DS . $app->getTemplate() . DS . 'params.php');
$templateParams = new TemplateParams($this);

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:

Code: Select all

$thumbnail_width = $this->templateParams->get('topstory_thumbnail_width');

Regards

Andrew
Website Design, Content Management, Search Engine Optimisation, Graphic Design, Flash Authoring - all in a day's work! :D
Footprint Media - Professional Web Services - http://www.footprintmedia.net

mbiker
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jul 20, 2008 2:03 am

Re: Accessing template parameters in component's tmpl

Postby mbiker » Tue Jun 08, 2010 10:57 am

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

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');

and

Code: Select all

$some_param_val  = JAddons::getTplParams()->get('some_param_name');


Regards
Michal

User avatar
drewhemm
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Jan 24, 2007 11:34 pm
Location: Spain
Contact:

Re: Accessing template parameters in component's tmpl

Postby drewhemm » Tue Jun 08, 2010 11:24 am

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! :D
Footprint Media - Professional Web Services - http://www.footprintmedia.net

User avatar
toivo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8662
Joined: Thu Feb 15, 2007 5:48 am
Location: Nottingham, UK
Contact:

Re: Accessing template parameters in component's tmpl

Postby toivo » Mon Apr 20, 2015 1:10 pm

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:
viewtopic.php?f=706&t=883134
Toivo Talikka, Global Moderator
my first programs were assembled and run in 16KB :)
http://archive.computerhistory.org/resources/text/GE/GE.GE-115SystemSoftware.1967.102646096.pdf#zoom=100

User avatar
mamboline
Joomla! Intern
Joomla! Intern
Posts: 62
Joined: Wed Oct 05, 2005 12:42 pm
Location: Beograd, Serbia
Contact:

Re: Accessing template parameters in component's tmpl

Postby mamboline » Thu Mar 31, 2016 2:23 pm

I found a way to get template parameters into the component, module or plugin from Joomla database:

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();


You will get a JSON string in $template_parameters that you can decode into an array:

Code: Select all

// decode JSON string into array
$template_parameters_array = json_decode($template_parameters, true);


Now, you can access any parameter from this array, in example:

Code: Select all

echo $template_parameters_array['templateColor'];


Regards,
Milos
http://www.hotjoomlatemplates.com - Joomla templates
http://www.designdestiny.net - Design and Joomla development


Return to “Templates for Joomla! 1.5”

Who is online

Users browsing this forum: No registered users and 12 guests