How to open a link in a new tab using twig template language.
Here is my code to open the link
{% set link = getUrl %}
I tried this but failed.
{% set link = getlUrl, {'target': 'blank_'} %}
Try something like this
<a target="_blank" href="{{ path('_welcome') }}">Home</a>
Or smyfony 3
<a target="_blank" href="{{ path('homepage', {'_fragment': 'home'}) }}">Home</a>
Related
I have some issue with drupal 9 and the menu. Basically i want to add some extra fields for menu link, like icon (image or svg upload) and some text for description. I use Simplify menu for the menus.
I already tried with Menu item extras module but i can't print the added fields in twig file.
Someone know how to fix this and get the fields value in twig template? (menu.html.twig)?
Thanks
You can print it in *.html.twig file, the below code I rendered the main menu like:
{% set items = simplify_menu('main') %}
{{ menu_item.NEW_FIELD }}
<nav class="nav">
<ul class="navbar-ul">
{% for menu_item in items.menu_tree %}
<li class="nav-item">
<a class="nav-link" href="{{ menu_item.url }}">{{ menu_item.text }}</a>
</li>
{% endfor %}
</ul>
</nav>
I am looping through a list of social profiles to render the logo. Each social item has a field of svg_icon_code that looks like "twitter" or "facebook". I want to use this code to render the correct file. Currently I am just hardcoding in "twitter" resulting in all logos being the Twitter svg. How can I use the icon_code to render the correct svg?
{% for item in options.social_media_links %}
<li class="header__social-media-links__item">
<a href="{{item.link}}" target="_blank">
{% include 'component/svg-twitter-icon.twig' %}
</a>
</li>
{% endfor %}
You need to concat the icon like this,
{% for item in options.social_media_links %}
<li class="header__social-media-links__item">
<a href="{{item.link}}" target="_blank">
{% include 'component/svg-'~item.svg_icon_code~'-icon.twig' %}
</a>
</li>
{% endfor %}
If an icon doesn't have a template or the template is missing you can do this to render a default icon
{% for item in options.social_media_links %}
<li class="header__social-media-links__item">
<a href="{{item.link}}" target="_blank">
{% include [ 'component/svg-'~item.svg_icon_code~'-icon.twig', 'component/svg-default-icon.twig' ] %}
</a>
</li>
{% endfor %}
Here's partial template:
<a href="{{ url('videos') }}">
<h6 class="borbottom text-uppercase"><i class="fab fa-youtube"></i> Serke TV</h6>
</a>
{% for p in page.find('/videos').children if p != page %}
<h3>{{ p.title }}</h3>
{% endfor %}
I have videos route, that displays all his child elements. I want to display all videos (child elements) in a block as partial template, which i certain pages.
However that page.find() nor page.collection('videos') not working for me.
You are using page object which is the current page. So you are looking for videos pages in the current page.
You need to use pages object to be able to search in all pages. You can see the functions you can use with this object here
I am using Ajax in my Django project to implement some simple searching. In my template folder, I have created an html file called "ajax_search.html" where I write how I want my objects to be displayed on a page. Right now it looks like this:
{% if puzzles.count > 0 %}
{% for puzzle in puzzles %}
<a href="{% url 'puzzle' puzzleID=puzzle.id %}">
{{puzzle.title}}
</a>
<br>
{% endfor %}
{% elif search_len < 3 %}
{% else %}
No puzzles match the search
{% endif %}
This works fine, but I wanted to style it to look better. Is there any way I could import some CSS into this template?
Thank you!
I'm trying add Lightbox plugin to my proyect, I need to use it on users images, but when I load the page Ligthbox another div without images which have position absolute and superimpose on all page. So, plugin
behavior continues being normally. You can see the problem on this image:
Lightbox2 Bad Behavior
The funniest thing that I have other page with Lightbox implemented, but there is no problems. I use the exactly same code on both parts of proyect, so here you have:
{% if image != null and image != '' %}
<a id="avatar" href="{{ asset(item.getWebPath) }}" data-lightbox="image" data-title="{{ 'field.avatar'|trans }}">{{ image }}</a>
{% endif %}
{% set image2 = entity_field(item, 'path2', definition_fields['path2']) %}
{% if image2 != '' and image2 != null %}
<a id="handwriting" href="{{ asset(item.getWebPath2) }}" data-lightbox="image" data-title="{{ 'field.image_writting'|trans }}">{{ image2 }}</a>
{% endif %}
{% endif %}
This block have Twig code, but the main it's just on the links. Do you find any problem or have any ideas?
Do you have http:// at the beginning of your url inside the Twig string? Need the full path to load correctly. Greetings.