Customizing K2 using sub-templates
Let's take one thing at a time and start with the use of sub-templates and how they work.
K2, as most Joomla extensions nowadays, uses the MVC design pattern. This means that you can override (copy) the default content of the tmpl or template folder under the template's folder. In the screenshot below you can see the component's folder structure:
All you need to do is copy the contents of the template folder (as shown in the image above) and paste it under templates/YOUR_TEMPLATE/html/com_k2/ folder. That way you can make any modification on your project without tampering with the component's core files. The 'default' folder is the one that you can theme into variations unlike the files that are not included in this folder, i.e. generic.php, profile.php, register.php, etc.
You can create multiple copies of the 'default' folder each one representing a new variation, and you can select each of them through the K2 category parameter or the menu's parameter. One example of that would be if you had one Blog style page and one Catalog page. The Blog page would be a list of articles and the catalog page would most likely be a grid of products. In this case your blog category should have the 'default' template assigned to it and the catalog category should have the 'catalog' sub-template.
You can also decide which elements your sub-templates have in common, whether this is the category (category.php), tag (tag.php), user (user.php) or item page (item.php) with the default and include only the different file(s) in your sub-template.
We will explain that better by using an example.
Let's assume that you have a project with two categories, News and Blog. You want a different item view for each one of these categories. The page layout is completely different and you have to use a new sub-template. All other page layouts are the same though. How should you approach this?
Your first move would be to create the override of the component's default template, following the procedure you already know, and assign it through the News category parameters. Then you would create another copy of the default template with a different name, let's say blog, to cover the second case where the item.php is different. Now you have to assign this template (blog) to the Blog category.
Your file/folder structure is as follows:
So far all is set and you can start modifying to reach the desired result.
In the process you might ask yourself though
"Why do I have to maintain all files in both template folders since the only one that actually changes is the item.php? Wouldn't it be better if I could just keep only the different item.php in my sub-template and all other common files were fetched through the 'default' template?"
Fortunately for you, the K2 team has already thought of that and you can keep in your sub-template folder only the files that contain the modifications represented to the page layouts. All common structured files will be fetched through the 'default' template and the result would be the same without keeping any duplicate files.
Your files/folder structure would be:
In Joomla, to set up your category options and item options specifically by category you would have to add them to every single menu item you create. With K2 you don't have to search and add parameters every time you want to add or change a view (eg. category). These parameters are located in the categories parameters and, in some cases, can be also overridden through the menu item for more flexibility.
If you already use K2 you might have noticed the category option called "Inherit parameter options from category" in the category's parameters. This option has proven to be very useful since you don't have to set the same parameters to all your categories one by one. Instead, you can set up one category's parameters and assign all others to inherit their parameters from the first one.
Let's examine some category structure cases to understand how this works.
Case 1: Category structure with one parent category and children categories.
In this case we have set up our children categories to inherit their parameters from their Parent category, Blog.
We will start with the first child category and select from the option "Inherit parameter options from category" their parent, Blog, as shown below.
You have to do this for every child category that needs to have common parameters. To help you easily refer to each category's inheritance, without having to edit the category itself, you can check the "Inherits parameters from" column. This filter is very useful especially when we have a large number of categories which inherit their parameters from multiple categories.
Case 2: Category structure with all categories in the same level that share the same parameters.
In this case we will demonstrate a way to use dummy categories (with no items assigned to them) which will be only used for the category setup.
We have created a category called "SETUP Category Blog". We will set our categories to inherit their parameters from this category.
Let's start from the first category
and move on to the rest. Our resulting list will be like the following:
You can choose which of these two cases suits your project best and apply them.
K2 Inheritance through the menu
Another place you can set up category parameters and inheritances is the menu item of the K2 Category.
If you create a K2 Category menu item and select a category through the category selector, you could override only its Ordering options. The parameters are not editable and are inherited from the category's parameters.
If multiple categories are selected then the parameters become editable and you can set them through this menu item.
Sub-templates and inheritance are two of the most powerful features of K2 when it comes to creating multiple and complex layouts under the same roof. Each feature requires only a few steps to set up and make use of, provided that you grasp the basic concepts regarding the functionality of K2 and Joomla This blog post is the first of many that might shed some light to various points that remain vague up to now about K2's features and functionality. Stay tuned!
Note: For those who are now getting started with K2, visit http://getk2.org/.