Page 1 of 1

Opencart 2 php code to - opencart 3 twig?

Posted: Thu Nov 21, 2019 12:25 am
by korisnik
I used this code in opencart 2.3:

Code: Select all

<?php echo str_replace(array('-300x300.jpg', '-'), array('', ' '), basename($thumb)); ?>
to change product image alt tag to product image name instead product name.
How to convert this code to twig, i would like to use this in opencart 3?
I tried some online convertors, but without success.

Re: Opencart 2 php code to - opencart 3 twig?

Posted: Thu Nov 21, 2019 12:47 am
by straightlight
The simplest way is to create a $data['basename_image'] as well as the $thumb variable in your controller affecting the provided line above and to recall it in your TWIG file with:

Code: Select all

{{ basename_image }}

Re: Opencart 2 php code to - opencart 3 twig?

Posted: Thu Nov 21, 2019 1:54 am
by pprmkr

Code: Select all

            
            {% set temp_thumb_array = thumb|split('/') %}
            {% set temp_thumb = temp_thumb_array|last|replace({'-300x300.jpg': "", "-" : " "}) %} 
            {{ temp_thumb }}</li>
Basename is not default function in Twig.
Therefore split on '/' into array and take last element ( temp_thumb_array|last ) ( = filename )
Then replace text ( |replace({'-300x300.jpg': "", "-" : " "}) )

Re: Opencart 2 php code to - opencart 3 twig?

Posted: Thu Nov 21, 2019 8:00 am
by korisnik
Thanks pprmkr, it works.

Re: Opencart 2 php code to - opencart 3 twig?

Posted: Thu Nov 21, 2019 5:56 pm
by paulfeakins
korisnik wrote:
Thu Nov 21, 2019 8:00 am
Thanks pprmkr, it works.
Maybe, but it should be done in the controller and it should use a RegEx so you don't have to hard code the 300 image sizes.

Re: Opencart 2 php code to - opencart 3 twig?

Posted: Thu Nov 21, 2019 8:47 pm
by letxobnav
exactly, never put data logic in the view