How to set a template programmatically in a component

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
roba121
Joomla! Explorer
Joomla! Explorer
Posts: 465
Joined: Tue Jun 13, 2006 12:20 pm

How to set a template programmatically in a component

Post by roba121 » Sun Aug 31, 2008 10:07 pm

So I did some searching in the forum and just couldn't find anything....I suppose there is still a way to do this without hacking the core but couldn't find it.

So I have come up with the following code...before I put it out though I think the reason for setting a template programatically should be made clear...There are often times where a component needs a different layout then the rest of the site...It would be nice for a programmer to be able to set what template that component is rendered in...It can be abstracted out to the user through the config system or through a custom config for the component. Either way it would be very useful.

So to add this into your own site.

In libraries/joomla/document/html/html.php

add the two following functions

Code: Select all

function getTemplate($cur){
    if($this->template){
        return $this->template;
     }else{
        return $cur;
     }
  } 
  
  function setTemplate($tmpl){
  
       return $this->template = $tmpl;
  }
Then around line 215 in the render function change

Code: Select all

	$template	= JFilterInput::clean($params['template'], 'cmd');
To:

Code: Select all

	$template	= JFilterInput::clean($this->getTemplate($params['template']), 'cmd');
This will let you set the template in your component and if its not provided then it just goes to whats set in the system.

API:

In your component you can do the following to set the template

Code: Select all

$doc=&JFactory::getDocument();
$doc->setTemplate("my_tempalte_name");
Hope that helps someone, if anyone knows a way to do this without hacking the core TELL ME!

I'd much rather not hack the core but this was a feature I needed....so there you go.

Thanks

Rob

Jason_Lester
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Oct 10, 2008 9:51 pm

Re: How to set a template programmatically in a component

Post by Jason_Lester » Tue Oct 21, 2008 5:23 pm

I agree, Joomla needs a better way to set the template within component code... and if one already exists it should be better documented. But for now, if you wish to do so without hacking the core you can just use

Code: Select all

$GLOBALS["mainframe"]->setTemplate = "new_template_name";

Jason_Lester
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Oct 10, 2008 9:51 pm

Re: How to set a template programmatically in a component

Post by Jason_Lester » Tue Oct 21, 2008 5:27 pm

or

Code: Select all


function some_method_within_component()
{
	global $mainframe;

	$mainframe->setTemplate = "new_template_name";

	// more code ...
}


User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: How to set a template programmatically in a component

Post by ianmac » Tue Oct 21, 2008 6:03 pm

Jason_Lester wrote:or

Code: Select all


function some_method_within_component()
{
	global $mainframe;

	$mainframe->setTemplate = "new_template_name";

	// more code ...
}

hmmm... this seems counterintuitive to me, because I would think to make a more flexible component you wouldn't want to require a certain template. Why don't you allow site administrators to choose the template they want for the page of your component?

Ian

Jason_Lester
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Oct 10, 2008 9:51 pm

Re: How to set a template programmatically in a component

Post by Jason_Lester » Tue Oct 21, 2008 6:36 pm

Hi Ian. I'm not sure I understand your question...

If your asking why a developer would want to change the template within a component, my answer is that it depends on how the component needs to be developed, plus on how much responsibility you want to give to those managing the site that the component is being developed for. For example, I'm making a calendar component and all calendar pages have the same template which is different from the default. The menu module builds the menu dynamically (not using the joomla menu module). I don't have the ability to set the templates on a per page basis using the template manager since the calendar doesn't use the main menu module... plus the client is in the real estate business and knows little about CMS administration, so we would rather not put the task in their hands anyway. It's far easier this way then to have to teach them how to create event entries within the calendar manager, create links for the entries within the menu manager, and then assign a template within the template manager. I have ran into a few situations where it was easier to manage templates on a per component basis, or even a per category/section basis within a component, rather then a per page basis.

roba121
Joomla! Explorer
Joomla! Explorer
Posts: 465
Joined: Tue Jun 13, 2006 12:20 pm

Re: How to set a template programmatically in a component

Post by roba121 » Tue Oct 21, 2008 7:36 pm

Well I have two comments, first ian. Take this a step further and you would provide a method for selecting a template for your component in the admin. In my case I needed a completely open layout with just a menu across the top to accomodate the space my component needed. Think of a case where you distribute a component and a full template for it.

Second, Jason thanks...this helps.

Third I think this is something that should explicitly be part of JDocument but whatever Im glad it works.

Thanks All

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: How to set a template programmatically in a component

Post by ianmac » Tue Oct 21, 2008 7:43 pm

roba121 wrote:Well I have two comments, first ian. Take this a step further and you would provide a method for selecting a template for your component in the admin.
This can be done by assigning a template to the menu item for your component.
In my case I needed a completely open layout with just a menu across the top to accomodate the space my component needed. Think of a case where you distribute a component and a full template for it.
Yes, and that full template can be assigned to your component using the method I described above. I find it hard to imagine though that the template you provide would mesh with the design of many sites. Could be wrong though.

Hope that helps,
Ian

Jason_Lester
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Oct 10, 2008 9:51 pm

Re: How to set a template programmatically in a component

Post by Jason_Lester » Tue Oct 21, 2008 8:37 pm

