IF URL statment opencart 3 twig files - twig

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

Related

How to add active class if current url matches the given (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 %}

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

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

django-haystack and endless pagination can't {% include page_template %}

#page_template('voziladijelovi_dodatak.html')
def traziVozilaDijelovi(request):
sqs = SearchQuerySet().filter(kategorije=('Vozila i Dijelovi')).facet('podkategorije').facet('drzava').facet('grad')
view = FacetedSearchView(form_class=FacetedSearchForm, searchqueryset=sqs)
return view(request)
in template {% include page_template %} doesn't work.
extra_context {u'page_template': u'voziladijelovi_dodatak.html'} is missing from
create_response()
How to include this in view
Do you mean like:
view = FacetedSearchView(form_class=FacetedSearchForm,
searchqueryset=sqs,
template='voziladijelovi_dodatak.html')

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