47 votes
Accepted

What’s the recommended way to set the site URL?

As of Craft 2, you should be using the new siteUrl config setting. There are two advantages to setting your site URL using this config setting. First, The value can be set on a per-locale basis, in ...
user avatar
19 votes
Accepted

Macros vs Includes

Fabien Potencier, the main developer of the Twig templating language answered the question like so: Macros: reusable markup across a lot of templates Includes: part of "pages" that are ...
user avatar
  • 35.8k
9 votes
Accepted

How to handle top-level/index pages in structure sections?

I recently setup a structure which contains (almost) every page of my site. When I set it up, I specified only {slug} as the URL. So far, it's working fantastically! And that's with about 30-40 pages ...
user avatar
  • 23.6k
9 votes
Accepted

One structure vs a structure per section?

TL;DR: Let the content dictate how you model and architect it. If the site is sufficiently small or sufficiently simple, I think either approach is ok. My team has used both approaches, and it really ...
user avatar
8 votes
Accepted

When to use "if" or "without"?

For your example, you should exclude the fictional towns in the initial query itself rather than filtering them out afterwards: {% set towns = craft.entries.section('towns').townFictional('not 1')....
user avatar
  • 4,361
8 votes
Accepted

Which is best practice, SET or FOR?

Regardless of the number of elements involved, Craft will return an iterable ElementCriteriaModel whenever you query for content (e.g. via craft.entries, or using relational FieldTypes like Assets, ...
user avatar
7 votes

Best practice for channels and structures

You basically have two options for creating a "listing" or "archive" view for your Channel or Structure sections. The first option is to create a Single. This is in many cases appropriate, e.g. if ...
user avatar
6 votes
Accepted

Many db requests with relationship fields

This is how relationships work in Craft at the moment. Each relation equals +1 query. If you don't access your relation on an entry the query isn't executed. So by only fetching title and/or id you ...
user avatar
  • 2,824
6 votes

Modelling flexible content sections and recommendations

I'm not familiar with ACF, but if you're looking for blocks-within-blocks, you could use the Neo plugin/fieldType. It allows you to define blocks, and then have nested blocks. It allows for decent ...
user avatar
6 votes
Accepted

What are «Structures»?

A structure is a section that allows for a 'tree' like layout with hierarchy. A good example of where Structures might be used for instance is the Craft documentation pages, or building a navigation ...
user avatar
6 votes
Accepted

What are the best-practices for useful logging messages?

If you're throwing Exceptions from your plugin, then those will automatically get logged in Craft's log files at craft/storage/runtime/logs with full stack trace information, so there is no need for ...
user avatar
  • 66.4k
6 votes
Accepted

Scaling channels with 35 locales

You could have a single News section that is enabled for each of the locales, and choose which locale each entry should target from their Edit pages. To make it easer on the authors, you can set the ...
user avatar
6 votes
Accepted

What are the best practices for the development setup?

That's the setup we use here. It's ok, but local development runs slowly because the database is off-site - we'll be moving to an internal database server in the new year because of that. Image ...
user avatar
  • 3,084
5 votes
Accepted

Sections, Categories, and EntryTypes. Which, when?

Sections are for completely separate areas of content. They have their own URLs, and are conceptually different. Like blog posts and press releases would be different sections. Your sections could ...
user avatar
5 votes
Accepted

Should my controllers actions 'return' when rendering templates?

There’s no reason for your controller to return anything – it will never be checked. Many requests call more than one controller action, so it would be pretty presumptuous for Craft to think that it ...
user avatar
5 votes

Which is best practice, SET or FOR?

Both are essentially the same query, the only thing that differs is the WHERE statement. The ID approach will always be the most efficient, since it uses the primary key index, thus finding the result ...
user avatar
  • 396
5 votes
Accepted

Grouping entries by category - most efficient approach

One improvement I think could be made is to fetch only the ID's of your hotTubs entries at the start of your code. You're only using this to get the related categories and fetching ID's is faster than ...
user avatar
  • 1,456
5 votes

Best practice for naming fields shared across entries in CraftCMS

The job of naming something is sometimes the most difficult part of a project! This is pretty subjective, but even though it seems like those three fields would share the same settings and ...
user avatar
4 votes

What would be the best way to build an Ad rotator?

I built a plugin which does exactly this... Ad Wizard. You can put your ads into groups, and add that group to your template like this: {{ craft.adWizard.position('rightSidebar') }} That code will ...
user avatar
  • 23.6k
4 votes

Populating an RSS feed with full post content?

I would use a lead paragraph field that you could then call into the RSS feed. Or you could add a post summary field. You could then call that whenever you need to, RSS or otherwise.
user avatar
4 votes
Accepted

Enable the user to upload background Image

You can simply add it inline to your body tag like so: <body style="background-image: url({{ myGlobal.myAssetsField.first().url }})"> If you want to only use it with selected templates, you'd ...
user avatar
  • 35.8k
4 votes

What are the best practices for the development setup?

Matt Wilcox alluded to it in his original comment, but something like Craft-Scripts will really help out with this workflow. You can read more here: Database & Asset Syncing Between Environments ...
user avatar
  • 10.9k
4 votes

What is the best way to deploy a Craft CMS 3 site?

At the risk of offering a "link answer", the Setting up a New Craft CMS 3 Project has a section on DEPLOYING CRAFT CMS that should be useful to you. I've never heard of Rocketeer, but popular ways to ...
user avatar
  • 10.9k
4 votes
Accepted

Get entry associated with current uri

Surprisingly, the Craft docs aren't super clear on this so I'll post a little info. When you set up a Section in the Control Panel, you set the URI Format and Template fields to map to an entry ...
user avatar
3 votes
Accepted

What's best practice for creating an index page for Channels?

If you are using a Structure to manage your site hierarchy, and the news index page naturally falls in that hierarchy, then yes, this can work well. Make an entry type for the index page, and in your ...
user avatar
3 votes
Accepted

Is it possible to Filter entry types via the URL?

Yes, it is possible. Pass the entry type handle to a segment and access that segment using the craft.request getSegment() method. Use this segment variable with the craft.entries .type() method to ...
user avatar
3 votes
Accepted

What's are some best practices for building a site structure with multiple locations?

A different News Channel for each location: For example: News Location 1 with Entry URL Format location-1/news/{slug} News Location 2 with Entry URL Format location-2/news/{slug}
user avatar
3 votes

What would be the best way to build an Ad rotator?

You build this in a couple different ways but the simplest would be to setup ads as as channel with an asset for the add, url etc. In the template you could set the ad [the entry] to appear randomly ...
user avatar
  • 1,685
3 votes
Accepted

How do I set up a section for pages with very flexible content?

I would use a matrix field with multiple types.
user avatar
3 votes
Accepted

How should I include my custom enums class in my plugin?

I suppose it depends on what the code is going to be doing. If it's standard "business logic" code related to your plugin, then it would make more sense to add it to your plugin's services, which is ...
user avatar
  • 66.4k

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