Skip to content

Instantly share code, notes, and snippets.

@Natetronn
Last active November 23, 2016 07:34
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Natetronn/7ee8ee1f8788702d06cb to your computer and use it in GitHub Desktop.
Save Natetronn/7ee8ee1f8788702d06cb to your computer and use it in GitHub Desktop.
Craft Grid System

Craft Grid System

It would be great to have a Grid System (core or plugin) which worked with all fields including the Matrix.

This would be a wrapper of sorts which would allow for the setup of Grid Systems which are available in all the popular frameworks (Foundation, Bootstrap, Gumby) as well as stand alone grid systems libraries and those which people have created on their own etc.

Currently it's possible to build a Grid System using special Matrix setups along with some advanced Twig tempting but, that's not ideal. Plus, it's a top down UI and contextually it doesn't very well match the frontend; as demonstrated in the following setup from Iain:

Building a grid system in #craftcms to manage 1x1, 1x2, 2x1 & 2x2 blocks in a responsive 2, 4 & 5 col layout. Phew: https://t.co/TXWiWLbbnM

— Iain Urquhart (@iain) April 29, 2015
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

Craft Grid 1st draft

This is what that might look like within Craft with the addition of a Grid System so, without further ado...

Introducing Craft Grid

aka Craft Lautering or Craft Lauter

Craft Grid Rough Draft 1 - Click image then click again to zoom to full view

Note: I didn't format the fields to actually match Iain's design so, feel free to use a little imagination.

This is a very rough first draft of Craft Grid. I can see working on this a bit more to make it just right but, you can seen where I'm going with this; I hope anyway. I'd also like to create the settings page where setup happens for sections, rows and columns. This could be inputs for section tag and it's class. Row tag and it's class. Column tags and their classes etc. Unless of course the whole thing could be handled with template code via Craft Grid tags and conditionals alone (which, now that I think about, it is probably best.)

It would be great to be allowed to edit what the actual template code looked like for Craft Grid. This way everything stays vanilla, stays Bring Your Own HTML, and we can use any Grid System our hearts desire. This of course is the Craft Way!

In other words, allow us to build a "Grid Wrapper" around our fields and when a Section gets started add this tag/class. When a Row starts add this tag/class. When, for example, five Columns are added then add the tags/classes and finally place our Fields within. Then of course allow us to close the tags so on and so forth.

It would be also be great to allow Fields to be added to Sections and Rows. This, for example, would allow us to set background images or colors on certain Sections or Rows. Say you want to set a class on a Section so it spans the whole width of the page 100% and add a bg image and you could then add 100% to the Row so the content is full width. Or, for the Row, just have it's fields centered by setting width and margin 0 auto etc. via a class of container/wrapper etc.

Currently, based on the rough draft, you could click on a Section's name and a modal will popup similar to how it works when you click on an Asset's name like so:

Asset Modal

For the Row, I haven't added a name yet, though, there is plenty of space to do so. I'm not totally set on where to put names as of yet either way but, the concept would be the same for Sections and Row Fields; click something then fields via modals.

I won't go as far to ask for Fields for Columns because, well, I haven't worked that into the UI yet ;-)

Here is a pseudo code of what the template code could look like just for fun. I realize it's not correct and who knows what something like this would do in regards to speed:

