Keyword

Different Joomla template for category and item views?

  • PhoenixOmega
  • PhoenixOmega's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago - 9 years 6 months ago #142267 by PhoenixOmega
I am using T3 to create completely custom templates for every area of my site. I need to have a different Joomla template (not K2 template) assigned to the category view and item views, or find a way to alter the URL structure somehow. The reason for this is that I have a completely different markup I need for the category view,,with different JS files loading in the HTML head, a different navigation structure, different page structure (no sidebar with modules on the category view pages, for example), different modules, etc

I have K2 items in two main categories: science, and science-fiction. I have a menu item with the alias "science" that goes to the "science" k2 item category. So all the K2 item URLs under the category of "science" have URLs like "science/item/8445-new-earth-like-planet-found" and the K2 item URLs under the category of "science-fiction" have URLs like "science-fiction/item/8445-new-earth-like-planet-found". The item pages have a different menu at the top than the category pages and have a sidebar with modules on the right (which the category pages do not).

The category views are at the URLs "science" and "science-fiction" and I'm forced to have those URLs point to the K2 category views for Science and Science-Fiction. Or am I? Is there a way around that? I can't seem to be able to assign anything to have a URL of "science" or "science-fiction" without disrupting the URLs of the K2 items like "science/item/8445-new-earth-like-planet-found". The category views have a different site navigation menu than the item views, and there's no sidebar on the right displaying modules.

So far the only way I have found to do this is to stuff the code for multiple templates into one PHP template file. I basically have some code that checks if the URL is "science", and if so displays one set of a few hundred lines of code... if the URL is "science-fiction", it displays a different set of a few hundred lines of code... if it's neither, then it must be a K2 item, so it displays yet a different set of hundreds of lines of code. This approach works but is incredibly messy and hard to manage. It would be so nice to put the code into separate templates, but I cannot figure out how!

Another approach is obviously to just put a redirect in .htaccess (or in the K2 template's category.php file) to have the URL "/science" go to a different URL where I'm using a different template, but then the problem is that after the redirect, it's still showing a different URL than "/science"

Maybe the shSEF404 could help accomplish what I need by changing around the URLs, but that extension is a nightmare to manage and is way overkill for this one task.

Please note that I am a developer and am completely comfortable doing any coding of my own that's needed, but I'd like to know what the best "Joomla/K2 friendly" way of accomplishing this is.
Last edit: 9 years 6 months ago by PhoenixOmega.

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #142303 by Krikor Boghossian
Replied by Krikor Boghossian on topic Different Joomla template for category and item views?
Hello Phoenix.

I would approach this project a bit different (same method different approach).

I prefer to work in a single index.php file this is something personal
Now in my main template file I would create two cases.

a) If the option is K2 and the view is the item
JRequest::getCmd('option') ==  'com_k2' && JRequest::getCmd('view') == 'item'

I would 'require' another php file which loads different module positions and scripts.

b) Else continue with the default template.

This method can be used i helper file to keep your code cleaner and load conditionally other dependencies. It can also be extended to load different php/ js/ css / modules (you get the picture) depending on the ID of either the menu item or the K2 item.
Pretty much you can built multiple subtemplates and maintain clean code without using multiple templates.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • PhoenixOmega
  • PhoenixOmega's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago - 9 years 6 months ago #142419 by PhoenixOmega

Krikor wrote: Hello Phoenix.

I would approach this project a bit different (same method different approach).

I prefer to work in a single index.php file this is something personal
Now in my main template file I would create two cases.

a) If the option is K2 and the view is the item

JRequest::getCmd('option') ==  'com_k2' && JRequest::getCmd('view') == 'item'

I would 'require' another php file which loads different module positions and scripts.

b) Else continue with the default template.

This method can be used i helper file to keep your code cleaner and load conditionally other dependencies. It can also be extended to load different php/ js/ css / modules (you get the picture) depending on the ID of either the menu item or the K2 item.
Pretty much you can built multiple subtemplates and maintain clean code without using multiple templates.


Hi Krikor,

Thanks for the reply!!

Your solution works very well, however I had to tweak it a bit for my particular situation. In the T3 layout file (which is basically the main index.php file), I did this:
if($_SERVER['REQUEST_URI'] == '/science' || $_SERVER['REQUEST_URI'] == '/science-fiction')
    $this->loadBlock ('category-view');
else
    $this->loadBlock ('item-view');

I then created two new blocks, and put in the item view template in the item-view.php block, and put the category view template in the category-view.php block.

I couldn't get JRequest::getCmd to work properly, maybe because of how my URLs are structured, so had to resort to getting the $_SERVER value. Oh well. It works perfectly.

Thanks for helping to support K2, quite possibly the most useful extension for Joomla!
Last edit: 9 years 6 months ago by PhoenixOmega.

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #142428 by Krikor Boghossian
Replied by Krikor Boghossian on topic Different Joomla template for category and item views?
You 're welcome Phoenix :)

One last thing, I am not an expert of T3 but I think they might have variables for the JRequest::getCmd commands, eg $option, $itemid etc.
This might be helpful in a future project.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • PhoenixOmega
  • PhoenixOmega's Avatar Topic Author
  • Offline
  • New Member
More
9 years 6 months ago - 9 years 6 months ago #142470 by PhoenixOmega

Krikor wrote: You 're welcome Phoenix :)

One last thing, I am not an expert of T3 but I think they might have variables for the JRequest::getCmd commands, eg $option, $itemid etc.
This might be helpful in a future project.


Thanks again. I think I found a more "Joomla proper" way of doing it. According to Joomla's documentation, JRequest is kind of deprecated, and you should use JInput now.

docs.joomla.org/Retrieving_request_data_using_JInput

Here's my new code, which is basically what you had, but it seems to work for me.
$jinput = JFactory::getApplication()->input;

//if in K2 itemview, load the item view template. Otherwise, load the category view.
if($jinput->get('option') == 'com_k2' && $jinput->get('view') == 'item')
    $this->loadBlock ('item-view');
elseif($jinput->get('option') == 'com_k2' && $jinput->get('view') == 'itemlist')
    $this->loadBlock ('category-view');
else
    $this->loadBlock ('error');
Last edit: 9 years 6 months ago by PhoenixOmega.

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 6 months ago #142507 by Krikor Boghossian
Replied by Krikor Boghossian on topic Different Joomla template for category and item views?
Thank for sharing your code Phoenix :)
I am sure other users will benefit as well.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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


Powered by Kunena Forum