<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DJ Walker</title>
    <description>The latest articles on DEV Community by DJ Walker (@dwalkr).</description>
    <link>https://dev.to/dwalkr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F254829%2F694af850-00a8-4def-9e99-3b42e4041c40.jpeg</url>
      <title>DEV Community: DJ Walker</title>
      <link>https://dev.to/dwalkr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dwalkr"/>
    <language>en</language>
    <item>
      <title>What are "Blocks" in TinaCMS?</title>
      <dc:creator>DJ Walker</dc:creator>
      <pubDate>Thu, 19 Dec 2019 20:48:25 +0000</pubDate>
      <link>https://dev.to/tinacms/what-are-blocks-in-tinacms-1nm5</link>
      <guid>https://dev.to/tinacms/what-are-blocks-in-tinacms-1nm5</guid>
      <description>&lt;p&gt;&lt;em&gt;"There are only two hard things in Computer Science: cache invalidation and naming things."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This axiom, &lt;a href="http://www.tbray.org/ongoing/When/200x/2005/12/23/UPI" rel="noopener noreferrer"&gt;attributed to Phil Karlton&lt;/a&gt;, resonates with anyone who has spent any amount of time working with software. The post you're currently reading is, in some ways, about the latter problem.&lt;/p&gt;

&lt;p&gt;One concept that we were eager to introduce to Tina is something we refer to as &lt;strong&gt;Blocks fields&lt;/strong&gt;. We first &lt;a href="https://forestry.io/blog/sawmill-layout-composer-for-hugo-and-forestry/" rel="noopener noreferrer"&gt;introduced this concept in Forestry&lt;/a&gt; some time ago, and we think it’s a powerful idea. The challenge with Blocks is that it’s kind of an abstract idea, and thus was tagged with a similarly abstract name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Blocks?&lt;/strong&gt; To put it succinctly, Blocks refers to a data structure that consists of an &lt;em&gt;array of unlike objects&lt;/em&gt;. If you didn’t quite grok that, read on and I’ll do my best to explain why we introduced the Blocks concept to Tina and how it relates to other kinds of fields.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Fields and Compound Fields
&lt;/h2&gt;

&lt;p&gt;The field types we’ve implemented in Tina can be broadly grouped into two categories: &lt;strong&gt;simple fields&lt;/strong&gt; and &lt;strong&gt;compound fields&lt;/strong&gt;. The designation for whether a field is simple or compound has to do with the kind of data that the field represents.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple fields&lt;/strong&gt; are fields for data that can be represented as a single value, like a string or number. In computer science lingo, these are referred to as &lt;a href="https://softwareengineering.stackexchange.com/questions/238033/what-does-it-mean-when-data-is-scalar" rel="noopener noreferrer"&gt;scalar values&lt;/a&gt;. An example of simple fields in Tina would be the text field, color field, or toggle. Even the &lt;a href="https://tinacms.org/docs/fields/markdown" rel="noopener noreferrer"&gt;markdown WYSIWYG&lt;/a&gt; can be considered a simple field, in spite of its complex frontend behavior, because the value it exports is just a big block of text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compound fields&lt;/strong&gt; are fields that can’t be represented by a single value. Data exported by a compound field is &lt;em&gt;structured.&lt;/em&gt; When saved, a compound field’s data will be represented by a non-scalar data type such as an array or object. Compound fields are &lt;strong&gt;fields composed of other fields&lt;/strong&gt;. The compound fields in Tina include the Group, Group List, and Blocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Groups&lt;/em&gt; and &lt;em&gt;Group Lists&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Tina’s &lt;strong&gt;Group&lt;/strong&gt; field is a collection of &lt;strong&gt;simple fields&lt;/strong&gt;. The fields that comprise a Group field can all be of the same type, or be of different types. Group fields are good for representing a single &lt;em&gt;entity&lt;/em&gt; that is comprised of smaller pieces of data.&lt;/p&gt;