{% for each section in craft.grid %}
    <section class="{{ section.class }}">
        {% for each row in craft.grid %}
            <div class="{{ row.class }}">
                {% for each column in craft.grid %}
                    <div class="small-{{ column.number}} column {{ column.xtraClasses }}">
                        {# ...fields go here... #}
                    </div>
                {% endfor %}
            </div>
        {% endfor %}
    </section>
{% endfor %}

Other CMS have Grid Systems (commonly known as Drag and Drop Builder plugins) and I'll include some examples to demonstrate how said other systems have gone about it.

Please note: these examples are for contextual purposes only and by no means have they got it right. Let me explain what I mean:

WP plugins of this sort make use of WP's WYSIWG editor and these plugins basically just drop all the code the plugin/user has created right into the editor as one large mess of html, text and shortcodes etc. This is how I recall them all working and such a thing is of course nasty!

Here, let me show you what I mean. It's Shortcode Soup up in here!

Warning, you may just loose your lunch when you see this...You've been warned!!!

ShortCode Soup

I needed to share that with you so you understand what I'm talking about is something different from what's out there for other CMS; minus the concept of allowing one to build out Sections, Rows, and Columns etc:

Anyway, on to some examples:

Visual Composer Visual Composer by WP Bakery

Visual Composer is interesting because you can actually see the various columns which live inside a row. It allows you to add new rows within each Element (think the section tag) as well as add new Elements too (I think that's how it worked because it's since changed.) There is definitely too much going on there visually, however.

Although it's changed quite a bit from that screenshot, you can preview Visual Composer if you don't mind giving up your email address (make sure to edit in the backend to get a better comparison since VC has frontend editing as well.)


Divi by ElegentThemes Divi by ElegentThemes

Divi is simpler and easier on the eyes since it's just basic shapes.


Page Builder by SiteOrigin Page Builder by SiteOrigin

And, whatever, you get the idea by now I'm sure!

Thanks for your consideration!

@jasonmccallister
Copy link

Yii2 includes some grid style options as widgets: http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html

Might be worth a look through.

@Natetronn
Copy link
Author

There is a similar request called "Matrix Block Settings" which is trying to accomplish something similar to Craft Grid System. I think it's lacking the visual context that my suggestion is aiming for, however, in general it's trying to accomplish some of the same things.

With that said, the idea of adding Custom Fields to a Matrix field actually ties into my suggestion about adding Custom Fields to Sections, Rows and Columns (see my gist where Asset Custom Fields are being edited for example.)

The "Matrix Block Settings" idea doesn't show it and I'd like to take the idea a little different direction and further:

It would be AMAZING to be able to have the Custom Field Layout manager on every Custom Field so we can add any Field to any Field.

So, in a new tab of a Custom Field's creation page, you'd have the Field Layout Designer:

Field Layout Designer

Maybe we'll treat Sections, Rows, and Columns as their own Custom Fields. They of course will now have access to the Field Designer in their setup. And, now that I think about it, this might be what allows us to place Rows inside Sections and Columns inside Rows (which are inside Sections) so, this is actually three levels deep. Does that makes sense? (it's late, I still need to think that through)

This really goes along with what Craft is already trying to achieve with it's Custom Fields for Users and Assets and hopefully Grid System etc. Just now you'll be able to add fields in a few more places.

One other thought I had about the Craft Grid system is to tie the Grid System with Entry Types. There now would be two options for using the Grid System in the settings of Sections and Rows.

This might be achieved, since Sections, Rows and Columns are just like any other Custom Field by having settings which allow for a minimum and a maximum or 0 for infinity etc.

1 - Option one is to have the dev build out a Grid, with Section/s, Row/s, Column/s and Fields that is fixed (no plus signs or column icons for adding more.) This can then be "attached" to a Entry Type. Same as the Entry Type now just with the added features of the Grid.

2 - Option two is more of the DIY style which I currently show in my mockup. This option allows more of a page builder type workflow and people can add however much of and however many of what they want etc.

People might get carried away with option 2 and why option 1 and min/max settings gives the dev more control over their layout via the Grid and Entry Types but, not so much freedom as option 2 does.

@Natetronn
Copy link
Author

@brandon There are currently at least 3 "Matrix field within a Matrix field" and or "More Column Types in Table Fields" requests here along with what I've suggested.

This request has moved up the ladder very fast in a very short amount of time and is quickly catching up to both of the requests I've quoted above. It's already on the first page in fact of the feedback site. With that said, there is definitely a HIGH demand for SOMETHING like this.

It's possible that all the solutions could be wrapped into one as you seem to be suggesting and that could be the case with "The Ultimate Matrix Warrior" and I'm okay with that though, it would be nice to let this finish it's movement upwards before we (P&T) makes a decision either way.

That seems fair anyway, no?

I could leave it at that but, I hope you don't mind me adding a little more on this topic. And please forgive me if this comes off as pushy in anyway. That is defiantly not my intention! I really appreciate everyones time to even consider reading what I've written and I'm very happy with the way things are moving along etc.

I read Matrix within Matrix is already close to completion technically ("most of the PHP code has already been set up for it - BK") and it's the UI that needs to be worked out at this point. You said UI submissions is something you'd be willing to take into consideration.

