Page 1 of 1

tpl to twig

Posted: Fri Jun 21, 2019 8:30 pm
by skip
How to convert this line from product tpl to product twig file ?

Code: Select all

<div class="price2" style="font-size:17px;"> </br> <?php   $number = preg_replace("/[^0-9\.]/", '', $price);   
$total= $number*1.2; echo "List price: "."$".number_format($total, 2);  ?> </div> 
 <h2><?php echo "Your price ". $price; ?></h2>
thanks

Re: tpl to twig

Posted: Fri Jun 21, 2019 8:43 pm
by straightlight
No OC version posted and incomplete code statement since the $price variable has not been indicated where it originates from due to variable replacement.

However,

Code: Select all

<div class="price2" style="font-size:17px;"><br>
{% if number %}
	{{ number }}
{% endif %}
Then, in your catalog/controller/product/product.php file, in the index() method, you could add below $price variable IF condition:

Code: Select all

$data['number'] = $this->currency->format($price, $this->config->get('config_currency'), false, false);
instead of:

Code: Select all

$number = preg_replace("/[^0-9\.]/", '', $price);
Then,

Code: Select all

{% set total = number * 1.2 %}
List price: {{ total|number_format(2, '.', '') }}</div>
<h2>Your price: {{ price }}</h2>

Re: tpl to twig

Posted: Fri Jun 21, 2019 11:22 pm
by skip
$number and $total is variables defined directly in view tpl file (there is no code in controller at all) Version of OC is 2302 , and Im trying convert code for 3032

Code: Select all

<div class="price2" style="font-size:17px;"> </br> <?php   
$number = preg_replace("/[^0-9\.]/", '', $price);   
$total= $number*1.2; echo "List price: "."$".number_format($total, 2);  ?> </div>

Re: tpl to twig

Posted: Fri Jun 21, 2019 11:45 pm
by straightlight
You'd still need to define your PHP variables into your controllers or, as an alternative, to use a custom function in order to use preg_replace in TWIG files. Another approach you could use is the TPL Engine from the Marketplace in order to use TPL files into OC v3.0.3.2 rather than using TWIG files.

Re: tpl to twig

Posted: Sat Jun 22, 2019 11:02 am
by letxobnav
you should never put data logic in a tpl or twig file, always in the controller.

Re: tpl to twig

Posted: Sat Jun 22, 2019 8:17 pm
by straightlight
You'd still need to define your PHP variables into your controllers
letxobnav wrote:
Sat Jun 22, 2019 11:02 am
you should never put data logic in a tpl or twig file, always in the controller.
Redundancy. However, the data array don't seem to be used according to the examples the OP has posted, so far, fortunately.

Re: tpl to twig

Posted: Sat Jun 22, 2019 8:33 pm
by thekrotek
skip wrote:
Fri Jun 21, 2019 11:22 pm
$number and $total is variables defined directly in view tpl file (there is no code in controller at all) Version of OC is 2302 , and Im trying convert code for 3032

Code: Select all

<div class="price2" style="font-size:17px;"> </br> <?php   
$number = preg_replace("/[^0-9\.]/", '', $price);   
$total= $number*1.2; echo "List price: "."$".number_format($total, 2);  ?> </div>
TPL is basically PHP, so you can use any PHP functions in it. TWIG is a stupid template engine with its own syntax and limited coding options. In most cases you will have to move any kind of calculations from TPL to controller, like in example above.

Re: tpl to twig

Posted: Mon Jun 24, 2019 10:56 pm
by skip
Ok understand , please give me example how to define one basic variable in controller, for example

Code: Select all

$number=2* $price ;        
and on which place in controller I can set it for 3032 version - index() funkction is 500 rows long.
Thanks

Re: tpl to twig

Posted: Mon Jun 24, 2019 11:27 pm
by straightlight
skip wrote:
Mon Jun 24, 2019 10:56 pm
Ok understand , please give me example how to define one basic variable in controller, for example

Code: Select all

$number=2* $price ;        
and on which place in controller I can set it for 3032 version - index() funkction is 500 rows long.
Thanks
Below the $price variable IF statement in the controller, you could add:

Code: Select all

$data['number'] = $price * 2;
Then, use the {{ number }} in your product.twig file.