Hot answers tagged

5 votes
Accepted

SEO titles/descriptions for tags and archive pages

The better approach in my opinion is to declare the variables in the corresponding templates rather than in the _layout template Layout.twig {% if seoTitle is not defined or seoTitle is empty %} ...
5 votes
Accepted

Trying to achieve a row column layout with batch

I think you're getting your variable names mixed up. Something like this should work: {% for row in entry.content1|batch(2) %} <div class="row"> {% for block in row %} <div class="col-...
5 votes
Accepted

Recommended Structure - One Site with Pages With Totally Different Layouts?

Option 1 is a safe bet. This allows you to have completely separate, editable templates for each chosen year. You can then change the page slug to match the year of that page. You can reuse fields ...
  • 76
4 votes

Send a block... into a block?

It seems to be what you need is Twigs extend functionality. What you have here is a very common template structure so there is nothing out of the ordinary! Firstly, going from your code I'd recommend ...
  • 3,604
4 votes
Accepted

Determine a layout based on column number

How about we have a matrix, with block types x and y. Then you can test for block type in your loop: {% for block in entry.this %} {% switch block.type %} {% case 'x' %} ... {% case 'y' %} ...
3 votes
Accepted

Ajax layout sample

You're not specific about what your Ajax template should return (HTML, JSON...?) – but assuming you want your payload in JSON format, here's a basic layout example which will 1) test if the current ...
3 votes
Accepted

Send a block... into a block?

Unfortunately, there's no way around having the {% block %} tag somewhere in the template that's being extended. In other words, your main layout template it's going to have {% block %} tags. However,...
3 votes

Output a Twig tag within another Twig tag

You are hashing the string '{{item.mailto}}'. There shouldn't be any need for the quotes or second set of curly braces. Twig does not permit curly braces inside curly braces. Try {{ item.mailto|hash ...
  • 735
2 votes

Determine a layout based on column number

Instead of building rows, how about building your layout with inline-block elements or flexbox elements with the flex-wrap property set, to make your elements wrap automatically.
  • 35.9k
2 votes

How does one achieve drop-left or drop-right image position?

Generally, I'll have a div that picks up on the position's value. For example: <div class="image-{{ entry.myPositionHandle }}"> <img src="my-image.jpg"> </div> That renders ...
  • 4,696
2 votes

Re-usable Field Layouts

No, unfortunately there's no way to re-use a Field Layout in Craft. You might want to consider adding a feature request at http://feedback.buildwithcraft.com.
2 votes

What are best practices for using Matrix fields to establish a grid layout

Easiest way I can think of is just to use some math. If your grid columns need to add to 12, keep a current column count while iterating over the matrix blocks. I'd imagine you'd want your content ...
  • 6,502
2 votes
Accepted

Using Structures instead of Singles

I'm doing this at the moment. It's not a perfect solution, but nor is it terrible. In a lot of ways, I'd quite like Craft to just burn Singles and Channels and use nothing but Structures. You can do ...
  • 3,084
2 votes
Accepted

Craft Neo – childs Output

Your above code is not working because you are using "block" instead of "item" in your children. So... {% for item in block.children %} {% switch item.type.handle %} You don't necessarily need ...
  • 1,517
2 votes
Accepted

How to handle client expectations regarding page building and layout within Craft

This question comes up in support tickets every so often from client users. We like to say that Craft fills the space between where WordPress ends and Enterprise begins. It's meant for designers and ...
2 votes
Accepted

How to slightly alter _layout for one specific page?

Try putting it inside of a simple block as the default value... {% block contactButton %} {% include '_contact-button' %} {% endblock %} Then on your one-off page, override that block with ...
  • 23.6k
2 votes
Accepted

Exclude a block from appearing on a specific page

You can check against the Entry Type as well. I would try to validate it with something like: {% set exclusions = ['home'] %} {% if entry.type not in exclusions %} ....
2 votes
Accepted

Custom Field layouts

Check out this repository for an example of Matrix Façades which does exactly what you're looking for (article forthcoming)
  • 11.1k
2 votes
Accepted

pass data to _layout?

Are you on Craft 3 or 2? In 3, you can use the built-in {% js %} ... {% endjs %} tag to inject scripts into tags named {{ endBody() }} or {{ beginBody() }} that you place wherever you like in your ...
  • 4,059
1 vote

How to dispatch equally entries from different channels?

You can use one entry query to fetch entries from multiple sections: {% set entries = craft.entries() .section(['channel_one', 'channel_two']) .orderBy('postDate DESC') .limit(9) .all()...
  • 6,925
1 vote

Whats crafts best practice when having to reuse fields

Sounds like you're looking for Matrix Fields. A matrix field allows you to create repeatable content blocks of varying types. This is different from a simple table (repeater) field since you can ...
  • 6,925
1 vote

Split entries in columns

You'll need to loop through all entries first and divide the entries into three arrays. Then using a combination of id and fixedOrder you can create three more entries calls to loop through each ...
1 vote

Split entries in columns

There's no easy twig function available to do this, but the code from this answer should be able to help you. {% set columns = 3 %} {% for entry in entries %} {% if loop.first or loop.index0 is ...
  • 171
1 vote

Copy the Entry Type along with its field layout

I've had this issue previously before when working on a larger website, when you want to copy an entry type and all its field layouts, or even copy a section all its entry types and all their field ...
1 vote

Copy the Entry Type along with its field layout

After hours I managed to fix it. I'm assuming I made an error where I was trying to copy the Fields in the Entry Type I literally just created. So it was copying over 0 fields. I built the functions ...
1 vote

Craft Neo – childs Output

You must call the inner blocks (level2, level3 and so on) like the first level {% if entry.bodyMain is defined and entry.bodyMain | length %} {% nav block in entry.bodyMain.level(1) %} {% ...
  • 119
1 vote

What are best practices for using Matrix fields to establish a grid layout

You could use the approach you already suggested, but combine it with a grid that doesn't require to close rows, but automatically wrap into the next row if there's not enough space left. There's two ...
  • 35.9k
1 vote

Is it possible to load different "_layouts" with ajax for certain pages?

Although you couldn't necessarily change the _layout template with the method given in that tutorial. What you could do is include the header the templates rather than in the _layout.html for each ...

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