amilaishere
amilaishere

Reputation: 408

Set template programmatically joomla view

Is there any way to set template programmatically inside joomla view ?

I'm using MVC architecture to develop my component. I have two templates for front end side. 1 for logged in users (template B) other for ordinary visitors (Template A).

The problem that I have encountered is, I have a book store for logged in users. template is Template B. inside book store there is function for search book. Both search and book store load from my component. Book store assigned to a menu item and load with template B. But when I clicked on search button it brings the results with Template A. which means template changed to template A from template B.

My site's default template is template B. I assigned template A via menu items.

I'm using Joomla 2.5

What happened?

Upvotes: 3

Views: 1603

Answers (4)

user21422744
user21422744

Reputation:

I was using successfully $app = JFactory::getApplication(); $app->setTemplate('TEMPLATE_NAME'); in Joomla 3.x to switch site templates via onAfterInitialise() system plugin event.

Now I'm updating a plugin to make it compatible with Joomla 4 and it doesn't work well anymore. It switches the template partially; for example, if I try to switch from another template to Cassiopeia template, I see Cassiopeia, but without the main css file. So I see the component, but there's no style of the template. With third-party templates (such as Helix Ultimate) the situation is even worse. I tried on Joomla 4.2.0, 4.2.5 and just now on the newer 4.2.9.

So, in reply to SkipIntro on Joomla ‎4.1.4‎ did you correctly manage to switch to another template programmatically via $app->setTemplate('TEMPLATE_NAME');? If yes, until with version of Joomla 4 are you sure that this code works? I would like to resume here until which version we can use $app->setTemplate('TEMPLATE_NAME'); and, if possible, to find a workaround for the versions of Joomla 4 where this code doesn't work.

Upvotes: 0

SkipIntro
SkipIntro

Reputation: 124

There are problems in Joomla 4 in setTemplate() method. It is implemented for SiteApplication class (which instance is accessible with Factory::getApplication() for further template setting from SITE view), but it is not for AdministratorApplication. So, it is harder to set template programmatically for admin`s views.

After some tweaking I was able to achieve this by next lines (use it in HtmlView.php):

$app = Factory::getApplication();
$app->set('theme', 'any-theme-name');
$app->set('themeParams', 'any-params-for-theme');

You can even set site`s (frontend) templates for admin`s views without duplicating it in administrator`s template folder - just add this

$app->set('themes.base', JPATH_SITE . DIRECTORY_SEPARATOR . 'templates');

Works fine in Joomla ‎4.1.4‎.

For Site section of application this https://stackoverflow.com/a/30754502/3486976 method should work still, so there is not need in hacking above.

Upvotes: 0

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13908

I had a similar problem where, I was using two templates for registered users and for guests respectively. I wanted a view to be loaded only inside a specific template, so I had to programatically set/change the template inside the view, I tried using Itemid but it was just unreliable, finally after a bit of research I got that working as follows..

  $app = JFactory::getApplication();
  $app->setTemplate('Protostar'); //or whatever your template is named.

With this, I could be sure that this view/layout loaded only in a specific template. I have used this with success on , though it might work on your version.

Upvotes: 2

Umut KIRGÖZ
Umut KIRGÖZ

Reputation: 2113

I think, this can be done by using JSite::setTemplate($template,$templateParams) method, but I am not sure this will work with caching, need to test.

Upvotes: 1

Related Questions