Questions tagged [templating]

Front-end Twig templates that can Craftily render markup, scripts, and styles.

Filter by
Sorted by
Tagged with
17
votes
4answers
5k views

Is it possible to use Twig in a Rich Text Field?

I have global variables that I want to include in Rich Text Fields, for example: This is a rich text field, call us today at at {{ phone.number }}! This doesn't work out-of-the-box, are there any ...
11
votes
1answer
7k views

How to display all fields belonging to current entry?

How do I modify a template to include a loop that displays all custom fields that I added to the field group that the current entry belongs through? I know how to display the fields individually by ...
23
votes
3answers
4k views

How can I route a specific entry in a structure section to its own template?

While setting up a site recently I wanted to route a specific entry in a structure section to its own custom template. Using a dynamic route sounded like the right option until I remembered that it ...
29
votes
4answers
18k views

Test for empty field?

How do I check if a field is blank? I thought it was something like {% if article.articleImage is defined %} Which in this case is testing an Assets field (and seems to work). Tried the same for a ...
12
votes
4answers
10k views

Check user is in User Group / Custom User Permissions

I'm trying to restrict access to items in a channel using some form of custom user permissions however I don't appear to be able to create or set custom read permissions for a channel using crafts ...
13
votes
2answers
9k views

What's the difference between “is defined” and “is not null”?

{% if var is defined %} and {% if var is not null %} Is there a difference in usage of them?
9
votes
1answer
1k views

Two Plugins not showing in CP after pushing to staging server

Two different plugins not showing in the CP after code was pushed to staging server. My questions: 1) Why wouldn't the plugins show in CP if plugins are present in the craft_plugins table? 2) How ...
11
votes
5answers
8k views

How do you handle complex navigation in Craft?

This seriously can't be this hard. How are you handling complex navigation on your sites? Do you hard-code them? Do you use a dedicated "Menu" Structure Section? I am curious what everyone is doing. ...
14
votes
2answers
15k views

Can we use PHP in our templates instead of Twig?

Is it possible to use strait PHP in our templates along side of Twig and or instead of Twig? If so how does one go about it?
18
votes
1answer
11k views

How can I determine if a user is logged in, and who that user is?

I'm running some checks to see if there is a user logged in, and if that user is someone specific. When accessing user.isCurrent it's returning 1 when I'm logged in, but returns null when I'm not. ...
8
votes
5answers
3k views

Twig “is defined” always returning true

I am trying to code defensively and check whether a field exists before accessing it: {% if e.someField is defined %} {{e.someField}} {% endif %} And I get a CException Craft\EntryModel and its ...
22
votes
1answer
2k views

How does template caching work?

The {% cache %} tag is quite powerful, and can be used to greatly increase the performance of templates. But ultimately the whole thing is still pretty mysterious. I can see three tables in the ...
15
votes
1answer
12k views

Get the current section handle

I can't help but think this is a silly/obvious question, but I'm trying to retrieve the current section handle so I can use it as a class. Maybe I'm going about it wrong, but {{ handle }} does ...
8
votes
2answers
783 views

Can I apply an image transformation to an arbitrary image (non-Assets field)?

Curious if I'm able to use the resizer on a random, sourced image, not coming from a field anywhere. So I would just be passing a string to the path of the image.
6
votes
1answer
1k views

Is it possible to view 'Expired' entrys on the front end?

I have a site with 10000 events which need to expire once they have happened, the main reason for this is to keep things tidy in the CP. We need users to view events that they have completed after ...
5
votes
1answer
3k views

Merge two arrays of ElementModels?

