Questions tagged [templating]

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

Filter by
Sorted by
Tagged with
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 ...
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 ...
22
votes
4answers
10k views

Access environment information in template

Is there a way to check what environment craft is running on - ie, dev or not dev - via the template. For example: {%if env is 'dev'%} hello dev {% else %} hello world {% endif %}
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 ...
20
votes
3answers
15k views

How can I check on the value of a template variable?

If I'm used to PHP's var_dump() and print_r() to check the value of a variable, how can I evolve to inspect things in Craft where Twig's dump isn't available?
19
votes
5answers
17k views

Create line breaks from text field

I'm looking for a way to output line break from a text field. I'm aware of the "Allow line breaks" option and I have set that to true. I've then entered text with multiple new lines. However, when ...
19
votes
1answer
7k views

How do I output the locale’s name?

When I output {{ craft.locale }} in my template, I get the current locale’s ID (e.g. “en”) rather than its name (e.g. “English”). How do I get the locale’s name?
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. ...
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') %} ...
17
votes
1answer
6k views

Section entries - order by random

I was wondering if it's was possible to order section entries by random. Example: {% for entry in craft.entries.section('team').type('mainPeople').order('title random)) %}
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 ...
17
votes
1answer
7k views

How do you list categories in a category group?

I would like to output a list of all the categories in a category group on the front end. How do I do that?
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 ...
16
votes
4answers
6k views

Best method of text truncating

I see that there are Trimmer and Truncate out there that have been created for truncating text but I don't believe either does what it is I'm looking for. I'd like to truncate text similar to how ...
15
votes
3answers
4k views

Twig syntax highlighting in PHPStorm?

Does anyone know of a way to instruct PHPStorm to treat Craft templates as Twig files, without globally assigning all .html files to be interpreted as Twig? Since Craft templates don't use the .twig ...
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 ...
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?
14
votes
1answer
2k views

A/B testing - how to in Craft?

What's your process for A/B testing a Craft entry? I'm curious about how you'd structure alternative content for a page within Craft and what tools you'd use to measure the results. Any comments ...
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(/([*][*...
13
votes
3answers
81k views

How does the negative (not) conditional work in Twig?

If I write: {% if entry.section not 'Homepage' %}Blabla {% endif %} this results in the error: Unexpected token "operator" of value "not" ("end of statement block" expected) But if I write: ...
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?
13
votes
1answer
1k views

The Definitive Guide to Extending Twig in Craft CMS

So Craft's implementation of Twig is already very powerful and suitable to most common tasks. The Craft Docs already provide a great intro into using Twig and the handy extensions provided by Craft: ...
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 ...
12
votes
4answers
46k views

How to check variable type?

How do I check what type my variable of (string, integer, boolean, array, dictionary, etc)?
12
votes
1answer
7k views

How to get one random entry from among multiple sections

I'm trying to pull a random entry from among an array of sections, but I can't get it to work. I tried the code below, but I'm just getting errors. I've tried multiple ways of listing the sections -- ...
12
votes
1answer
2k views

Macros vs Includes

Is there any guidance or pitfalls/benefits of using one over another? I see that includes can be done more or less the same way a Macro can be done (certainly for simple variable changes). An example ...
12
votes
2answers
2k views

What is the best way to handle errors in plugins and templates?

What's the best way to prevent exceptions in my plugin from blowing up my entire page? try/catch and return null, and then check for null in the template? For example: my plugin fetches data from a ...
12
votes
2answers
12k views

How to output a field as raw HTML?

I have a field in a matrix block that is used for video embed codes. I want to output the contents of that field directly as HTML. However, it always seems to get entity-encoded (ie. the < and > ...
12
votes
1answer
2k views

How can I output the Field Name in my template?

I've created a plain text-field and added this field into a section. I then created an entry and filled it with content. I know I can output the content of the field in my template with {{ entry....
11
votes
4answers
6k views

Can I disable template caching for local development?

Can I disable template caching for local development? For example: setting a environmentVariable in a config file? Or is this possible in the future?
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. ...
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 ...
11
votes
5answers
29k views

Include a template that sets variables (global template variables)

I'm essentially looking for a way to have a template that runs through a bunch of sets that I might use throughout my templates. The problem is, if I include in a template with an extends, it dies up ...
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 ...
11
votes
2answers
3k views

Templates with .twig extension

Does anyone know how to make Craft by default use .twig extensions. It helps IDEs better understand Twig blocks. I have tried .twig and .html.twig but it doesn't appear to work by default. I don't ...
11
votes
1answer
2k views

What's the difference between using single and double quotes in strings?

Is there a difference between using single-quoted and double-quoted strings?
11
votes
1answer
6k views

How do I sort entries by two variables?

Currently I am sorting the entries by an ID: {% for gallery in craft.entries.section('Gallery').order('available desc').limit(none) %} but I would like to show the latest entries on top, currently ...
11
votes
1answer
2k views

Check to see if category is populated?

I want to list all categories that have an entry assigned to them, but hide the ones that are empty. In EE I could do this show_empty="no" But I can't see a parameter in the docs for Craft. ...
11
votes
2answers
10k views

Display entries that belong to a category

I have a page that should show entries within a category, and then entries within children of that category. Basically, I need a way to loop through entries that have a level 1 category but not a ...
11
votes
1answer
487 views

How can I utilize existing UI for my plugin's control panel views?

I'm new to building plugins and I need to make templates for the control panel. How can I use existing Craft UI components to keep things consistent and save myself from having to rebuild everything?
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 ...
10
votes
1answer
2k views

System Status Off Custom Text

I'd like a more personalized version of the offline page when the System Status is set to: Off. Is there a way to customize the page and/or change the text?
10
votes
2answers
5k views

How can I convert text into a URL-friendly string?

If I want to convert text into a URL friendly string for sharing text in a social media link, how would I go about that?
9
votes
4answers
7k views

Remove the last comma in an inline list of tags?

In a list of tags I'd like to remove the last comma. So is there something that works like the Ee backspace parameter?
9
votes
4answers
15k views

Whitespace created when using if statements

I am using the following block of code in a template: <hr class="rule"> {% if entry.features | length %} <div class="feature-block"> <h4 class="is-uppercase">Features&...
9
votes
2answers
12k views

Is it possible to break out of a loop in twig?

Is it possible to break out of a loop in twig? If it is, how would I do it?
9
votes
3answers
3k views

Approaching Google AMP

Does anyone have any thoughts on automating the implementation Google's new AMP article markup? https://www.ampproject.org/docs/get_started/create/basic_markup.html Also see: Google will start ...
9
votes
2answers
512 views

Why does my base template never reload?

I use template inheritance: In _layout.html every other template is loaded with {% block content %}{% endblock %} The problem: _layout.html never reloads. I changed the CSS path in the head tag of ...
9
votes
3answers
6k views

Check to See if Field Exists

When looping through a matrix, is there aquick way to check whether or not the block has a particular field (not if the field is empty, but if the field exists at all). I have a matrix with ~20 block ...
9
votes
3answers
3k views

Creating partials outside of the templates folder

I'm interested in building a highly modular Craft site in which my folder structure might look something like this: - craft/ - modules/ - hero-banner/ index.html scripts.js styles....

1
2 3 4 5
32