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 %}
Related
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
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 %}
Trying to edit a particle in Joomla -- I'm fairly new to twig and I'm trying to list information based on a selection in vertical tabs. I have an on-click refresh for my li edit that makes a tab "selected" and creates my internal url:
{% for item in particle.items %}
<li>
<a href="/Joomla/about-the-library/locations#{{ loop.index }}">{{ item.title|e }}
<img src="{{ url(item.image)|e }}" alt="{{ item.title|e }}">
<div class="g-flexslider-carousel-item-image-preview-icon"></div>
</a>
</li>
{% endfor %}
This is all well and good, but I ran into an issue when trying to display the data associated with the selected item. I tried to create a variable to check my items against, but the variable doesn't seem to be coming back as an integer, and I've tried a few things:
{% set branch = app.request.pathinfo|trim('/Joomla/about-the-library/locations#') %}
{% if loop.index == branch %}
<div class="g-flexslider-showcase-item-content-container">
<div class="g-flexslider-showcase-item-image">
<img src="{{ url(item.image)|e }}" alt="{{ item.title|e }}">
</div>
Can anyone tell me what I'm doing incorrectly?
(I found that get current url in twig template? helped, but I'm not sure I'm using the answers provided correctly. I've also sifted through the Twig Documentation for a couple hours to no avail.)
[Nov 2016] - This is still what "my" code looks like for this section. It seems to just be a problem with this if statement, as the "else" statement (which I'm using for debugging purposes) keeps coming through.
{% for item in particle.items %}
{% if app.request.get('name') == item.URLname|e %}
<p>You have branched {{ item.title|e }} correctly. </p>
I am very new with webhook.com , But i have setup the cms and able to manage my site the issue is i want to join two strings with the if else condition
{% if link!="" %}
{% set link = object.menu_url %} menu link will look like "https://www.example.com" it is ok
{% else %}
{% set link = object.menu_id %} menu link will look like "#menulid"
{% endif %}
i just want to add # with the menu id something like {% set link = "#".object.menu_id %}
use + sign to concatinate like
{% set link = "#"+object.menu_id %}
I am trying to get a list to highlight if it is at its current page (route). However, I also have subpages within the pages of which I also want the list to be highlighted.
I have my menu if statements:
<ul class="menu">
{% if app.request.get('_route') == 'home' %}
<li class="current">Home</li>
{% else %}
<li>Home</li>
{% endif %}
{% if app.request.get('_route') == 'reports' %}
<li class="current">Reports</li>
{% else %}
<li>Reports</li>
{% endif %}
// etc etc
Now in my reports page, the route is /reports/ I have a menu that clicks to "Detail", "Summary", etc it will go to /reports/detail and /reports/summary... I want it so that when users click on those links, the main navigation is still highlighted.
I was wondering if there is an if statement condition something like this:
{% if app.request.get('_route') starts with(?) 'reports' %}
So whenever anyone goes to a route that's a sub page of /reports/, the "Reports" li in the menu will still be highlighted?
I'm not sure if twig has a function for "starts with" but you can check for containment using in
{% if 'reports' in app.request.get('_route') %}
Just to update this question with current information.
As per current (2.x) Twig documentation this is possible the way it is asked for.
To be more specific, the documentation states:
You can also check if a string starts with or ends with another string:
{% if 'Fabien' starts with 'F' %}
{% endif %}
{% if 'Fabien' ends with 'n' %}
{% endif %}
As such, the desired expression is perfectly possible:
{% if app.request.get('_route') starts with 'reports' %}
And works as expected.
There are some good choices on this thread but another option is to pass the route into an array. This is useful if you have dropdown menu items.
The items in the array would be your defined routes.
{% set routes = {
'activities':
[
'walking',
'swimming',
'running'
]
} %}
Then in your menu bar add this to your menu label class.
{% if app.request.attributes.get('_route') in routes.activities %} Do Something {% endif %}