JoomlaWorks

Member Dashboard
  • Home
  • Extensions
    • Commercial
      • Frontpage Slideshow
      • K2 Plugin for sh404SEF
      • Simple Image Gallery Pro
      • SocialConnect
    • Free
      • K2
      • AllVideos
      • DISQUS Comments for Joomla!
      • IAKI (Import As K2 Image)
      • Quick Menu (for Joomla 4)
      • Simple Image Gallery
      • Simple RSS Feed Reader
    • Free On Github
      • Akismet for Kunena
      • Fill It Up
      • K2 Example Plugin
      • K2 Links for JCE
      • Rebuild K2 Image Cache (CLI utility for K2)
      • TinyLetter Subscribe
      • URL Normalizer
      • User Extended Fields for K2
  • Templates
    • Commercial Templates
      • Anagram
      • Archetype
      • BusinessOne
      • Ibento
      • Janaro
      • Kiji
      • Matchbox
      • nuMuzik
      • RadioWave
      • Toreda
    • Free Templates
      • nuModusVersus
      • Takai
  • Demos
    • Joomla Extension Demos
    • Joomla Template Demos
    • WordPress Plugin Demos
  • Labs
    • Web Apps
      • JSON 2 JSONP
      • Feed Reader (bookmarklet)
      • Device Info
  • Support
    • Documentation for Joomla Extensions
      • AllVideos
      • Frontpage Slideshow
      • Simple Image Gallery
      • Simple Image Gallery Pro
      • SocialConnect
    • Documentation for Joomla Templates
      • General Resources
        • Installation
        • Content
        • Customization
      • Commercial Templates
      • Free Templates
    • Get Help
      • Community Forum
        • Recent Topics
        • Categories
        • Create free account
      • Help Desk
      • Contact us by e-mail
      • Contact us on Facebook Messenger
      • Contact us on Twitter
  • About
    • Blog
    • Company
    • License & Terms of Service
    • Privacy Policy
  • My Account
27 11 2013
Written by  JoomlaWorks
Published in Blog
1 comment

K2 inheritance & sub-templating

K2 inheritance & sub-templating

K2 is the most popular content management extension for Joomla. Together they provide a friendly and powerful interface that works great for both experts and beginners who want to customise their website with minimum effort.

In this post we'll take a deeper look in the flexibility provided by K2 when it comes to customisation, either through its multiple sub-template capabilities or through the inheritance options provided in its parameters.

 

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:

image1

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:

image2

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:

image3

K2 Inheritance

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.

case1 structure

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.

case1-inherit2

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.

case1-afterInherit

Case 2: Category structure with all categories in the same level that share the same parameters.

case2-structure

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

case2-inherit

and move on to the rest. Our resulting list will be like the following:

case2-afterInherit

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.

menuInheritOrdering2

If multiple categories are selected then the parameters become editable and you can set them through this menu item.

menuOverrideParameters2

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/.

Read 38280 times Last modified on Wednesday, 28 January 2015 18:23

1 comment

  • Scot Giambalvo Scot Giambalvo 16 Mar 2015
    Comment Link

    I think this needs to be updated with screenshots for Joomla 3.4 and later now. There is NO templates folder in my com_k2 folder on my fresh install. :-(

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

back to top
BY MAIL BY RSS

Archives

  • March 2023 (1)
  • January 2022 (1)
  • July 2021 (2)
  • May 2021 (1)
  • February 2021 (1)
  • December 2020 (1)
  • September 2020 (1)
  • June 2020 (1)
  • May 2020 (1)
  • February 2020 (1)
  • January 2020 (5)
  • December 2019 (2)

Tag Cloud

allvideos announcement apache community development frontpage slideshow joomla k2 Performance plugin release sh404sef simple image gallery simple image gallery pro simple rss feed reader socialconnect ssh tips update video

Latest Entries

  • New free extension release: Quick Menu (for Joomla 4)
    Written by  JoomlaWorks
    06 Mar 2023
  • Simple Image Gallery (free) v4.2 released
    Written by  JoomlaWorks
    07 Jan 2022
  • Simple Image Gallery Pro v3.9.1 released (bug-fix release)
    Written by  JoomlaWorks
    26 Jul 2021
  • Simple Image Gallery Pro v3.9.0 released
    Written by  JoomlaWorks
    05 Jul 2021
  • Simple RSS Feed Reader v3.9.0 released
    Written by  JoomlaWorks
    25 May 2021

Join Our Newsletter

Enter your e-mail address to subscribe to our new or updated product notifications. We send combined newsletters, so we won't spam you every week :)

Get Social

  • Twitter
  • GitHub
  • Facebook
  • GitHub (K2)
  • YouTube
  • Speaker Deck

Grab Our Feeds

  • Extension Updates
  • Blog
  • Videos
  • Presentations
Copyright © 2006 - 2023 JoomlaWorks Ltd.
JoomlaWorks Ltd. and this site is not affiliated with or endorsed by The Joomla! Project™. Any products and services provided through this site are not supported or warrantied by The Joomla! Project or Open Source Matters, Inc. Use of the Joomla!® name, symbol, logo and related trademarks is permitted under a limited license granted by Open Source Matters, Inc.
designed by Nuevvo