{"id":4771,"date":"2022-04-15T09:45:35","date_gmt":"2022-04-15T09:45:35","guid":{"rendered":"https:\/\/learncode.tinjurewp.com\/?p=4771"},"modified":"2022-04-16T09:46:05","modified_gmt":"2022-04-16T09:46:05","slug":"patternizing-block-templates","status":"publish","type":"post","link":"https:\/\/learncode.tinjurewp.com\/patternizing-block-templates\/","title":{"rendered":"Patternizing Block Templates"},"content":{"rendered":"<p class=\"note icon-note\">Notes: This post is <strong>part 2<\/strong> of leaning creating and block patterns using full site editing (FSE) interface in block themes. This learning post is still in active development and updated regularly.<\/p>\n<p>In <a href=\"https:\/\/learncode.tinjurewp.com\/refactoring-patterns-in-block-themes\/\" target=\"_blank\" rel=\"noopener\">this previous post<\/a>, how to move entire patterns from <code>\/inc\/patterns<\/code> folder to theme\u2019s root <code>\/patterns<\/code> folder was described. The PHP registered patterns were refactored by modifying header section inside the <code>\/patterns<\/code> folder, a feature available since <a href=\"https:\/\/make.wordpress.org\/core\/2022\/03\/02\/whats-new-in-gutenberg-12-7-2-march\/\" target=\"_blank\" rel=\"noopener\">Gutenberg 12.7<\/a> and to be landed in WordPress 6.0.<\/p>\n<p>In this short learning-note post, I wanted to move the entire post-content from the block templates to <code>\/patterns<\/code> folder and just refer the related patterns in the templates.<\/p>\n<div class=\"article-series\"><strong>Learning Goals<\/strong><br \/>\nPart<strong> 1<\/strong>: <a href=\"https:\/\/learncode.tinjurewp.com\/refactoring-patterns-in-block-themes\/\" target=\"_blank\" rel=\"noopener\">To create entire block theme templates based on block patterns only using experimental emptytheme.<\/a><br \/>\n<strong>Part 2:<\/strong> Patternizing Block Templates (<strong>this post<\/strong>)<\/div>\n<p>The objective of this learning-note post is to move entire post-contents from the block templates to <code>\/patterns<\/code> directory as described in the previous <em>part 1<\/em> post.<\/p>\n<p>For this proof of concept post, I used a child theme (<code>Empty-child<\/code>) of the <a href=\"https:\/\/github.com\/WordPress\/theme-experiments\/tree\/master\/emptytheme\" target=\"_blank\" rel=\"noopener\">emptytheme<\/a> of WordPress experimental theme.<!--more--><\/p>\n<pre class=\"line-numbers\"><code class=\"language-bash\">#! EMPTY-CHILD theme file structure\nempty-child\n|__ assets\n|__ patterns\n    |__ footer-default.php\n    |__ header-default.php \n    |__ hidden-404.php\n    |__ posts-list.php\n    |__ query-default.php\n    |__ query-pagination.php\n    |__ single-post.php\n|__ templates\n    |__ index.html\n    |__ archive.html\n    |__ singular.html\n    |__ search.html\n    |__ 404.html\n|__ parts\n    |__ header.html\n    |__ footer.html\n|__ style.css\n|__ functions.php\n|__ index.php\n|__ theme.json<\/code><\/pre>\n<p>In this example, I created parts and templates and added content from WordPress <a href=\"https:\/\/pcm.wordpress.org\/themes\/archeo\/\" target=\"_blank\" rel=\"noopener\">Archeo<\/a> theme, with some modification.<\/p>\n<p>The <code>index.html<\/code> template of the Empty-child looks as shown below.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-markup\">&lt;!-- wp:template-part {&quot;slug&quot;:&quot;header&quot;,&quot;tagName&quot;:&quot;header&quot;} \/--&gt;\n\n  &lt;!-- wp:group {&quot;align&quot;:&quot;full&quot;,&quot;layout&quot;:{&quot;inherit&quot;:true}} --&gt;\n  &lt;div class=&quot;wp-block-group alignfull&quot;&gt;&lt;!-- wp:query {&quot;queryId&quot;:0,&quot;query&quot;:{&quot;perPage&quot;:10,&quot;pages&quot;:0,&quot;offset&quot;:0,&quot;postType&quot;:&quot;post&quot;,&quot;order&quot;:&quot;desc&quot;,&quot;orderBy&quot;:&quot;date&quot;,&quot;author&quot;:&quot;&quot;,&quot;search&quot;:&quot;&quot;,&quot;exclude&quot;:[],&quot;sticky&quot;:&quot;&quot;,&quot;inherit&quot;:true,&quot;taxQuery&quot;:null},&quot;tagName&quot;:&quot;main&quot;,&quot;align&quot;:&quot;wide&quot;,&quot;layout&quot;:{&quot;inherit&quot;:false}} --&gt;\n    &lt;main class=&quot;wp-block-query alignwide&quot;&gt;&lt;!-- wp:post-template --&gt;\n    &lt;!-- wp:separator {&quot;opacity&quot;:&quot;css&quot;,&quot;className&quot;:&quot;is-style-wide&quot;} --&gt;\n    &lt;hr class=&quot;wp-block-separator has-css-opacity is-style-wide&quot;\/&gt;\n    &lt;!-- \/wp:separator --&gt;\n    \n    &lt;!-- wp:columns {&quot;style&quot;:{&quot;spacing&quot;:{&quot;padding&quot;:{&quot;top&quot;:&quot;1em&quot;}}}} --&gt;\n    &lt;div class=&quot;wp-block-columns&quot; style=&quot;padding-top:1em&quot;&gt;&lt;!-- wp:column {&quot;verticalAlignment&quot;:&quot;top&quot;,&quot;width&quot;:&quot;10%&quot;,&quot;style&quot;:{&quot;spacing&quot;:{&quot;padding&quot;:{&quot;top&quot;:&quot;1em&quot;}}}} --&gt;\n    &lt;div class=&quot;wp-block-column is-vertically-aligned-top&quot; style=&quot;padding-top:1em;flex-basis:10%&quot;&gt;&lt;!-- wp:post-date {&quot;format&quot;:&quot;M j&quot;,&quot;fontSize&quot;:&quot;small&quot;} \/--&gt;&lt;\/div&gt;\n    &lt;!-- \/wp:column --&gt;\n    \n    &lt;!-- wp:column {&quot;verticalAlignment&quot;:&quot;center&quot;,&quot;width&quot;:&quot;&quot;} --&gt;\n    &lt;div class=&quot;wp-block-column is-vertically-aligned-center&quot;&gt;&lt;!-- wp:post-title {&quot;isLink&quot;:true,&quot;fontSize&quot;:&quot;x-large&quot;} \/--&gt;\n    \n    &lt;!-- wp:post-excerpt {&quot;moreText&quot;:&quot;Read More ....&quot;} \/--&gt;&lt;\/div&gt;\n    &lt;!-- \/wp:column --&gt;&lt;\/div&gt;\n    &lt;!-- \/wp:columns --&gt;\n    &lt;!-- \/wp:post-template --&gt;\n    \n    &lt;!-- wp:spacer {&quot;height&quot;:&quot;32px&quot;} --&gt;\n    &lt;div style=&quot;height:32px&quot; aria-hidden=&quot;true&quot; class=&quot;wp-block-spacer&quot;&gt;&lt;\/div&gt;\n    &lt;!-- \/wp:spacer --&gt;\n    \n    &lt;!-- wp:pattern {&quot;slug&quot;:&quot;emptytheme\/query-pagination&quot;} \/--&gt;\n    \n    &lt;\/main&gt;\n    &lt;!-- \/wp:query --&gt;&lt;\/div&gt;\n    &lt;!-- \/wp:group --&gt;\n  \n &lt;!-- wp:template-part {&quot;slug&quot;:&quot;footer&quot;,&quot;tagName&quot;:&quot;footer&quot;} \/--&gt;<\/code><\/pre>\n<p>The above code snippet was refactored by moving the content section into <code>\/patterns<\/code> folder, as described in the <a href=\"https:\/\/learncode.tinjurewp.com\/refactoring-patterns-in-block-themes\/\" target=\"_blank\" rel=\"noopener\">previous post<\/a>.<\/p>\n<h6><strong>Create <code>posts-list.php<\/code> file inside <code>\/patterns<\/code> folder<\/strong><\/h6>\n<p>First create the following header section at the top section of <code>posts-list.php<\/code> file.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-php\">\/\/ header section\n&lt;?php\n\/**\n * Title: Posts list\n * Slug: emptytheme\/posts-list\n * Categories: featured, query\n * Block Types: query\n *\/\n?&gt;<\/code><\/pre>\n<p>Next copy the content section (line 3-31) from the previous section append after the header in <code>post-list.php<\/code> file, see below in entirety.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-php\">&lt;?php\n\/**\n * Title: Posts List\n * Slug: emptytheme\/posts-list\n * Categories: emptytheme-posts\n * Viewport Width: 1280\n * Inserter: yes\n *\/\n\n?&gt;\n\n&lt;!-- wp:group {&quot;align&quot;:&quot;full&quot;,&quot;layout&quot;:{&quot;inherit&quot;:true}} --&gt;\n&lt;div class=&quot;wp-block-group alignfull&quot;&gt;&lt;!-- wp:query {&quot;queryId&quot;:0,&quot;query&quot;:{&quot;perPage&quot;:10,&quot;pages&quot;:0,&quot;offset&quot;:0,&quot;postType&quot;:&quot;post&quot;,&quot;order&quot;:&quot;desc&quot;,&quot;orderBy&quot;:&quot;date&quot;,&quot;author&quot;:&quot;&quot;,&quot;search&quot;:&quot;&quot;,&quot;exclude&quot;:[],&quot;sticky&quot;:&quot;&quot;,&quot;inherit&quot;:true,&quot;taxQuery&quot;:null},&quot;tagName&quot;:&quot;main&quot;,&quot;align&quot;:&quot;wide&quot;,&quot;layout&quot;:{&quot;inherit&quot;:false}} --&gt;\n&lt;main class=&quot;wp-block-query alignwide&quot;&gt;&lt;!-- wp:post-template --&gt;\n&lt;!-- wp:separator {&quot;opacity&quot;:&quot;css&quot;,&quot;className&quot;:&quot;is-style-wide&quot;} --&gt;\n&lt;hr class=&quot;wp-block-separator has-css-opacity is-style-wide&quot;\/&gt;\n&lt;!-- \/wp:separator --&gt;\n\n&lt;!-- wp:columns {&quot;style&quot;:{&quot;spacing&quot;:{&quot;padding&quot;:{&quot;top&quot;:&quot;1em&quot;}}}} --&gt;\n&lt;div class=&quot;wp-block-columns&quot; style=&quot;padding-top:1em&quot;&gt;&lt;!-- wp:column {&quot;verticalAlignment&quot;:&quot;top&quot;,&quot;width&quot;:&quot;10%&quot;,&quot;style&quot;:{&quot;spacing&quot;:{&quot;padding&quot;:{&quot;top&quot;:&quot;1em&quot;}}}} --&gt;\n&lt;div class=&quot;wp-block-column is-vertically-aligned-top&quot; style=&quot;padding-top:1em;flex-basis:10%&quot;&gt;&lt;!-- wp:post-date {&quot;format&quot;:&quot;M j&quot;,&quot;fontSize&quot;:&quot;small&quot;} \/--&gt;&lt;\/div&gt;\n&lt;!-- \/wp:column --&gt;\n\n&lt;!-- wp:column {&quot;verticalAlignment&quot;:&quot;center&quot;,&quot;width&quot;:&quot;&quot;} --&gt;\n&lt;div class=&quot;wp-block-column is-vertically-aligned-center&quot;&gt;&lt;!-- wp:post-title {&quot;isLink&quot;:true,&quot;fontSize&quot;:&quot;x-large&quot;} \/--&gt;\n\n&lt;!-- wp:post-excerpt {&quot;moreText&quot;:&quot;Read More ....&quot;} \/--&gt;&lt;\/div&gt;\n&lt;!-- \/wp:column --&gt;&lt;\/div&gt;\n&lt;!-- \/wp:columns --&gt;\n&lt;!-- \/wp:post-template --&gt;\n\n&lt;!-- wp:spacer {&quot;height&quot;:&quot;32px&quot;} --&gt;\n&lt;div style=&quot;height:32px&quot; aria-hidden=&quot;true&quot; class=&quot;wp-block-spacer&quot;&gt;&lt;\/div&gt;\n&lt;!-- \/wp:spacer --&gt;\n\n&lt;!-- wp:pattern {&quot;slug&quot;:&quot;emptytheme\/query-pagination&quot;} \/--&gt;\n\n&lt;\/main&gt;\n&lt;!-- \/wp:query --&gt;&lt;\/div&gt;\n&lt;!-- \/wp:group --&gt;<\/code><\/pre>\n<h6>Refactoring Template Files<\/h6>\n<p>Next, refactoring template file. The refactored <code>index.html<\/code> file has only three lines of code.<\/p>\n<pre class=\"line-numbers\" data-line=\"3\"><code class=\"language-markup\">&lt;!-- wp:template-part {&quot;slug&quot;:&quot;header&quot;,&quot;tagName&quot;:&quot;header&quot;} \/--&gt;\n\n&lt;!-- wp:pattern {&quot;slug&quot;:&quot;emptytheme\/posts-list&quot;} \/--&gt;\n  \n&lt;!-- wp:template-part {&quot;slug&quot;:&quot;footer&quot;,&quot;tagName&quot;:&quot;footer&quot;} \/--&gt;<\/code><\/pre>\n<p>The refactored index.html file and original template displayed exactly same way both in the editor and in the front-end.<\/p>\n<p>The following other refactored template files (see below) behave similarly too.<\/p>\n<p><strong>(a)<\/strong> Refactored <code>singular.html<\/code> template:<\/p>\n<pre class=\"line-numbers\" data-line=\"3\"><code class=\"language-markup\">&lt;!-- wp:template-part {&quot;slug&quot;:&quot;header&quot;,&quot;tagName&quot;:&quot;header&quot;} \/--&gt;\n\n&lt;!-- wp:pattern {&quot;slug&quot;:&quot;emptytheme\/single-post&quot;} \/--&gt;\n\n&lt;!-- wp:template-part {&quot;slug&quot;:&quot;footer&quot;,&quot;tagName&quot;:&quot;footer&quot;} \/--&gt;<\/code><\/pre>\n<p><strong>(b)<\/strong> Refactored <code>archive.html<\/code> template file:<\/p>\n<pre class=\"line-numbers\" data-line=\"7,11\"><code class=\"language-markup\">&lt;!-- wp:template-part {&quot;slug&quot;:&quot;header&quot;,&quot;tagName&quot;:&quot;header&quot;} \/--&gt;\n\n&lt;!-- wp:query {&quot;tagName&quot;:&quot;main&quot;,&quot;layout&quot;:{&quot;inherit&quot;:true}} --&gt;\n&lt;main class=&quot;wp-block-query&quot;&gt;\n  &lt;!-- wp:query-title {&quot;type&quot;:&quot;archive&quot;,&quot;style&quot;:{&quot;spacing&quot;:{&quot;margin&quot;:{&quot;bottom&quot;:&quot;80px&quot;}}}} \/--&gt;\n  &lt;!-- wp:post-template --&gt;\n  &lt;!-- wp:pattern {&quot;slug&quot;:&quot;emptytheme\/query-default&quot;} \/--&gt;\n  &lt;!-- \/wp:post-template --&gt;\n  &lt;!-- wp:group {&quot;layout&quot;:{&quot;inherit&quot;:true}} --&gt;\n\n  &lt;!-- wp:pattern {&quot;slug&quot;:&quot;emptytheme\/query-pagination&quot;} \/--&gt;\n\n  &lt;!-- \/wp:group --&gt;\n  &lt;\/main&gt;\n  &lt;!-- \/wp:query --&gt;\n  \n  &lt;!-- wp:template-part {&quot;area&quot;:&quot;footer&quot;,&quot;slug&quot;:&quot;footer&quot;,&quot;tagName&quot;:&quot;footer&quot;} \/--&gt;<\/code><\/pre>\n<p><strong>(c)<\/strong> Refactored <code>404.html<\/code> template:<\/p>\n<pre class=\"line-numbers\" data-line=\"6\"><code class=\"language-markup\">&lt;!-- wp:template-part {&quot;slug&quot;:&quot;header&quot;,&quot;tagName&quot;:&quot;header&quot;} \/--&gt;\n\n&lt;!-- wp:group {&quot;tagName&quot;:&quot;main&quot;} --&gt;\n&lt;main class=&quot;wp-block-group&quot;&gt;&lt;!-- wp:group {&quot;layout&quot;:{&quot;inherit&quot;:true}} --&gt;\n&lt;div class=&quot;wp-block-group&quot;&gt;\n&lt;!-- wp:pattern {&quot;slug&quot;:&quot;tt2test\/hidden-404&quot;} \/--&gt;\n&lt;\/div&gt;\n&lt;!-- \/wp:group --&gt;&lt;\/main&gt;\n&lt;!-- \/wp:group --&gt;\n\n&lt;!-- wp:template-part {&quot;slug&quot;:&quot;footer&quot;,&quot;tagName&quot;:&quot;footer&quot;} \/--&gt;<\/code><\/pre>\n<p>Similarly, other templates can be created with desired patterns to display.<\/p>\n<h6>Wrapping Up<\/h6>\n<p>In this learning-note post, how to move page contents from template or parts files to \/patterns directory was discussed. This process greatly simplifies patterns use in templates.<\/p>\n<h6>Useful Resources<\/h6>\n<p>The following is list of references link that I gathered during my brief research. While preparing this post, I have also referred some of the following references extensively.<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/WordPress\/gutenberg\/issues\/39654#issuecomment-1096823783\" target=\"_blank\" rel=\"noopener\">A new \/patterns directory for themes<\/a> | GitHub<\/li>\n<li><a href=\"https:\/\/github.com\/Automattic\/themes\/pull\/5820\" target=\"_blank\" rel=\"noopener\">Patternized the block header #5820<\/a> | GitHub<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Notes: This post is part 2 of leaning creating and block patterns using full site editing (FSE) interface in block themes. This learning post is still in active development and updated regularly. In this previous post, how to move entire patterns from \/inc\/patterns folder to theme\u2019s root \/patterns folder was described. The PHP registered patterns [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[27],"tags":[],"class_list":["post-4771","post","type-post","status-publish","format-standard","hentry","category-full-site-editing"],"_links":{"self":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/4771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/comments?post=4771"}],"version-history":[{"count":0,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/4771\/revisions"}],"wp:attachment":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/media?parent=4771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/categories?post=4771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/tags?post=4771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}