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
Related
I have a page view that displays title and body fields and the twig template for the view is named as "page--my--view.html.twig". The view content is as follows:
{% if page.content %}
{{ page.content }}
{% endif %}
How can I add individual fields and classes in this template? For example:
<h3 class="title">{{ fields.title.content }} </h3>
I want to add a 'for' loop and print all the titles through page template like above. How to do this?
I have the following in a custom text field in a view:
<h5 class="rates_title">{{title}}</h5>
{%if field_car%}
<p class="car">{{field_car}}
{%if title != "Home" %}
<span class="small_caps">car</span>
{%endif%}
</p>
{%else%}
<p class="view-details">View Details</p>
{%endif%}
Everything works except the {%if title != "Home" %} part. Every item gets the span tag, even Home, which I do not want. Yes, the title is Home, and I can confirm that in the <h5> tag.
title at that point is an array and needs to be rendered but then it will contain html tags so you need to search in that rendered string for Home like this
{% if 'Home' in title|render %}
<span class="small_caps">car</span>
{% endif %}
How would I be able to supersede the hierarchical dependencies in Drupal 8's twig engine to be able to loop within the i.e Lists/Views which is assigned to a block. So we would have a template: block--views-block--[machine-name]-1.html.twig You will be required to have the variable {{ content }}
Which then recursively buries itself down to field templates. Its completely killing me that one would need so many levels to produce on block of content.
I would like to iterate within the top custom block template the list.
Attempted
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
To evaluate what is available to iterate down into the object but with no luck. I did though find a nice overriding object structure to reach the field attributes but that was within the field level
item.content['#item'].entity.uri.value
Thanks
i use this to "generate" a picture from my
node--news--full.html.twig
<div class="col-md-3">
{{ content.field_newsbild }}
</div>
the twig debug suggests some filenames. i took this:
field--node--field-newsbild--news.html.twig
and in there i wrote:
{% for item in items %}
<img alt="" src="{{ file_url(item.content['#item'].entity.uri.value) }}" class="img-responsive" {{ attributes }} >
{% endfor %}
hope i'll help a bit.
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.
I'm trying show image using assetic (symfony2, twig). URL address of image depends on the bundle.
{% image '#'{{ bundleName }}'/Resources/public/images/logo.png' %}
<img src="{{ asset_url }}" />
{% endimage %}
But it does not work.
You need to use the concatenation operator (~) to combine expressions inside twig tags:
{% image '#' ~ bundleName ~ '/Resources/public/images/logo.png' %}
I found a workaround for this. My situation is that I have images stored in a subdirectory under Resources/images. I wanted them there, rather than the web directory, as they will only be used temporarily and I want to just delete the subdirectory when they are no longer needed rather than having to delete images from various subdirectories. So, I couldn't use the asset() function (since the images were in the bundle) and assetic doesn't seem to support dynamic images.
I needed to display one of 2 banner images based on certain criteria. I wound up creating a twig template that would generate each image using assetic (2 images, so 2 templates).
<a href="{{ vm.bannerLinkUrl }}">
{% image '#MyBundle/Resources/public/images/path/to/image/banner.jpg' %}
<img src="{{ asset_url }}">
{% endimage %}
</a>
I created a 'banner' action in my Home controller to render the templates.
public function bannerAction()
{
/* code to load data into a view model object called $vm */
$template = $this->render(
"MyBundle:Home:path/to/banner-template-subdir/{$vm->bannerType}-banner.html.twig",
['vm' => $vm]
);
return $template;
}
Finally, I used the render function to generate the correct banner from the main page template.
<div class='banner'>
{{ render(controller("MyBundle:Home:banner")) }}
</div>