Post by trujve » Fri Dec 01, 2023 12:14 am

Hej folks,

i try to build my first extension and the only thing i want to do for now is to replace an existing twig template with my own.

What i had so far is:

event:

Code: Select all

'trigger'		=> 'catalog/view/product/product/before',
'action'		=> 'extension/myextension/module/myfirstextension.something',
function:

Code: Select all

public function something(string &$route, array &$data): void {

	$data['template'] = 'extension/openwebdev/catalog/view/product/product';
	$this->response->setOutput($this->load->view($data['template'], $data));
	
}
the new twig file:
Image

Unfortunately the new file is not output, does anyone have any idea why?

Best,
Jack

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by JNeuhoff » Fri Dec 01, 2023 12:27 am

After the execution of your before-event, OpenCart will then execute the original code and use the old template again.

You'd have to create your own theme, see the embedded oc_theme_example.ocmod.zip for an example.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by chongshengdz » Fri Dec 01, 2023 5:30 am

you can download a free theme from marketplace and learn from it first before making your own theme

https://www.ectransistors.com/
https://www.transistormosfet.com/


Active Member

Posts

Joined
Sat Apr 12, 2014 10:18 pm


Post by trujve » Fri Dec 01, 2023 1:10 pm

Thank you so much. That makes sense!

But than I think it is a bit to much to build my own theme, because I only wanted to replace a string in the product.twig with the strg_replace function.

Best,
Jack

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by JNeuhoff » Fri Dec 01, 2023 7:25 pm

trujve wrote:
Fri Dec 01, 2023 1:10 pm
Thank you so much. That makes sense!

But than I think it is a bit to much to build my own theme, because I only wanted to replace a string in the product.twig with the strg_replace function.

Best,
Jack
Fair enough. When I have some time later today, I'll post an example how to do it with a simple before-handler.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by trujve » Fri Dec 01, 2023 10:23 pm

JNeuhoff wrote:
Fri Dec 01, 2023 7:25 pm
Fair enough. When I have some time later today, I'll post an example how to do it with a simple before-handler.
That would be incredibly nice @JNeuhoff
Because I've already managed that with an "after" handler. However, it was also just a simple string. A special string with variables like

Code: Select all

<div>{{ something }}</div>
is a mystery to me ???

Jack

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by JNeuhoff » Sat Dec 02, 2023 12:05 am

OK, I think the event handler needs to look something like this:

Code: Select all

<?php

	......

	// event handler for catalog/view/product/product/before
	public function eventViewProductProductBefore( &$route, &$data, &$code=null ) {

		$template_code = $this->getTemplateCode( $route, $code );

		// modify the template code
		// .....

		$code = $template_buffer;
		return null;
	}


	protected function getTemplateCode( $route, &$code ) {
		if (!empty($code)) {
			return $code;
		}

		$file = DIR_OPENCART . $route . '.twig';
		if (is_file($file) {
			$code = file_get_contents( $file );
		} else {
			trigger_error("Cannot find template file for route '$route'");
			exit;
		}

		return $code;
	}

	......
?>
I haven't tested it, but it should work, try it.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by trujve » Sat Dec 02, 2023 3:02 am

JNeuhoff wrote:
Sat Dec 02, 2023 12:05 am
OK, I think the event handler needs to look something like this:

Code: Select all

...
I haven't tested it, but it should work, try it.
You are so great O0
So many thanks!!!

Here is my adapted code, because i had some smal errors, but not worth mentioning :-)

Code: Select all

public function myfunction(&$route, &$data, &$code = null)
{
$template_code = $this->getTemplateCode($route, $code);
$template_buffer = str_replace('<h1>{{ heading_title }}</h1>', '<h1>{{ heading_title }} - TEST</h1>', $code);
$code = $template_buffer;
return null;
}

protected function getTemplateCode($route, &$code)
{
    if (!empty($code)) {
        return $code;
    }

    $file = DIR_OPENCART . 'catalog/view/template/' . $route . '.twig';
    if (is_file($file)) {
        $code = file_get_contents($file);
    } else {
        trigger_error("Cannot find template file for route '$route'");
        exit;
    }

    return $code;
}

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm
Who is online

Users browsing this forum: No registered users and 2 guests