I have recently started with twig. For an online shop i want to display certain images, if there is a number written in a specific field.
This is my code so far:
{% if not myItemData.free1 is empty %}
<img class="free1img"
src="/documents/gefahrenicons/{{ myItemData.free1 }}.gif"
{% endif %}
It works as long as there is only one number written into the field.
When i put 02 in the field, the frontend shows the image 02.gif from that chosen folder.
Can someone help me how i would load multiple images, when they are written in the field like this:
02, 03, 04
In this case picture 02.gif, 03.gif and 04.gif should be shown. Right now it is trying to load one image with the name 02, 03, 04.gif
Greetings,
Malwin
You are looking for the filter split
{% if not myItemData.free1 is empty %}
{% for image in myItemData.free1|split(',') %}
<img class="free1img" src="/documents/gefahrenicons/{{ image|trim }}.gif" />
{% endfor %}
{% endif %}
demo
Related
Two part question:
One:
I've used ACf to make some fields for linking to a Zoom meeting, including a date field and a time field, and I want a banner to only show if the date value is equal to or greater than the current date (i.e. today or future) - I've tried:
{% if post.meta('zoom_meeting_date') >= 'now'|date %}
<!-- markup here -->
{% endif %}
This is only working if the date is in the future, not if it is today.
Two: I want a 'join' button that will only show once it is within say 15mins of the start time - no idea where to start with that one!
Any help greatly appreciated!
I suggest using the same date filter without time on both sides of the comparison: |date('m/d/Y')
{% if date("2020-03-30 23:59:59.000000")|date('m/d/Y') >= 'now'|date('m/d/Y') %}
<h1>bang<h1>
{% endif %}
{% if date("2020-03-30 00:00:01.000000")|date('m/d/Y') >= 'now'|date('m/d/Y') %}
<h1>boom<h1>
{% endif %}
{{ 'now'|date }}
{{ 'now'|date('m/d/Y') }}
I have a view in which I show several content types so I need to filter some custom menu entries that I've made in "views-view--myview.html.twig" based on the content type that is shown in the current view.
I've used xdebug but cannot print the content type machine name in my view (I need the content type machine name, not the label!)
Please note that I don't want to do that in the views-view-fields template.
My question is: How can I get the content type (that is shown in a view) in the views-view.html.twig file?
If there is a preprocess function it would also help me!
in views-view.html.twig or overriden twig such as views-view--myview.html.twig you can do the following
get title of first row entity
{{rows[0]['#rows'][0]['#row']._entity.getTitle()}}
get type/bundle of first row entity
{{rows[0]['#rows'][0]['#row']._entity.bundle()}}
So to access all rows you probably need a loop
following checks the entity type/bundle for each row
{% for row in rows[0]['#rows'] %}
{% if row['#row']._entity.bundle() == 'page' %}
<div>its a page</div>
{% endif %}
{% endfor %}
But perhaps the best place to modify individual rows of a view is inside a specific display of a view such as views-view-unformatted.html.twig or views-view-unformatted--myview.html.twig
for comparison get title and type/bundle and looping through rows as above
{{rows[0]['content']['#row']._entity.getTitle()}}
{{rows[0]['content']['#row']._entity.bundle()}}
{% for row in rows %}
{% if row['content']['#row']._entity.bundle() == 'page' %}
<div>its a page</div>
{% endif %}
{% endfor %}
I would like to define a twig object that contains two properties :
The first one is a short text
The second one is a large text (so, i need to use {% set %}{% endset %} to keep readability of the template)
I first tried like this :
{% block principal %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': ''} %}
{% set a_form_help.help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
<p>And several lines of tips for this field</p>
{% endset %}
{% endblock %}
But Twig complains about the use of the dot in the second {% set %} tag (for setting the large text content in the help_content property).
I've found this message (Twig - Why does it not allow us to set object / array values?) and then done this workaround by using a temporary variable:
{% block principal %}
{% set tmp_help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
{% endset %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': tmp_help_content} %}
{{ dump(a_form_help) }}
{% endblock %}
I've used a a temporary variable since using the merge() filter would give the same result.
Is this workaroud a good practice or is there a better/cleaner way ?
Thanks in advance for any tip|advice ! :-)
There is no clean way to do it in twig.
Is this workaroud a good practice or is there a better/cleaner way ?
It is not a good practice. As twig is a templating language its responsability is only to display data, not to structure it. Your View Object should have been created elsewhere
Hi how do I access an image from a different entry / page /url? I'm building in Craft CMS.
I'm creating a nav that will be used across the site. It works fine and I can pull the title and page url ok. I want to use a picture from that page as a thumbnail for the link but it doesn't work.
On my page I have 4 or 5 featured images but I just want to access the first one. What ever I try doesn't work
{% set image = craft.entries.id(50 ####this is the page ID that the image is attached to).featuredImages %} - doesn't work.
Basically how do I access an image when all I have is the id number of the page it is attached on?
Any help would be much apreciated,
Thanks.
Phew, found it. Perhaps there's a cleaner way but:
{% set pageData = entry.id(50) %} ## or in my case swap 50 with node.entryId as I'm using Navee plugin to build my nav.
{% set x = 1 %}
{% for image in pageData.featuredImages %}
{% if x == 1 %}
<img class="navImage" src="{{ image.getUrl('siteThumb') }}" alt="{{ page.title }}">
{% set x = 2 %}
{% endif %}
{% endfor %}
I am inserting a list of sponsor images in a sidebar in grav and want to show only images starting with sponsor_.
At the moment, my code adds any image in the page folder.
base.html.twig:
<div class="sidebar-right-content">
<!-- insert R sidebar images here -->
<div class="sponsor-image">
<h3>Sponsors</h3>
{% for image in page.media.images %}
<br>{{ image.html }}
{% endfor %}
</div>
I have tried restricting the images returned by using the expression below, but no images are returned:
{% for image in page.media.images if image matches '/^sponsor.*$/' %}
Is there a way to use a filter in this context?
try: {% image in page.media.images |contains('sponsor') %}
After some testing of a few options, I found a somewhat inelegant way to solve this problem.
The main thing to undertand is that page.media.images returns an array, so we can't filter this as a string.
So I just nested a match statement. I wonder if there's a one-liner solution.
{% for image in page.media.images %}
{% if image.html matches '/.*sponsor.*$/' %}
<br>{{ image.html }}
{% endif %}
{% endfor %}