How to translate this particular in_array PHP code into Twig template code - twig

I have this code:
<?php if ((in_array('name1', (array)$settings)) && (in_array('name2', (array)$settings))) { ?>
As well as this one:
<?php if ((in_array('name1', (array)$this->config->get('settings'))) && (in_array('name2', (array)$this->config->get(settings')))) { ?>
How can this be written in Twig?
Thanks in advance.

In twig you can write
{% if 'needle' in haystack and ... %}
Oh Yeah!
{% endif %}

Related

Formatting statements

Currently I'm using this:
{% if locale == 'pt' %}
<html lang="pt-BR">
{% elseif locale == 'en' %}
<html lang="en-US">
{% endif %}
It's decent, but makes the whole <html> tag repeat in the source code. What do you think?
In PHP templates I often use this:
<html lang="<?php
if ($locale === 'pt') {
echo 'pt-BR';
} elseif ($locale === 'en') {
echo 'en-US';
}
?>">
I was wondering if there's a way to produce a similar result with Twig. This is the best I got so far, but it looks a bit odd to me:
<html lang="{%
if locale == 'en'
%}en-US{%
elseif locale == 'pt'
%}pt-BR{%
endif %}"></html>
Is there a neater way to do this?
https://twigfiddle.com/4lwsjb
You could create an array/hash to shorten your code:
{% set localization = {
'pt': 'pt-BR',
'en': 'en-US',
} %}
<html lang="{{ localization[locale]|default('en-US') }}">
demo
I would even suggest to just create the localization array in your controller and pass it to the view or register it as a global variable
The default filter I've just added to provide a fallback if the locale is unknown in the array

IF URL statment opencart 3 twig files

can you see whats wrong with this :
{% if 'information_id=10' in url %}
Im trying to use an if statement when the url contains that string, but its not working, have i done something wrong?
Many thanks!
If you want to do something in a special information page, edit catalog\controller\information\information.php, find:
if ($information_info) {
Add after it:
if ($information_id == 10) {
$data['target_page'] = true;
} else {
$data['target_page'] = false;
}
Now in catalog\view\theme\your-theme\template\information\information.twig file, use it:
{% if target_page %}
This is target page.
{% endif %}
You may need to refresh modifications and clear theme cache.
Edit:
or you can pass $information_id from controller:
$data['information_id'] = $information_id;
And in view file:
{% if information_id == 10 %}
...
{% endif %}

Opencart php to twig

can anybody help convert to twig this piece of code.
foreach ($methods as $method){?>
<li><?php echo ${'tab_express' . $method}; ?></li>
<?php } ?>
I have tried this:
{% for method in methods %} <li>{{ 'tab_express' ~ method }}</li>
{% endfor %}
But this part: {{ 'tab_express' ~ method }} does not work. What wrong?
*EDIT:
need retrieve names of the tabs from the controller
foreach ($data['methods'] as $method){
$data['tab_express' . $method] = $this->language->get('tab_express' . $method);
}
*
I answered below
solution for my question i found.
in controller should be:
foreach ($data['methods'] as $method){
$data['tab_express'][$method] = $this->language->get('tab_express' . $method);
Twig:
{% for method in methods %}
<li>{{ tab_express[method] }} </li>
{% endfor %}
In controller it should be:
{{ attribute(_context, 'tab_express' ~ method) }}

html_entity_decode for twig (opencart)

im trying to output an attribute of a product on my product page (opencart v3).
The attribute is called 'technicaldetails' and it works just fine using this code:
{% if attribute_groups %}
{% for attribute_group in attribute_groups %}
{% if attribute_group.name == 'technicaldetails' %}
{% for attribute in attribute_group.attribute %}
{{ attribute.text }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
but the technical details field have unstyled list stored in it.. and this outputs the complete html instead of rendering the list.
ive tried using {{ attribute.text|e }} and {{ attribute.text|raw }} and many other alternatives i could find.. but each time is just throws out the html and not render it..
in php this used to work.
<?php echo html_entity_decode($attribute['text']); ?>
so how can i decode the html now as i cant use php in twig and there is no html_entity_decode in twig either :(
looking forward for somehelp :)
much appreciated
thanks.
Just register the html_entity_decode function in twig.
The most simple way is to look where twig is loaded and add the following code,
$twig->addFilter(new \Twig_Simple_Filter, 'html_entity_decode', 'html_entity_decode');
After that you can just do the following in your twig templates
{{ attribute.text|html_entity_decode }}
UPDATE: For Opencart 3.0.3.7 version filter should be like this:
$twig->addFilter(new \Twig\TwigFilter('html_entity_decode','html_entity_decode'));
Find file
document_root/system/library/template/twig.php
Just after
$this->twig = new \Twig_Environment($loader, $config);
add following code
$twig->addFilter(new \Twig_SimpleFilter('html_entity_decode', 'html_entity_decode'));
After doing this, you must went to admin to reload all modifications in menu Extensions -> modifications.
After that you can do the following in all twig files *.twig
{{ attribute.text|html_entity_decode }}

Check if variable is string or array in Twig

Is it possible to check if given variable is string in Twig ?
Expected solution:
messages.en.yml:
hello:
stranger: Hello stranger !
known: Hello %name% !
Twig template:
{% set title='hello.stranger' %}
{% set title=['hello.known',{'%name%' : 'hsz'}] %}
{% if title is string %}
{{ title|trans }}
{% else %}
{{ title[0]|trans(title[1]) }}
{% endif %}
Is it possible to do it this way ? Or maybe you have better solution ?
Can be done with the test iterable, added in twig1.7, as Wouter J stated in the comment :
{# evaluates to true if the users variable is iterable #}
{% if users is iterable %}
{% for user in users %}
Hello {{ user }}!
{% endfor %}
{% else %}
{# users is probably a string #}
Hello {{ users }}!
{% endif %}
Reference : iterable
Ok, I did it with:
{% if title[0] is not defined %}
{{ title|trans }}
{% else %}
{{ title[0]|trans(title[1]) }}
{% endif %}
Ugly, but works.
I found iterable to not be good enough since other objects can also be iterable, and are clearly different than an array.
Therefore adding a new Twig_SimpleTest to check if an item is_array is much more explicit. You can add this to your app configuration / after twig is bootstrapped.
$isArray= new Twig_SimpleTest('array', function ($value) {
return is_array($value);
});
$twig->addTest($isArray);
Usage becomes very clean:
{% if value is array %}
<!-- handle array -->
{% else %}
<!-- handle non-array -->
{% endif % }
There is no way to check it correctly using code from the box.
It's better to create custom TwigExtension and add custom check (or use code from OptionResolver).
So, as the result, for Twig 3, it will be smth like this
class CoreExtension extends AbstractExtension
{
public function getTests(): array
{
return [
new TwigTest('instanceof', [$this, 'instanceof']),
];
}
public function instanceof($value, string $type): bool
{
return ('null' === $type && null === $value)
|| (\function_exists($func = 'is_'.$type) && $func($value))
|| $value instanceof $type;
}
}
Assuming you know for a fact that a value is always either a string or an array:
{% if value is iterable and value is not string %}
...
{% else %}
...
{% endif %}
This worked good enough for me in a project I was working on. I realize you may need another solution.

Resources