&lt;p&gt;Consider two ways to store a name in JSON. We could store the full name as a simple string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"DJ Walker"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, we could contrive a simple data structure to store the name in a more semantic fashion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"first"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"DJ"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Walker"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We might use a simple text field in the first case, and a &lt;strong&gt;Group&lt;/strong&gt; of two text fields for the second.&lt;/p&gt;

&lt;h3&gt;
  
  
  Group Lists
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftinacms.org%2Fimg%2Fblog%2Ffig-group-grouplist.svg" class="article-body-image-wrapper"&gt;&lt;img alt="diagram of linked circles" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftinacms.org%2Fimg%2Fblog%2Ffig-group-grouplist.svg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Group List&lt;/strong&gt; is similar to the &lt;strong&gt;Group&lt;/strong&gt; field type, with an added dimension. Whereas the Group field represents a single entity, the Group List represents &lt;em&gt;multiple entities&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let’s say, instead of a single name, we’re storing a list of names like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"subscribers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"first"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"DJ"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Walker"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"first"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Nolan"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Phillips"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could use a &lt;strong&gt;Group List&lt;/strong&gt; here. All entities in the Group List have the same &lt;strong&gt;shape&lt;/strong&gt;; in other words, each object in the array will have the same keys. This makes the Group List analogous to a two-dimensional data structure, like a spreadsheet or database table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;first&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;last&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DJ&lt;/td&gt;
&lt;td&gt;Walker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nolan&lt;/td&gt;
&lt;td&gt;Phillips&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Blocks&lt;/em&gt;: Like a &lt;em&gt;Group List&lt;/em&gt;, But Different
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftinacms.org%2Fimg%2Fblog%2Ffig-blocks.svg" class="article-body-image-wrapper"&gt;&lt;img alt="diagram of different shapes linked together" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftinacms.org%2Fimg%2Fblog%2Ffig-blocks.svg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like the &lt;strong&gt;Group List&lt;/strong&gt;, the &lt;strong&gt;Blocks&lt;/strong&gt; structure represents multiple entities. The difference between a Group List and Blocks is that the Blocks structure supports multiple entities &lt;em&gt;with potentially different shapes&lt;/em&gt;. This makes the relationship between entities in a Blocks structure much looser than with a Group List.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Blocks Useful For?
&lt;/h3&gt;

&lt;p&gt;In practice, there are a couple use cases uniquely suited to Blocks.&lt;/p&gt;

&lt;p&gt;The primary motivation for the Blocks-style data structure was to facilitate a page builder experience. In our &lt;a href="https://github.com/tinacms/tina-starter-grande" rel="noopener noreferrer"&gt;Tina Grande starter&lt;/a&gt;, a page can be strung together by adding different entities to a Blocks field, each one containing fields that configure a different part of the page.&lt;/p&gt;

&lt;p&gt;Another way Grande makes use of Blocks is in its embedded form builder. Like the page builder, Grande approaches forms as a sequence of loosely-related, complex components (in this case, the form fields.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Give Blocks a Chance
&lt;/h2&gt;

&lt;p&gt;By now, you should have a better sense of what we mean when we talk about Blocks in Tina.&lt;/p&gt;

&lt;p&gt;If you want to see a glimpse of what you can do with a blocks-based content strategy, take a look at &lt;a href="https://github.com/tinacms/tina-tailwind-inline-demo" rel="noopener noreferrer"&gt;our inline Tailwind and Next.js demo&lt;/a&gt; and &lt;a href="https://tina-demo-two.vercel.app" rel="noopener noreferrer"&gt;give it a try&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you still aren't quite sure how the Blocks field works, or want to share some ideas on using Blocks, swing by &lt;a href="https://community.tinacms.org/" rel="noopener noreferrer"&gt;our community forum&lt;/a&gt; and make a post!&lt;/p&gt;

</description>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
