Page 1 of 1

Custom category layout using alternative .twig template?

Posted: Tue Sep 15, 2020 10:09 pm
by applab
Hi,

Opencart Version 3.0.3.2
Theme: Default (with minor CSS tweaks)

I would like just some of my categories to use a different twig template rather than the default one. I thought I might be able to do this using a custom layout and then applying this using the 'Layout Override' option on the Categories->Design tab. However, I can't see any way to specify which twig template gets used.
Is this even possible?

I'm new to OC development but an old hand with HTML/CSS/PHP/JS (& Twig) so happy to dig into code if necessary.

Any pointers much appreciated.

Thanks.

Re: Custom category layout using alternative .twig template?

Posted: Tue Sep 15, 2020 10:53 pm
by IP_CAM

Re: Custom category layout using alternative .twig template?

Posted: Tue Sep 15, 2020 10:57 pm
by letxobnav
there is a "Layout Override" option?

Re: Custom category layout using alternative .twig template?

Posted: Tue Sep 15, 2020 11:07 pm
by letxobnav
you can call any view you have created in a controller as long as you make sure they receive the $data they expect:

Code: Select all

// use different views for different categories
$cat_view_1 = array(23,44,66,2,55);
$cat_view_2 = array(16,53);
if (in_array($category_info['category_id'], $cat_view_1)) {
	$this->response->setOutput($this->load->view('product/category_view_1', $data));
} elseif (in_array($category_info['category_id'], $cat_view_2)) { {
	$this->response->setOutput($this->load->view('product/category_view_2', $data));
} else {
	$this->response->setOutput($this->load->view('product/category', $data));
}

Re: Custom category layout using alternative .twig template?

Posted: Tue Sep 15, 2020 11:19 pm
by applab
@IP_CAM - the last of those modules (Custom Pages) looks interesting, however, for now I've found a solution requiring modification to just one file.
@letxobnav - Yes, I thought I might be able to use that but I couldn't find a way to use an alternate template for the same route (product/category)
@letxobnav - Just seen your 2nd post, it's along the same lines as what I've done but I was able to use a conditional instead of hard-coding category id's.

My requirement was to have an alternative twig template for any categories that don't have sub-categories, so here's what I've done, I'm not sure I've gone about it the best way so I would still appreciate any feedback.

1) create new twig file /theme/default/template/product/category2.twig (copied category.twig then modified as required)
2) copy /catalog/controller/product/category.php to /system/storage/modification/catalog/controller/product/category.php
3) modified the copy of category.php as follows:

after the call to getCategories set a boolean depending on whether the $results array is empty

Code: Select all

$results = $this->model_catalog_category->getCategories($category_id);
$lowest_level = count($results) ? false : true;  // added
then where the view is loaded add an 'if' statement...

Code: Select all

if($lowest_level) {
	$this->response->setOutput($this->load->view('product/category2', $data));
} else {
	$this->response->setOutput($this->load->view('product/category', $data));
}

Re: Custom category layout using alternative .twig template?

Posted: Wed Sep 16, 2020 7:51 am
by letxobnav
yes, you can use any condition really.
The layouts can only be defined on route level with/without a wildcard, besides a layout only defines which modules are statically placed if and where on a layout, no dynamics there.

Re: Custom category layout using alternative .twig template?

Posted: Wed Sep 16, 2020 8:41 am
by IP_CAM
Well, you don't seem to be a Coder Newbie, as it looks, :laugh:
it's kind of refreshing, to at least once in a while find such 'Newbies' here ... :D
Good Luck! ;)
Ernie

Re: Custom category layout using alternative .twig template?

Posted: Wed Sep 16, 2020 10:03 pm
by applab
letxobnav wrote:
Wed Sep 16, 2020 7:51 am
The layouts can only be defined on route level with/without a wildcard, besides a layout only defines which modules are statically placed if and where on a layout, no dynamics there.
Still getting my head around layouts, I find it easier to just edit the twig files but I guess I should learn to do things the 'proper' way ;-)
The ability to apply a custom twig template to a layout would be nice, though there might be implications I haven't thought of.

Re: Custom category layout using alternative .twig template?

Posted: Wed Sep 16, 2020 10:09 pm
by applab
IP_CAM wrote:
Wed Sep 16, 2020 8:41 am
Well, you don't seem to be a Coder Newbie, as it looks, :laugh:
it's kind of refreshing, to at least once in a while find such 'Newbies' here ... :D
Good Luck! ;)
Ernie
Thanks Ernie :-)

Many years of coding under my belt, and thankfully the Opencart source code is quite easy to find your way around.

Checked out your Github repo. Pleasantly surprised to find that 1.5.6 still has it's fans, that's the last version I used and I remember it fondly.

Re: Custom category layout using alternative .twig template?

Posted: Wed Sep 16, 2020 11:43 pm
by IP_CAM
Checked out your Github repo. Pleasantly surprised to find that 1.5.6 still has it's fans ...
Well, 1.5.6.(5) is the only Version, able to handle PHP v.7.4.9, by only replacing the system 'encryption.php'
File, and no other Version ever offered a comparable Range of free Extensions. It's a Goldmine for those,
looking for a speedy & simple free Solution, and so far, I just don't see any Forum Postings, complaining
about technical or security-related Problems. So, why should I care about later Releases, as long as my
OC Baby works so well ... :laugh:

But from the Developer Point of View, 1.5.6.x is not big Business anymore, it's therefore understandable,
that some of the DEV's don't seem to like it. But that's sure not my Problem, to me, it's just another OC,
like every other Version too. An some will further use it, it's one reason, to have it on Github, in addition
to most OC other Version Github-placed OC Extensions, available on my Github Repository Site. And
should I ever run into Problems, I can count on some Friends, willing and able to be of help ... :D It also
keeps me from playing 'latest' Versions Tester, required, to move up every few months again, to eventually
some day end up with something, as solid as the one I use already now ... :crazy: :laugh:

Still, I don't even have a 'real' Online Shop, and I don't need one either, it's more, to keep Mr. Alzheimer
away as far as possible, and my self-trained english intact, because with 74+, I'm probably one of the oldest
Fellows around here. ;)

Ernie