This UI is something which I've spent time on and isn't something which I threw together with out first doing some research. I've looked at over 20+ solutions similar to what I'm suggesting. With that said, please remember this was a very rough first draft. It's already gotten better since and I can keep working on mockup to show all the details if need be. I could include the mobile "Artboard" along with it since I'm in 100% agreement that if this UI doesn't stand up to Mobile First than it shouldn't be on the list of possibilities. (Mobile First wasn't an initial option because it was the concept that I was trying to demonstrate, first, if that makes sense?)

And please allow me touch on Iain's situation directly: Iain's situation is definitely unique (though at the same time not) in that it could be difficult to give Content Editors and or Publishers such a layout which was visually and contextually a one-to-one experience. I think this to be the case for both Craft Grid System as well as Matrix within Matrix. Would one be able to achieve it better thank the other? That's probably a matter of opinion and we'd have to put the UI's side-by-side to make such a decision at that point.

I will say what I'm suggesting will most definitely handle Iain's solution fairly nicely and with ease. Now that I think about it let me show you what Ian's solution would look like in Craft Grid System so people have the visual context which I'm trying to suggest:

Side By Side
click to view larger and then click again to zoom

Note: looking at the rows of fields you'll notice that they are all collapsed. Now, if you look at Iain's original shot you can make a pretty good guess at to what the fields could consist of. Maybe Matrix with image, title and possibly background colors for both the box background and the transparent color under the text for example etc. Also, I mention in the mockup of Ian's site that this could be a 10 column grid system. In this example it would use col-2 and col-4 added up to 10 but, it could just as easy be a 5 column grid too.

Anyway, I've spent quite a bit of time on this so, at this point, I think I will digress unless anyone any questions related to this topic.

By the way, I now have an 90% completed Sketch design based on much of the Craft Control Panel. I might seek your approval to clean it up and release it so others can make use of it for their ideas, plugins and mockups etc. @brandon maybe we can touch base on this later? Up to you of course ;-)

@Natetronn
Copy link
Author

I've changed up the Craft Grid System and renamed it to the Layout Manager (or Layout Editor if you prefer?)

The objective of this change was to make it more Mobile Friendly and to remove Layout from Content which I think I've accomplished!

NOTE: this is just a super quick mockup and very sloppy. The fields are mismatched so, please use your imagination.

This is now in a format similar to Live Preview. There will be a left sidebar, which can be resized to make more space on either side just like Live Preview.

In Live Preview drag the grey line between the sidebar and the website preview. A drag handle will appear if you didn't know.

Layout Manager

click to view larger and then click again to zoom

While editing an entry there could be a button below the Live Preview button that said "Layout Manager" which would launch the Layout Manager into Layout Editing Mode.

Maybe the fields in the left sidebar could be representations of fields and not the actual fields that you see in my example. I don't see any reason to edit a field in the Layout Manager in other words.

Maybe it could just be a title and the drag icon similar to what currently exists on Matrix fields and a box around the field or something so there is visual separation between the fields.

You could build out the layout that you want by adding sections and rows (and columns maybe) then you could drag and drop over your fields into there places etc.

By no mean is this version of the Layout Manager complete but, I think it will definitely pass the Mobile First test or at least be a much better start than my original concept. Not sure why anyone would use Layout Manager or Live Preview on their 4" cell phone screen but, hey, you never know.

Seems like a possibility now, though. At the very least it should work as good as Live Preview does is if not more. I say if not more because there is nothing to say a website displayed in Live Preview will be responsive where as this will of course be responsive from the get go.

I shared this idea with @Crawf in Slack and he said "Oh, it has a similar feel to the DesignModo Generator." Come to find out it does in fact! Use your imagination, test it out and you'll get a pretty good feel for how Layout Manager might work and feel.

@hongc-cc
Copy link

hongc-cc commented Oct 12, 2016

structure request
Better to keep frontend things away from content.
And tiles UI are achievable using entries structure (tiles layout usually required to know its siblings and parents)
I will simply suggest matrix field structure so we can actually put matrix into a hierarchy.

// lets use the nav here for easy demo
{% nav matrixBlock in entry.matrixFieldHandle %}
    <div>
    {% switch matrixBlock.type %}
    ...
    {% endswitch %}
    {% if children %}
        {% children %}
    {% endchildren %}
    </div>
{% endnav %}

You can then create a matrixBlock tileBlock that have the layout settings, and the titles content as children.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment