13

block.type is actually an alias for a getType() function, which returns a new MatrixBlockTypeModel object. Those will output their handle by default, which is why it's possible to compare block.type against block type handles. But MatrixBlockTypeModel also has a handle property, so this is totally possible: {{ block.type.handle }}


6

The guys at Supercool have developed a Craft plugin that does exactly what I was after, and as a bonus it allows you to group matrix block types which is very handy. UPDATE As of Craft 2.3, matrix block titles are now shown as part of the core functionality, thanks P&T!


6

There is the Ace Freely plugin, which adds an Ace Editor Field Type that would work well in a Matrix field.


5

I just published a plugin called Limit Block Type, which adds the ability to limit Matrix blocks per block type. There's also a feature request for adding this feature to core.


5

Correct - currently Matrix fields are not global, so you'll need to recreate them if you want to use them in other Matrix blocks.


5

What about comparing the currently looped block ID with the ID of the last block of type "text"? _entry.html: {% set lastTextBlock = entry.simpleMatrix.type('text').last() %} {% set lastTextBlockId = lastTextBlock ? lastTextBlock.id : null %} {% for block in entry.contentBlocks %} {% include '_blocks/' ~ block.type|lower %} {% endfor %} _blocks/text....


4

Matrix Colors plugin for Craft CMS would help. It lets you assign a different color for each block type.


4

On Straight up Craft we use a Doxter Block for Markdown and Rainbow.js with a custom Craft-Twig extension for syntax highlighting. Doxter allows you to identify the code block like so: ``` twig {{ siteUrl }} ``` or ``` php <?php echo "hello world"; ?> ``` And you can view the syntax in highlighting in the CP before you publish using Live Preview.


4

Unfortunately this is a limitation of that feature. In this case, both of your Lightswitch fields will be tweaking the DB query with their own WHERE [field_column_name] = 1, without knowledge of each other. To avoid this, just give each of your Lightswitch fields their own unique handles. If you want to get all the blocks where either one of your ...


4

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 your main layout template file looks something like this. The idea here being that these elements will not change for each page, but the content or information ...


4

You can limit which blocks you want by type and only show the first of each, if they exist. Something like this might do the trick: {% for entry in craft.entries.section('blog').limit(3).find() %} <div class="col-8 {% if loop.last %}last{% endif %}"> {% set textBlock = entry.blogBody.type('text').first() %} {% set imgBlock = ...


3

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, you can use the {% embed %} tag to "wrap" a block in a third template (e.g. "components/main-wrapper.html"): {# base.html #} <body> {% block main %}...


3

I believe you're referring to Matrix block types. You can create additional block types for a Matrix field, or edit existing ones by going to that Matrix's field settings under Settings->Fields->(your matrix field) in the control panel.


3

I'd just put whatever you want to share into an include. Also you shouldn't need the .html either as craft should find the file


2

No. Matrix blocks do not work in that manner. However you define it in the field is how it'll show when you edit it. If it's really important not to show that block type you can write a plugin that checks for an entry type, and hide certain block types based on that result. Granted that's a lot of work for very little gain. You would be better off ignoring ...


2

It does not appear to be in the matrix block model. http://buildwithcraft.com/docs/templating/matrixblockmodel


2

What about using the length filter on a type filtered array of block models to get the total number of your "text" blocks. And then combine that with a counter in your block include to conditionally output the buttons? _entry.html: {% set lastTextBlock = entry.contentBlocks.type('text')|length %} {% set loopIndexText = 0 %} {% for block in entry....


2

Reading your description I see no reason to not use all three fields in a single matrix block. If you need additional control (e.g. for positioning the title), you could add another field to that block that allows you to set your settings (e.g. a dropdown with "Title left" / "Title right" options). If you really need a per block type limit (now), you could ...


2

You can write a custom plugin and make use of Craft's buildElementsQuery, to be able to add custom query statements to your ElementCriteriaModel. Give your plugin a Template Variable addVideoBlockCriteria and with some lines of plugin code you will be able to do something like this from your template: {% set criteria = craft.entries.section('blog').order('...


2

Bumping php.ini's max_input_vars to a higher value as described in the answer here fixed it.


2

To test if the current block is the last in the loop and of a certain type, you could do something like: {% if loop.last and block.type == "yourBlockType" %}yes{% endif %} or shorthand {{ loop.last and block.type == "yourBlockType" ? 'yes' : 'no' }} In the above example replace 'no' with '' to output nothing. It's just there if you want to output ...


2

I'm assuming that you're attempting to create the Matrix, with one or more blocks each containing one or more fields, from scratch? I.e. you're not simply trying to populate content into an already defined Matrix? Going off of that assumption: PS: Sorry, this code is not based on your code - I simply took working code from one of my other projects. // ...


2

I think you'd be better off with your Matrix field, entry.homepageSlider having only one block type called Slide. This block type would then contain the fields for each slide such as slideHead, slideText and slideImage. You would then loop through each block (slide) to output fields within the same slide. If certain fields are not required you would wrap ...


2

I reduced your code a little: You can use elseif to cycle trough each block so you don't have to write each if statement. Now where do you want your div? If you want each block with one separate div you write them inside the if statement (As I did in this example). {% for block in entry.homepageSlider %} <div class="da-slide"> {% if block....


2

Assuming you can have multiple person blocks per entry (group), you could: Fetch your entries and eager-load the blocks and their entry to save some overheads {% set group = craft .entries() .id([100,200,300]) .with(['people.person:entry']) .all() %} Then build an array with each person.entry {# The array to store your person.entry #} {% set ...


2

You'll want to implement something like this: {% for block in entry.menuBuilder %} {% set type = block.type.handle %} {% set isFirstConsecutiveBlockOfType = (loop.first or type != block.getPrev().type.handle) %} {% set isLastConsecutiveBlockOfType = (loop.last or type != block.getNext().type.handle) %} {% switch block.type %} {% case 'menuItem' %...


2

This isn't ideal and reviewing your markup might be a better alternative but you could use the loop variable provided by Twig's for (docs). For you example, something like this should work {# Inject this on the first iteration only #} {% if loop.first %} <div class="relative bg-gray-50 pt-16 pb-20 px-4 sm:px-6 lg:pt-24 lg:pb-28 lg:px-8"> &...


1

Turns out my issue is because I am running 2 loops through entry.timeline: A subset of the field with only subsection blocks to generate a table of contents The entire field contents with a mixture of both subsection and item block types. Craft 2 was totally OK with me defining a subset and then looping through the entire set, but with Craft 3, if the ...


1

This was a bug in a beta version of Craft 3 (fixed here: https://github.com/craftcms/cms/commit/9b6dad7385c5a82d1c83d230fe6ce0cfe940675b)


1

Image are returned as an array so could use either of the following: {{ content.img[0].url }} Or set a variable which fetches the asset to then output its fields {% set asset = content.img.first() %} {{ asset.url }} Related: Looping through images in a Asset field type UPDATE: The line {% if content.img|length %} will always be true because you are ...


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