Skip to content
This repository was archived by the owner on Jul 19, 2021. It is now read-only.

Commit ec99adf

Browse files
author
Harshal Brahmbhatt
committed
Slate Sections Plugin
1 parent 480c0dc commit ec99adf

File tree

24 files changed

+3772
-176
lines changed

24 files changed

+3772
-176
lines changed
+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# @shopify/slate-sections-plugin
2+
3+
The plugin allows developers to create Shopify theme sections from a variety of files contained in a folder, and combines these files into a single Liquid section file for use on Shopify servers.
4+
5+
## Getting Started
6+
7+
First install the plugin
8+
9+
```
10+
npm install @shopify/slate-sections-plugin --save-dev
11+
```
12+
13+
or
14+
15+
```
16+
yarn add @shopify/slate-sections-plugin --dev
17+
```
18+
19+
Then add it to your webpack config, an example below.
20+
21+
```js
22+
const SlateSectionsPlugin = require('@shopify/slate-sections-plugin');
23+
24+
const slateSectionsOptions = {
25+
from: '/absolute/path/to/sections/source',
26+
to: '/absolute/path/to/sections/output',
27+
};
28+
29+
module.exports = {
30+
plugins: [new SlateSectionsPlugin({slateSectionsOptions})],
31+
};
32+
```
33+
34+
## Options
35+
36+
| Option | Required? | Type | Default | Description |
37+
| ------------------- | --------- | ------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
38+
| from | Yes | String | undefined | The absolute path to the folder containing your sections, typically ./src/sections |
39+
| to | Yes | String | undefined | The absolute path to the folder where your sections will be outputted to, typically ./dist/sections |
40+
| genericTemplateName | No | String | 'template.liquid' | If you're using the 'Sections as Folders' structure, your template names by default need to be 'template.liquid'. You may change this value if you would like to. |
41+
42+
## Sections as Liquid Files
43+
44+
You may structure your sections folder by creating separate Liquid files for each section:
45+
46+
```bash
47+
./sections
48+
├── blog-posts.liquid
49+
├── collection-list.liquid
50+
├── featured-collection.liquid
51+
├── featured-product.liquid
52+
├── footer.liquid
53+
├── header.liquid
54+
├── image-with-text.liquid
55+
├── newsletter.liquid
56+
├── product.liquid
57+
└── rich-text.liquid
58+
```
59+
60+
This structure simply tells the plugin to copy the Liquid files into the `./dist` sections folder
61+
62+
## Sections as Folders
63+
64+
You can separate a schema from your Liquid template by creating a folder for each section. This increases maintainability and provides syntax highlighting for your JSON schema objects.
65+
66+
In order to have separate schema files, you must create a folder for the section and within that folder create a `template.liquid` file which will take the same name as the directory it will live in. You may optionally create a `schema.json` file containing the section's settings, which will be appended to the Liquid template inside of `{% schema %}` tags.
67+
68+
### Example
69+
70+
```bash
71+
./sections/article-template
72+
├── schema.json
73+
└── template.liquid
74+
```
75+
76+
This will create an `article-template.liquid` with the contents of the `template.liquid` file and with JSON wrapped in `{% schema %}` tags, which will be copied over to the `./dist` sections folder.
77+
78+
## Translations
79+
80+
If you need settings in your schema that require multiple translations, you can specify this by doing the following:
81+
82+
```json
83+
{
84+
"name": {
85+
"de": "Posts",
86+
"en": "Posts",
87+
"es": "Publicaciones",
88+
"fr": "Articles",
89+
"it": "Articoli",
90+
"ja": "投稿",
91+
"pt-BR": "posts"
92+
},
93+
"settings": [
94+
{
95+
"type": "checkbox",
96+
"id": "image_parallax",
97+
"label": {
98+
"de": "Parallax-Animation für Bild anzeigen",
99+
"en": "Show image parallax animation",
100+
"es": "Mostrar animación de paralaje de imagen",
101+
"fr": "Afficher l'animation en parallaxe",
102+
"it": "Mostra animazione parallasse immagine",
103+
"ja": "画像のパララックスアニメーションを表示する",
104+
"pt-BR": "Exibir animação de paralaxe da imagem"
105+
}
106+
}
107+
]
108+
}
109+
```
110+
111+
However, as the number of theme languages add up over time, and when you have many settings that require translations, this can result in a very large JSON file which becomes difficult to maintain. This plugin allows you to separate the translations into separate locale files allowing for easier maintainability for your theme's translations.
112+
113+
First you specify the schema's structure in `schema.json`:
114+
115+
```json
116+
{
117+
"name": {
118+
"t": "name"
119+
},
120+
"settings": [
121+
{
122+
"type": "checkbox",
123+
"id": "image_parallax",
124+
"label": {
125+
"t": "settings.image_parallax.label"
126+
}
127+
}
128+
]
129+
}
130+
```
131+
132+
For every key with multiple translations you specify an object with a `t` key and its value is the key to retrieve the translation from a different JSON object as the one shown below. You would have a translation object such as the one below for every language you would like to support:
133+
134+
```json
135+
{
136+
"name": "Posts",
137+
"settings": {
138+
"image_parallax": {
139+
"label": "Show image parallax animation",
140+
"info": "Only shows on desktop"
141+
}
142+
}
143+
}
144+
```
145+
146+
The plugin will take care of combining all the translations into an object such as the one displayed initially. To specify the locale code of your JSON file, you must name it [`localecode`].json
147+
148+
Below is an example of what your section's folder may look like:
149+
150+
```json
151+
./sections/article-template
152+
├── locales
153+
│   ├── de.json
154+
│   ├── en.json
155+
│   ├── es.json
156+
│   ├── fr.json
157+
│   ├── it.json
158+
│   ├── ja.json
159+
│   └── pt-BR.json
160+
├── schema.json
161+
└── template.liquid
162+
```
163+
164+
This will all get combined and outputted into a single `article-template.liquid`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`section that has template living in folders with schema.json and no locales 1`] = `
4+
"
5+
<section>
6+
<h2>{{ section.name }}</h2>
7+
</section>
8+
9+
"
10+
`;
11+
12+
exports[`section that has template living in folders with schema.json and no locales 2`] = `
13+
"{% schema %}
14+
{
15+
\\"name\\": \\"Test\\"
16+
}
17+
{% endschema %}"
18+
`;
19+
20+
exports[`section that has template living in folders with schema.json and no locales 3`] = `
21+
"
22+
<section>
23+
<h2>{{ section.name }}</h2>
24+
</section>
25+
26+
{% schema %}
27+
{
28+
\\"name\\": \\"Test\\"
29+
}
30+
{% endschema %}"
31+
`;
32+
33+
exports[`sections that have templates living in folders with a schema.json and locales to go with aswell 1`] = `
34+
"<div class=\\"article\\" data-section-id=\\"{{ section.id }}\\" data-section-type=\\"article-template\\">
35+
<h1 class=\\"article__title h2\\">{{ article.title }}</h1>
36+
</div>
37+
"
38+
`;
39+
40+
exports[`sections that have templates living in folders with a schema.json and locales to go with aswell 2`] = `
41+
Object {
42+
"name": Object {
43+
"de": "Posts",
44+
"en": "Posts",
45+
"es": "Publicaciones",
46+
"fr": "Articles",
47+
"it": "Articoli",
48+
"ja": "投稿",
49+
"pt-BR": "posts",
50+
},
51+
"settings": Array [
52+
Object {
53+
"id": "image_parallax",
54+
"info": Object {
55+
"de": "Wird nur auf dem Desktop angezeigt",
56+
"en": "Only shows on desktop",
57+
"es": "Solo se muestra en el escritorio",
58+
"fr": "Ne s'affiche que sur le bureau",
59+
"it": "Appare solo su desktop",
60+
"ja": "デスクトップにのみ表示する",
61+
"pt-BR": "Aparece apenas na área de trabalho",
62+
},
63+
"label": Object {
64+
"de": "Parallax-Animation für Bild anzeigen",
65+
"en": "Show image parallax animation",
66+
"es": "Mostrar animación de paralaje de imagen",
67+
"fr": "Afficher l'animation en parallaxe",
68+
"it": "Mostra animazione parallasse immagine",
69+
"ja": "画像のパララックスアニメーションを表示する",
70+
"pt-BR": "Exibir animação de paralaxe da imagem",
71+
},
72+
"type": "checkbox",
73+
},
74+
],
75+
}
76+
`;
77+
78+
exports[`sections with no seperate schemas, with liquid files that just need to be copied over 1`] = `
79+
"
80+
<section>
81+
<h2>{{ section.name }}</h2>
82+
</section>
83+
84+
{% schema %}
85+
{
86+
\\"name\\": \\"Test\\",
87+
}
88+
{% endschema %}
89+
"
90+
`;
91+
92+
exports[`sections with no seperate schemas, with liquid files that just need to be copied over 2`] = `
93+
"
94+
<section>
95+
<h2>{{ section.name }}</h2>
96+
</section>
97+
98+
{% schema %}
99+
{
100+
\\"name\\": \\"Test\\",
101+
}
102+
{% endschema %}
103+
"
104+
`;

