Hot answers tagged

14

Twig variables are only accessible inside their own scope. . What is a scope: Tags like {% block %}, {% for %}, {% nav %} create a 'scope', which means that variables defined inside them, can't be accessed from outside. This is similar to local variables inside php-functions: {% block A %} {% set var = 'apple' %} {{ var }} {# This prints apple #} ...


6

You're exactly right: The advantage of using the variable is that you can re-use it throughout your template, which helps keep things DRY. When you use craft.entries in your template, Craft serves up an ElementCriteriaModel — This is an object that knows how to query the system for a set of entries... but it doesn't actually perform that query until you ...


5

Here's your problem... You're redefining entry halfway down the page. This page clearly uses the section's built-in routing functionality. I can tell because you use entry.body near the top of your page without explicitly declaring the value of entry. However, you later on use {% set entry %} to reassign the value of entry. Based on the new value of entry, ...


5

You need to add .first where you are setting the entry variable: {% set entry = craft.entries.id(43).first %} That way you can avoid doing a for loop.


4

Another option is to use the Preparse plugin to automatically parse out and store last names in a hidden field. Install the Preparse plugin Create a new “Last Name” field using the Preparse field type Set the field’s Twig template setting to this: {{ entry.title|split(' ')|last }} Edit the People section’s entry type settings, and add the Last Name field ...


3

There's no problem re-using a variable name from a technical point of view. There is likely a problem elsewhere in your code.


3

Start by looping thru each category. Say, all categories in a category group called "eventsCategories." Then find entries related to each category (if any). {# loop through each of the categories #} {% for category in craft.categories.group('eventsCategories') %} {# find the exhibitor entries that are related to both the Event entry and category #} {% ...


3

It’s not possible to do that unless you’ve already got the last word saved somewhere. Here’s what I would recommend: Create two new Plain Text fields - “First Name” and “Last Name” Go to your People section’s entry type settings Uncheck the “Show the Title field?” setting and set the “Title Format” setting to: {firstName} {lastName} Add the “First Name” ...


3

There’s no way to tell the relatedTo param to only factor in the first X relations per source element (the first 5 tags per user, in this case). I can think of two ways you could accomplish this: Option 1: Use two Specialism fields Replace your “Specialism” Tags field with two Categories fields – “Primary Specialism” and “Secondary Specialism”. Limit “...


3

There may be other ways also, but one approach is to use an array with key value pairs, which can be assigned and accessed more dynamically. {{ sizeVars[imgSize ~ 'x2'] }} You also might want to check out twigs attribute method. {{ attribute(array, item) }} Where 'item' can be a concatenated string.


3

The twig set tag has two syntaxes. You can use it like this: {% set foo = 'bar' %} or like this: {% set foo %}bar{% endset %} They are both useful in different ways. The error you were getting was about mixing them.


3

This article may help as well: Twig Processing Order & Scope


3

Your problem is a scoping issue. You've defined theUrl inside the scope of a for loop, so it only ever lives inside of that loop. You can do this to have it exist outside of the loop, too. {% set theUrl = '' %} {% for item in entry.customerLogoBarUrl.limit(1) %} {% set theUrl = item.url %} {% endfor %} {% for item in entry.customerLogoBar %} {% ...


3

ElementCriteriaModel parameters always add up to further filter down the results, which means an element has to be truthy for each one of them to be returned. So your params basically look right for what you're trying to achieve. The only thing that might mess with the results is the for me unusual use of the w3c methods on the DateTime objects, which might ...


3

In order to pull this off, you'll need to take advantage of the not very well documented _context variable in Twig. This should do it: {% set language = 'fr' %} {% set foobar = _context[language ~ "_foobar"] is defined ? _context[language ~ "_foobar"] : null %} {% if foobar %} {{ foobar.globalAddress }} {% endif %}


2

I'd suggest a better approach as detailed here: https://experiencehq.net/blog/craft-with-markdown-and-matrix Once you go down the "structured content" route you'll never look back, I promise you :)


2

Well this feels like an awful kludge but at least I have it working with a finite number of image variables in the code below. There has to be a more elegant and flexible way to do this. And Natetronn, if your proposal does just that, perhaps you could help me see how. Otherwise, other cleverer folks, do let me know. {% for subsection in entry.subsections ...


2

This is partially side stepping your question but, it could be a solid option just the same: You might consider checking out Selvin's Doxter Markdown plugin. It has support for Entries, Assets, Users, Tags, Globals and Categories. This all via reference codes which can be copied and pasted into the Markdown as needed.


2

You can concatenate variables like this: {% set foo = 'foo' %} {% set bar = 'bar' %} {% set foobar = 'Hello' ~ foo ~ bar %} {{ foobar }} #Outputs Hello foobar It's explained by Brandon here.


2

{{ entry.section }} - {{ entry.title }} Here are a list of properties you can get from the Entry model: https://craftcms.com/docs/templating/entrymodel So you could do {% set title = "#{entry.section} | #{entry.title}" %}


2

The most straightforward way would be to use a Categories field. The value that a Categories field returns to the template is an ElementCriteriaModel, which fits perfectly as a value for the .relatedTo parameter. So you could do something like: {% set featuredEntries = craft.entries.relatedTo( entry.selectedCategories ) %} The only circumstance where I ...


2

You can't just pass a string (category name) to relatedTo, you need to pass either the category ID or a complete CategoryModel (you can also pass in an array of IDs or models, if there are several categories). This means that unless you already have the category IDs or models, you'll need to pull them using the craft.categories service: {% set management = ...


2

Craft’s |group filter can be used here. I’m going to assume that the Categories custom field on your Exhibitor entries is called myCategoriesField. {% set groupedListings = entry.myEntriesField.find()|group('myCategoriesField.first.title') That will give you an array of grouped Exhibitor entries, grouped by their related category titles. For example, it ...


1

Yeah, I think you are close, you've just go your variables a bit mixed up. You set entries, but then reference entry and I think getUrl should be called as a function getUrl() as it's not a property (it might work that way, I've never tried tbh) {% set entry = craft.entries.slug('homepage') %} {% for image in entry.podcastItunesImage %} <itunes:image ...


1

i've got now a working approach to my question above: {% set field = craft.fields.getFieldByHandle('jahrgang') %} <form action="tourdefrance.html" method="post"> <select class="dropdown" name="modelljahr" size="1" onchange="this.form.submit();"> {% for opt in field.settings.options %} <option name="fields[myFieldHandle][]" ...


1

Looks to me like your solution is mostly correct, except for handling the gamesCatalogNumber. For that you could either: test for specific publisher(s) by id or slug test if gamesCatalogNumber is empty on the first entry (and assume that the publisher doesn't use catalog numbers), or (recommended solution) add a checkbox or lightSwitch field to the ...


1

Your first line must work. But there is a typo with double )) {% set entry = craft.entries.id(43).first() %} {{ entry.title}} But normally when you add a template for a specific single you doesn't have to set the entry.


Only top voted, non community-wiki answers of a minimum length are eligible