@ Ian
Joomla shouldn't be as dependent on its menu system as it is. I'm not saying that the menu isn't awesome for what it does, but Joomla would be better as a platform/framework if much of the functionality within the menu system was available within the API. It likely is, but with all the digging that I have done I still haven't found it. I also think that the title, meta data, template, etc is beyond the scope of a menu. I think it makes more since to configure these things from within the component itself, such as when creating a page within the article system. They need to at least have methods for making/editing menu entries available within the component. This still leads to a bigger question... why is this all tied to a menu?

Sorry... not tying to flame

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: How to set a template programmatically in a component

Post by ianmac » Wed Oct 22, 2008 5:12 am

Jason_Lester wrote:@ Ian
Joomla shouldn't be as dependent on its menu system as it is. I'm not saying that the menu isn't awesome for what it does, but Joomla would be better as a platform/framework if much of the functionality within the menu system was available within the API. It likely is, but with all the digging that I have done I still haven't found it. I also think that the title, meta data, template, etc is beyond the scope of a menu. I think it makes more since to configure these things from within the component itself, such as when creating a page within the article system. They need to at least have methods for making/editing menu entries available within the component. This still leads to a bigger question... why is this all tied to a menu?

Sorry... not tying to flame
Well, that is a good question. I don't know if there is an easy answer to that, except to say that with every system design choices are made to solve the problem of how to put together a page. With the menu system we have we gain a lot of flexibility but at the same time we lose some flexibility. I'm not sure what to say beyond that except to get involved and present your white paper when the call comes out for 1.7 or join the bug squad and work your way into the development working group. There are lots of ways to influence the project.

Perhaps it is a matter of paradigm. In Joomla!, each menu item represents in some ways, a page. So, the system is designed to allow you to specify what goes on a page and how everything gets displayed. Besides, if components really do want to have full control, they can easily do that by adding tmpl=component onto the query string or by refreshing the JDocument object to use tmpl=component.

Also, the title is indeed, in most cases, set by the component itself. A few releases ago of 1.5 we did a full sweep of every frontend component and made the way that page titles are consistent. This is done, in every case, in the component. The menu has the option of providing a value, but the component is ALWAYS able to override this. Components are also free to set which ever meta data they want as well. There are JDocument methods available to add all sorts of custom head tags to the head block of the document.

This sounds like it could be an interesting discussion and I always welcome fresh ideas. Feel free to continue here.

Ian

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: How to set a template programmatically in a component

Post by ianmac » Wed Oct 22, 2008 5:13 am

And I guess the one thing I would add is that if you give ALL power to the component, then there is very little control that administrators have in customizing the look of their sites. I generally want to specify what the page looks like, and not be forced into a certain look or feel by a component. All a matter of perspective, I guess, and design decisions.

Ian

User avatar
gopijack
Joomla! Intern
Joomla! Intern
Posts: 79
Joined: Wed Oct 20, 2010 7:32 am

Re: How to set a template programmatically in a component

Post by gopijack » Tue Nov 15, 2011 8:36 am

Hi,

I got solution but this only temporary solutions.

because,every templates are loading from includes/application.php in
getTemplate($params = false).

Using this function only every templates loading,In this function it
will get each every current page template id and template name.
If we use setTemplate() function it won't work any more,because in
getTemplate($params = false) function template id and template name.
load directly from the database so it will set as per the database
requirements.

so,i thought to hack this getTemplate($params = false).
i added one function like this

function fetch_template($extn){ //$extn contains name of the
component (3rd party extension component)

if($extn!=""){
$db = &JFactory::getDBO();
$select = "select * from #__menu where menutype='mainmenu'";
$db->setQuery($select); $result = $db->loadObjectlist();
foreach($result as $result){
if (preg_match("/\b$extn\b/i", "$result->link")) {
if($result->template_style_id!=0){
$select = "select * from #__menu where menutype='main'";
$db->setQuery($select); $result_ideal = $db-
>loadObjectlist();
foreach($result_ideal as $result_ideal){
$id = $result_ideal->id;
}
}
}
}
}
}return $result_ideal->id;

}


In getTemplate():

I add $com = JRequest::getVar('option');
if($com == "com_abcde"){
$post= JRequest::get('post');
$extn = $post['extn']; //extn is a variable i passes in post.

$id = $this->fetch_template($extn);//$extn you have post the 3rd
party extension and also change the template ID

//using this $id,you can fetch the template name from the
templat_style table

if (isset($templates[$id])) {
$template = $templates[$id];
}
else {
$template = $templates[0];
}

// Allows for overriding the active template from the request
$template->template = JRequest::getCmd('template', $template-
>template);
$template->template = JFilterInput::getInstance()->clean($template-
>template, 'cmd'); // need to filter the default value as well

// Fallback template
if (!file_exists(JPATH_THEMES . '/' . $template->template . '/
index.php')) {
JError::raiseWarning(0, JText::_('JERROR_ALERTNOTEMPLATE'));
$template->template = 'beez_20';
if (!file_exists(JPATH_THEMES . '/beez_20/index.php')) {
$template->template = '';
}
}
}

that's it !

I got it form above hack,now i can set each template for a each
component (Not only the main menu). for Joomla1.7
Thanks & Regards
Gopi.A


Locked

Return to “Joomla! 1.5 Coding”