Twig change meta data - twig

i need to get meta data of the page in twig, for replace them by my values, but i don't find how get them.
For now i have :
{% if spec.name matches '{meta}' %}
{% set getdatas = app.request.server.get('REQUEST_URI') %} // use uri for test, i need the same but with meta_title, meta_description...
{{ dump(uri|replace({'domaine': spec.name})) }}
{% endif %}

If you want to override anything in Twig, you need to create a block for it. As an example:
base.html.twig
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
{% block meta %}{% endblock %}
<title>{% block title %}{% endblock %}</title>
{% block stylesheets %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
parent.html.twig
{% extends 'base.html.twig' %}
{% block stylesheets %}
# My extra styles...
{% endblock %}
{% block body %}
<div>
{% block content %}{% endblock %}
</div>
{% endblock %}
child.html.twig
{% extends 'parent.html.twig' %}
{% block title %}My page{% endblock %}
{% block meta %}
<meta key="value">
{% endblock %}
{% block content %}My content{% endblock %}

my parent template :
{% extends 'DesignBundle::Front/layout.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{ asset('bundles/extension/css/flag-icon.min.css') }}">
<link rel="stylesheet" href="{{ asset('bundles/extension/css/main.css') }}">
{% endblock %}
{% block title %}
{{ _("Caractéristiques des noms de domaine") ~ " ." ~ extension.nomExt|idna_decode }}</code>
{% if extension.countryName is not empty %}
{{ " - " ~ extension.countryName|utf8_fix }}
{% endif %}
{% endblock %}
{% block content %} // view in first post is call here
{% include 'ExtensionBundle:New_fiche:show_content.html.twig' %}
{% endblock %}

Related

How to filter out the order status in frontend at orders overview in shopware6

I am trying to filter the order status depending on the orderstatus at frontend.
enter code here
{% block page_account_orders_overview %}
<div class="account-orders-overview">
{% block page_account_orders_table %}
{% block page_account_orders_table_body %}
{% for order in page.orders %}
{% set orderState = order.stateMachineState.technicalName %}
{% if order.type == in_progress %}
<div class="table order-table"
data-order-detail-loader="true">
{% sw_include '#Storefront/storefront/page/account/quotationorder-history/quotationorder-item.html.twig' %}
</div>
{% endif %}
{% endfor %}
{% endblock %}
{% endblock %}
</div>
{% endblock %}
You need to add quotes to a string comparison:
{% if order.type == "in_progress" %}

Trying to load a css stylesheet from a child template in django

I have a parent template with this code
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
{% load static %}
<link rel="stylesheet" href="{% static 'base.css' %}"/>
{% block additionalStyles %}{% endblock %}
</head>
and I have a child template with this code
{% extends 'base.html' %}
{% load static %}
{% block additionalStyles %}<link rel="sytlesheet" href="{% static 'rants.css' %}"/>{% endblock %}
this doesn't seem to work and I don't get an error either. The base.css loads fine and when I inspect element on the webpage I can see <link rel="sytlesheet" href="static/rants.css"/> in the HTML code but it doesn't have any affect on any of the elements that it should. Is there a better way of doing this? and why is it not working?
Edit: It magically started working?? idk why or how, here is the new code if anyone has the same problem, in child:
{% extends "base.html" %}
{% load static %}
{% block title %}Rants{% endblock %}
{% block additionalStyles %}
<link rel="stylesheet" href="{% static 'rants.css' %}"/>
{% endblock %}
in parent:
{% load static %}
<title>{% block title %}{% endblock %} </title>
<link rel="stylesheet" href="{% static 'base.css' %}"/>
{% block additionalStyles %} {% endblock %}
Kindly check the path to the folder that has the CSS file.
When everything seems to work correctly, remember to clear your browser cache or load the site/app in incognito mode(for Google Chrome users).
In the child
{% extends "base.html" %}
{% load static %}
{% block title %}Rants{% endblock %}
{% block additionalStyles %}
<link rel="stylesheet" href="{% static 'path_to_folder/rants.css' %}"/>
{% endblock %}
In the parent
{% load static %}
<title>Parent site{% block title %}{% endblock %} </title>
<link rel="stylesheet" href="{% static 'path_to_folder/base.css' %}"/>
{% block additionalStyles %} {% endblock %}
Need to give the <link> it's own line
{% extends "base.html" %}
{% load static %}
{% block title %}Rants{% endblock %}
{% block additionalStyles %}
<link rel="stylesheet" href="{% static 'rants.css' %}"/>
{% endblock %}
Now works

Twig, is it possible to override an tag atribute of parent template from a child template?

I have two templates, a parent template and a child template.
I want know if its possible to add 'properly' a class to a tag in the parent template from the child template ? and if yes, how ?
By example, if I have this parent.html.twig file :
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
And this child.html.twig file :
{% extends 'parent.html.twig' %}
{% block body %}
{# ... #}
{% endblock %}
From the child.html.twig file, can I add a class to the body tag ? and how ?
Thanks for help :)
You should modify the parent template adding a block, as example:
parent.html.twig
<body {% block bodyclass %}{% endblock %}>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
and use in the child:
child.html.twig
{% extends 'parent.html.twig' %}
{% block bodyclass %}class="child-class"{% endblock %}
{% block body %}
{# ... #}
{% endblock %}
You can try in this twigfiddle

Extending twig blocks in a complicated scenario

Let's say I have these twig templates:
base.twig
{# base.twig #}
<html>
<head>
{% include 'head_js.twig' %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
head_js.twig
{# head_js.twig #}
{% block headJS %}
<script src='/js/some-script.js'></script>
{% block headJSExtra %}{% endblock %}
{% endblock %}
page.twig (the one loaded by the controller)
{# page.twig #}
{% extends base.twig %}
{% block content %}
<p>Widget 1</p>
{% include 'widget.twig' with { name: 'foo' } %}
<p>Widget 2</p>
{% include 'widget.twig' with { name: 'bar' } %}
{% endblock %}
widget.twig
{# widget.twig #}
{% if wigetAlreadyIncluded is not defined %}
{% block headJSExtra %}
{{ parent() }}
<script src='/js/widget.js'></script>
{% endblock %}
{% set widgetAlreadyIncluded = true %}
{% endif %}
<p>My name is {{ name }}</p>
This code doesn't work (can't use parent() in widget.twig as it's not extending or using any template), but it should illustrate what I'm trying to achieve. The basic idea is:
In order to work, widget.twig requires a js library to be loaded in as a tag in the .
The widget can be rendered several times in one page.
Other widgets should be able to also add their own tags in the in this fashion, but they shouldn't override previous added tags (they should be appended).
I don't want to add more than once any tag required by any widget found in the page.
Any ideas on how can I achieve this would be greatly appreciated. I've read two SO related questions with no luck at all.
https://stackoverflow.com/a/29132604/4949663
https://stackoverflow.com/a/18160977/4949663
I finally got a good answer in the twig github issue tracker.
https://github.com/twigphp/Twig/issues/2275

Navigation link works on mobile but not on desktop

i have this website http://sds-test.nowcommu.myhostpoint.ch/de (please use the "de" at the end) and the last link of the navigation "RECHENZENTRUM" does not works on desktop but works on mobile. How can i solve it? This is the twig code:
{% extends 'partials/base.html.twig' %}
{% set show_onpage_menu = header.onpage_menu == true or header.onpage_menu is null %}
{% macro pageLinkName(text) %}{{ text|lower|replace({' ':'_'}) }}{% endmacro %}
{% block javascripts %}
{% if show_onpage_menu %}
{% do assets.add('theme://js/singlePageNav.min.js') %}
{% endif %}
{{ parent() }}
{% endblock %}
{% block bottom %}
{{ parent() }}
{% if show_onpage_menu %}
<script>
// singlePageNav initialization & configuration
$('#navbar').singlePageNav({
offset: $('#header').outerHeight(),
filter: ':not(.external)',
updateHash: true,
currentClass: 'active'
});
</script>
{% endif %}
{% endblock %}
{% block header_navigation %}
{% if show_onpage_menu %}
<ul class="navigation">
{% for module in page.collection() %}
{% set current_module = (module.active or module.activeChild) ? 'active' : '' %}
<li class="{{ current_module }}">{{ module.menu }}</li>
{% endfor %}
{% set datacenter_page = page.find('/services/datacenter') %}
<li>{{ datacenter_page.menu() }}</li>
</ul>
{% else %}
{{ parent() }}
{% endif %}
{% endblock %}
{% block content %}
{{ page.content }}
{% for module in page.collection() %}
<div id="{{ _self.pageLinkName(module.menu) }}"></div>
{{ module.content }}
{% endfor %}
{% endblock %}
The 'rechenzentrum' link is rendered correctly, and copying the link location via right click gives a correct URL that opens a seemingly correct page. So the template rendering is fine.
The link has two onclick handlers attached to it, though. Probably one of them fails and thus prevents navigation. (The JS is minified so I did not try to debug it.)

Resources