The power of custom K2 templates

Joomla as a content management system offers a remarkably easy way to manage articles and categories, but you may occasionally wanting something a little bit different, like a custom look different to your regular pages. This is especially true in a larger, more complex website, where single article or category views may not be sufficient enough for the diverse nature of your content. In such cases, you’ll want to create a custom view to make your content even more unique. There are plenty of different ways to do this – use custom CSS, overwrite some template sections, or create a custom template using K2. The latter in particular is probably the easiest option there is, and it allows you to create diverse, original views that are dedicated to just a few of your custom pages.

Custom K2 template examples

There’s no doubt that K2 is one of the most powerful content Joomla extensions. It extends the regular Joomla article manager features with an advanced layout, tags (especially important in Joomla 2.5.x), author information, built-in comments, social buttons and much more. It’s pretty much an essential extension at this point, which is why we include support for it in all our themes. But there’s another feature that’s really useful if your diversifying your content; K2 has a built-in template system which allows you to create different views and assign them to a particular menu item.

See what custom K2 templates can do

In one of our latest templates (Events, for the curious among you!) we’ve created three dedicated K2 templates which shows how this feature may be used to built attractive pages. With a non-K2 solution the only real way to achieve an original look is to just use custom HTML, but why confine yourself to what amounts to static HTML pages in a CMS, where content may be dynamic? Custom CSS may be added for particular pages to keep things fresh, but it’s just not efficient in the long term when there are several pages where totally different styles should be added. When it comes to reorder content, such as in category view you’ll see the real problem; but you don’t need to worry about that, since K2 is already here and ready to go!

In simple terms, the K2 component allows you to overwrite a K2 view with a custom template – which may include totally original HTML structure, styles and so on. Let’s look at the additional views in our Events Joomla template; thanks to K2 we were able to add three new views: Events, Speakers and Sponsors.

Events Agenda list view

As you can see in the examples available on our demo website, the custom K2 template may even include custom fields which allows you to customize every single part of this view. The Speakers view is just a regular K2 categories view so when you compare this look to the regular category layout you can see just how much of a difference the K2 template makes.

How to add your own custom K2 template?

All K2 views are defined in the override K2 template, located in template/html/com_k2/templates, and the structure in the Events template looks like this:

K2 template directory with templates view

In the visible directories, namely speakers, events and sponsors, we have added the extra views. The default folder contains regular K2 views that are overridden by the template. In the custom template view you may overwrite every K2 view and layout, like item, category item and latest items, so you have full access to all the generated views that K2 offers. To create a new one it is just necessary to add a new directory name and copy the base layout files from the default folder into it. Now, if you modify items in this new directory they won’t affect the standard K2 view unless you assign the template to a particular item. It’s also a great way to test some custom K2 changes before applying them to all pages, if you’re the tinkering kind.

Every directory that you add in the aforementioned path will automatically add a new custom view, which will be available in your menu item settings. When you go to the menu item settings (which of course should be a K2 item), in the Options tab you may choose a template for this view using the Select a template field. In this dropdown list you should see the name of the directory you created, and choosing this template will take the settings and views from your custom directory rather than the default views. That’s all!

K2 selecte custom template option

Creating custom K2 templates also has another great benefit – K2 updates will not overwrite these views if you create them in the template directory. So, if you need to create a totally custom view which will use K2 content you should give this solution a try; forget about custom HTML, modules etc… and just add a new K2 template which you can use later. Check out the extra views in Events to see how powerful this feature can be:

Events Joomla Template | Events view
Events Joomla Template | Speakers view
Events Joomla Template | Sponsors view

The power of custom K2 templates 4.785 (95.56%) 9 votes
Share
This article was first published August 18th, 2014