I have two queries on related entries (using Many to Many plugin) to find both related and then reverse related entries: {% set relatedArticles = article.relatedArticles %} {% set ...
10
votes
1answer
3k views

How to use plugin settings

I am learning craft and trying add settings page for my plugin. I read that need to define settings like, class CocktailRecipesPlugin extends BasePlugin { // ... protected function ...
5
votes
1answer
4k views

Image asset url is not being output

After going through the documentation I believe that this is the correct code to output the url for the asset field but its not working and I have no idea why. Please help! {% nav program in craft....
4
votes
1answer
3k views

Serving CSS files from template folder

Is there a way to serve my CSS files from the templates folder? I am wanting to be able to use TWIG tags, etc to build some of this. And even if the TWIG tag idea does not work out, I would prefer ...
3
votes
2answers
2k views

Is it possible to route a subdomain to specific template group?

We are looking for a way to route a subdomain to a specific template to use as a landing page. Is there a way to do this without routing through index.html and redirecting to the specified template ...
2
votes
1answer
583 views

How to get the previous or next two entries

I have an entry which needs to display the previous two entries, and the next two. I see in the docs there's a nextSiblingOf method. That does nothing in a channel, and in a structure it will only ...
2
votes
2answers
2k views

Channel index page linked in a structure

I come from expression engine and what I want to do is the following: I have a channel filled with entries. All I want to do is to list all these entries on a page. However, I don't want to link ...
9
votes
2answers
5k views

Looping through images in a Asset field type

Sorry, this is kind of a newbie question, but I can't figure out how to loop trough all images in one image asset field supporting multiple images. The only way I have been able to output anything ...
3
votes
2answers
2k views

How to show entries which are live between dates regardless of postdate or expiry dates?

I'm trying to show live entries to user by selecting list of months in a dropdown. Some my entries are live for 2 or 3 months. I having trouble in finding right method. I have tried using after and ...
2
votes
1answer
571 views

Getting a Single

It appears that getting a entry that is a single is more tricky that it should be. This works, craft.entries.section('footer').first() This does not work, craft.entries.section('footer').limit(1) ...
1
vote
2answers
465 views

Why is the Craft without filter not working correctly and how to debug?

I am using the Dukt Follow plugin to return 2 arrays which both hold User objects, I am then using the without filter to remove users from the array which are considered friends. Friends are ...
16
votes
4answers
7k views

How do you handle SEO on your site?

How do you guys handle SEO? Do you just make your own meta fields and add them in the template manually or am I missing something? I did notice there are some plugins being developed but there are ...
11
votes
3answers
2k views

How do I set up event listing & detail pages with entry post dates that are in the future?

I'm struggling to set up a listing template and a detail template which will display "event" entries with post date set in the future. I keep getting a 404 error on the detail page. How do I set up ...
17
votes
7answers
8k views

How to write a simple navigation?

After endless trial and error I figured out the right syntax for a simple one level navigation. {% set navi = craft.entries.section('not projekteintrag' ) %} {% for entry in navi.order('id asc') %} ...
9
votes
2answers
2k views

How do I add an HTML block from template extension to main layout template? (i.e. meta data)

I have a main layout file and extending it with another template. I have my markup and content in the {% block content %}{% endblock %} however, I'd like to add content to another "block" on the ...
4
votes
1answer
2k views

Is it possible to change a user's password in the front-end?

Is it currently possible to change a user's password in for example the profile edit form? I'm currently using the forgot password approach for this.
3
votes
1answer
1k views

Show a table field's column headers in template?

The question says it all. I have: A Matrix field, with A subfield with the type table Two columns within that table of which I want to show the column headers in my template Is that possible and if ...
9
votes
2answers
29k views

Is there a way to define a global var within a twig block?

Something that would work like this: {% block A %} {% set testVar = "foo" %} {% endblock %} ... {% block B %} {{ testVar }} {% endblock %} The scope of testVar seems limited to {% block A %} I'...
7
votes
1answer
2k views

Sorting entries A-Z, ignoring 'The' and grouping by first letter

Probably going to be an easy win for someone here, but being able to do this is currently eluding me. So: I'm trying to output a list of Artist names grouped by first letter but need to ignore 'The ' ...
6
votes
2answers
3k views

Calling variables, dynamically

I've got a parent template which contains the following code: {% include '_layout' with {'countryCode': 'au'} %} I have a global set with the handle au_Variables, and within that a field with the ...
6
votes
2answers
3k views

Is it possible to access Twig variables in a plugin variable using PHP?

I have a controller that returns route variables to the page: craft()->urlManager->setRouteVariables(array( 'errors' => $entry->getErrors() )); On the page that gets loaded, I can ...
5
votes
2answers
20k views

Using Twig to merge an array and remove duplicates

Problem I'm building a .json file for a search component of a used cars website. The file needs to be built from the output of two sections within CraftCMS, Used Cars and Vans & Trucks. It ...
4
votes
3answers
5k views

How can I make a template output its own filename?

Is it possible to output the template name being used? Using a global variable or something similar? I am wanting to check which template is being used in a conditional.
4
votes
1answer
211 views

Reusing logic to set string variables

I would like to set some variables for every entry within a section ('courses'), and then use those variables in loops that are filtering by entry types and categories. Some simplified markup: <...
3
votes
1answer
774 views

How to load a different template for parent page

So, I'm newer to Craft, and so I need to figure out the best way to set up a portfolio page with child entries. I've set up a structure, but I need the parent page to load a different template than ...
13
votes
2answers
45k views

How can I perform a RegEx match in Twig?

Is it possible to perform a RegEx match on a string in Twig? I'm looking for something like this: {% set string = "This is **string**. **wer** qewr **234**" %} {% set match = string.matches(/([*][*...
7
votes
1answer
674 views

How can I use operators within Twig hashes?

I'm looking for documentation on how to use operators in hashes in Twig. Someone mentioned this in the Google+ support group the other day and I was unable to find any docs or examples. {% set ...
4
votes
1answer
1k views

Edit a table field type from the front end

Similar to this question, I'm trying to make a table field type editable from a front end form. When I update from the CP, the changes are reflected on the front end, but the following code doesn't ...
4
votes
1answer
1k views

Get next entry based on entry type

As an extension of this question How to link to previous/next entries in a section? If my page is set to automatically provide a entry var in my template. And I can simply access a field like this ...
3
votes
2answers
476 views

How can I select users that are not members of any user groups?

I'm struggling to find a way to use craft.users to select all users that are not members of any groups. Looking at the User Element Type both the groupId and group parameters are used to perform ...
1
vote
1answer
1k views

How do I get the column headers of a table in a matrix?

I'm trying to write a general purpose matrix template and I've come up on a little snag with tables fields. I see no way to get the column header values. The answer here mentions craft.fields....
4
votes
2answers
2k views

Can you get the HTTP status code in twig templates?

I need a nice way to determine whether I'm on a 200 page. Basically anything other than a status page (404, 500, 403) in twig templates. I thought I could do something like craft.request.getStatus or ...
3
votes
4answers
5k views

Is there an easy-to-follow tutorial to learn Craft CMS basics?

I am thinking about using Craft CMS for building multiple websites for my organization. A friend recommended Craft CMS, but I am finding it very hard to get started, unlike GRAV, which was very easy ...
1
vote
2answers
999 views

How do I select a specific product variant by element/field/entry?

Excuse my new-ness, but I'm having an issue with product variants. Scenario I have a product with multiple variants (up to 30 in fact). Based on the URL, I want to choose a specific variant so that ...
7
votes
1answer
2k views

“A” or “B” in switch statements?

Is it possible to provide different possible values per case? I've tried the following formats without success. {% case "A" or "B" or "C" %} {# run this if case matches A or B or C #} I saw this ...