How to add active class if current url matches the given (twig) - twig

I want to write manually a side menu for the store on opencart, and I have a problem - how to make twig add the class "active" to the link for current page
I tried to do it like this
page about something
but it doesnt work

This is exactly what the category module does so you can copy it:
{% if child.category_id == child_id %}
- {{ child.name }}
{% else %}
- {{ child.name }}
{% endif %}
You can find the file above here:
/catalog/view/theme/default/template/extension/module/category.twig
You can find its controller here:
/catalog/controller/extension/module/category.php

They way I would do it is out the route request in by the controller phone file. This would be something like the below:
$urlroute = $this->request->get['route'];
Then in the twig file you could simply check the route variable in an IF query
{% if urlroute == "something" % }
page about something
{% else %}
page about something
{% endif %}

Related

Shopware : Impossible to extend through plugin user-detail

I have Shopware 6.5.3. I was trying to extend "sw-users-permissions-user-detail" like this :
import template from './sw-users-permissions-user-detail.html.twig';
Shopware.Component.override('sw-users-permissions-user-detail', {
template
});
And file 'sw-users-permissions-user-detail.html.twig'
{% block sw_settings_user_detail %}
{% parent %}
{% block test %}
<p>Blabla</p>
{% endblock %}
{% endblock %}
It's not working at all, and I don't know why.
Any help ?
NB : It's working when I'm overriding other templates :
Component.override('sw-dashboard-index', {
template
});
If you want to put the original contents to the block, that you are overriding, you should use the 'parent' statement like this:
{{ parent() }}

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

How can I check whether the provided URLs is youtube or vimeo?

I have this two URLs:
https://youtu.be/erwZDijlFAA
https://vimeo.com/262998843
Now i want to check it by Twig. How can i check ?
{% if url %} <p>youtube</p> {% else %} <p>vimeo</p> {% endif %}
Or have any way to check whether the provided URLs is youtube or vimeo ?
You can use a simply version that use starts with operator like:
{% if url starts with 'https://youtu.be/' %}
YouTube.com
{% endif %}
Or for more complex criteria you can use Regular expression thru matches like:
{% if url matches '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i' %}
YouTube.com
{% endif %}
RegEx credits: https://gist.github.com/ghalusa/6c7f3a00fd2383e5ef33
More info in the doc about comparison operator here
Hope this help
You can use the below code to segregate the videos in twig.
{% set link_type = view.field.body.original_value %} // Fetching value from the body field
{% if ('youtube' in link_type|render|render) %}
youtube url
{% else if ('vimeo' in link_typr|render|render ) %}
vimeo Url
{% endif %}

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

getting route name not the route path in twig symfony

i'm developing an application with symfony3.
I want to get route name in twig. i did this :
{% set current_path = path(app.request.get('_route')) %}
{{ current_path }}
it displays the url of the current page. But i want to get route name not the path.
example :
personnel_index:
path: /liste
defaults: { _controller: "PersonnelBundle:Personnel:index" }
methods: GET
must return : personnel_index
so how can i get the route name
This is because you put the path function try like this
{% set current_path = app.request.get('_route') %}
{{ current_path }}
In the twig template, there is a global variable activeRoute.
For example {{ dump(activeRoute) }} will give you the route name.
There is one example at vendor/shopware/storefront/Resources/views/storefront/component/address/address-personal.html.twig:40
GitHub Link
With this source code.
{% if activeRoute == 'frontend.account.login.page' %}
{% set isLoginPage = true %}
{% endif %}

Resources