1

I am building a new website using Craft CMS and I have a requirement from the design team that they want to be able to re-use templates across multiple sections. For example define how a product looks like and then be able to add a product from any sections.

I thought implementing this using a Matrix or having a channel/structure section with multiple entry types however I am bit unclear when to use entry types and when to use matrix or maybe there is better way to implement this and I am not aware?

4

I see this question as two parts: information architecture and templating.

If you're used to something like WordPress where the template has a one-to-one relationship with how the template gets data and how information is routed, ignore everything you've learned.

Templating

Let's get the requirement of reusing templates out of the way first. Twig, Craft's templating language, lets your reuse templates quite easily.

Not only can you use reuse templates from any page inside Craft, you can also extend templates from other templates.

Craft then lets you define which template is going to be used for each section. You could just set it to a single one and be done with it if your content doesn't change that much from section to section. A single Matrix field could pull off quite a bit across the entire site. Speaking of...

Matrix

Matrix fields can be used for "site builder" like functionality. Matrix lets you define block types (eg Pullquote, Featured Image with Caption, etc) and then by mixing and matching the blocks, you can "build up" a page. The blocks can also be reordered. That gives authors a lot of flexibility in how they layout the pages.

Entry Types

Entry types define what content Sections contain but unless you define the same Entry Types across multiple Sections, they're not "global" like Matrix fields would be.

Entry Types are useful if you need to define content (and layout) requirements for entries in the same section. If you curate a blog for example, you might have a "Link" Entry Type that links to other entries or even other blogs. Then you might have a "Blog Article" Entry Type for "regular" content.

In your templates, you have the ability to check which Entry Type the entry happens to be and have your template format it how it should look. The Happy Lager does this inside the News section.

Information Architecture

In my experience, there aren't any hard and fast rules for using Matrix and Entry types, it really depends on your content and requirements. You could probably pull off what you're asking for with either.

  • If your templates are "designed", meaning the products are always going to live in a certain spot inside a template, then you can probably use a relational field to define the relationship.

  • If you want your authors to be able to define multiple elements on a page and then pull in the products from anywhere, Matrix is going to be your weapon of choice

Mijingo just introduced a course you might find useful on the subject as well.

  • I'd add that it's probably easier to target a certain field in a fixed template than it would be in a matrix field. If you want to grab all of a certain thing—like all "Product Sheets" in section—I think it'd be easier to target the field directly than to have to look through each block in each entry. – Ian DeRanieri Aug 3 '16 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.