packages/slate-sections-plugin/__tests__/fixtures/normalsections/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Posts",
3+
"settings": {
4+
"image_parallax": {
5+
"label": "Parallax-Animation für Bild anzeigen",
6+
"info": "Wird nur auf dem Desktop angezeigt"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Posts",
3+
"settings": {
4+
"image_parallax": {
5+
"label": "Show image parallax animation",
6+
"info": "Only shows on desktop"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Publicaciones",
3+
"settings": {
4+
"image_parallax": {
5+
"label": "Mostrar animación de paralaje de imagen",
6+
"info": "Solo se muestra en el escritorio"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Articles",
3+
"settings": {
4+
"image_parallax": {
5+
"label": "Afficher l'animation en parallaxe",
6+
"info": "Ne s'affiche que sur le bureau"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Articoli",
3+
"settings": {
4+
"image_parallax": {
5+
"label": "Mostra animazione parallasse immagine",
6+
"info": "Appare solo su desktop"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "投稿",
3+
"settings": {
4+
"image_parallax": {
5+
"label": "画像のパララックスアニメーションを表示する",
6+
"info": "デスクトップにのみ表示する"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "posts",
3+
"settings": {
4+
"image_parallax": {
5+
"label": "Exibir animação de paralaxe da imagem",
6+
"info": "Aparece apenas na área de trabalho"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": {
3+
"t": "name"
4+
},
5+
"settings": [
6+
{
7+
"type": "checkbox",
8+
"id": "image_parallax",
9+
"label": {
10+
"t": "settings.image_parallax.label"
11+
},
12+
"info": {
13+
"t": "settings.image_parallax.info"
14+
}
15+
}
16+
]
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="article" data-section-id="{{ section.id }}" data-section-type="article-template">
2+
<h1 class="article__title h2">{{ article.title }}</h1>
3+
</div>

packages/slate-sections-plugin/__tests__/fixtures/seperatejsonsections/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Test"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
<section>
3+
<h2>{{ section.name }}</h2>
4+
</section>
5+

packages/slate-sections-plugin/__tests__/fixtures/startersections/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
<section>
3+
<h2>{{ section.name }}</h2>
4+
</section>
5+
6+
{% schema %}
7+
{
8+
"name": "Test",
9+
}
10+
{% endschema %}

0 commit comments

Comments
 (0)