Post by korisnik » Thu Nov 21, 2019 12:25 am

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.

New member

Posts

Joined
Sun Aug 07, 2016 1:21 am

Post by straightlight » Thu Nov 21, 2019 12:47 am

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 }}

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by pprmkr » Thu Nov 21, 2019 1:54 am

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': "", "-" : " "}) )

User avatar
Active Member

Posts

Joined
Sat Jan 08, 2011 11:05 pm
Location - Netherlands

Post by korisnik » Thu Nov 21, 2019 8:00 am

Thanks pprmkr, it works.

New member

Posts

Joined
Sun Aug 07, 2016 1:21 am

Post by paulfeakins » Thu Nov 21, 2019 5:56 pm

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.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by letxobnav » Thu Nov 21, 2019 8:47 pm

exactly, never put data logic in the view

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan
Who is online

Users browsing this forum: Majestic-12 [Bot] and 22 guests