Skip to content

Commit 6d8965b

Browse files
authored
Merge pull request #75 from jverneaut/50-allow-creating-server-side-rendered-blocks
50 Allow creating server side rendered blocks
2 parents db547bf + bbcbca6 commit 6d8965b

19 files changed

Lines changed: 302 additions & 97 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { registerBlockType } from "@wordpress/blocks";
2+
3+
import Edit from "./edit.js";
4+
import metadata from "./block.json";
5+
6+
registerBlockType(metadata.name, {
7+
edit: Edit,
8+
save: () => null,
9+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useBlockProps } from "@wordpress/block-editor";
2+
import ServerSideRender from "@wordpress/server-side-render";
3+
4+
export default ({ attributes, setAttributes }) => {
5+
return (
6+
<section {...useBlockProps()}>
7+
<ServerSideRender
8+
block="custom/server-side-rendered"
9+
attributes={{ title: attributes.title }}
10+
></ServerSideRender>
11+
</section>
12+
);
13+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "custom/input",
3+
"title": "Input",
4+
"textdomain": "input",
5+
"$schema": "https://schemas.wp.org/trunk/block.json",
6+
"apiVersion": 3,
7+
"version": "0.1.0",
8+
"category": "theme",
9+
"example": {},
10+
"attributes": { "align": { "type": "string", "default": "full" } },
11+
"supports": { "html": false, "align": ["full"] },
12+
"editorScript": "file:./index.js",
13+
"render": "file:./render.php"
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<section <?php echo get_block_wrapper_attributes(); ?>>
2+
3+
</section>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<section>
2+
<!-- $$attributes$$ is subject to change and undocumented at the moment -->
3+
<server-block
4+
name="custom/server-side-rendered"
5+
title="$$attributes.title$$"
6+
></server-block>
7+
</section>

docs/docs/common/_experimental.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:::danger
2+
Experimental features are subject to change, or their may be dropped entirely, depending on feedback and stability. Use with caution.
3+
:::
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Experimental",
3+
"position": 5
4+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Experimental from "@site/docs/common/_experimental.md";
2+
import CodeExample from "@site/src/components/CodeExample";
3+
4+
# Raw javascript expressions
5+
6+
<Experimental />
7+
8+
Using the special `$$...$$` escape sequence, it is possible to use raw javascript expressions inside the editor.
9+
10+
<CodeExample>
11+
```
12+
<section>
13+
<!-- This is yet another experimental API not yet implemented, stay tuned... -->
14+
<block-attribute name="title" type="string"></block-attribute>
15+
<block-attribute name="postType" default="pages"></block-attribute>
16+
17+
<div data-display="editor">
18+
<!-- This will update the title attribute -->
19+
<input
20+
type="text"
21+
value="$${attributes.title}$$"
22+
onChange="$${(e) => setAttributes({ title: e.target.value })}$$"
23+
/>
24+
25+
<!-- This will update the postType attribute -->
26+
<select
27+
value="$${attributes.postType}$$"
28+
onChange="$${(e) => setAttributes({ postType: e.target.value })}$$"
29+
>
30+
<option value="posts">Posts</option>
31+
<option value="pages">Pages</option>
32+
</select>
33+
</div>
34+
35+
</section>
36+
```
37+
</CodeExample>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Experimental from "@site/docs/common/_experimental.md";
2+
import CodeExample from "@site/src/components/CodeExample";
3+
4+
# Server side blocks
5+
6+
<Experimental />
7+
8+
The `<server-side>` element can be used to render a block server side using the `<ServerSideRender />` component from `@wordpress/server-side-render` package.
9+
10+
To use it, first install the package with:
11+
12+
```
13+
npm install @wordpress/server-side-render --save
14+
```
15+
16+
## Rendering blocks on the server
17+
18+
To render a block on the server, use the `<server-block>` element with the `name` attribute telling it which block to render.
19+
It uses the same API as the `<block>` element to define attributes.
20+
21+
<CodeExample>
22+
```
23+
<section>
24+
<server-block name="custom/latest-posts-slider" postType="pages">
25+
<attribute name="numberOfPosts">5</attribute>
26+
</server-block>
27+
</section>
28+
```
29+
</CodeExample>
30+
31+
## When to use server side blocks
32+
33+
Server side blocks are particularly useful when having to query posts, build listing pages, more generaly whenever you need to build a data-rich block.
34+
35+
To build a block that display the latest posts from our site, we could first define a block in HTML that will be used in the editor and that will render itself on the server:
36+
37+
```html title="custom/latest-posts.html"
38+
<section>
39+
<server-block name="custom/latest-posts"></server-block>
40+
</section>
41+
```
42+
43+
Then, we can define a `render.php` override by creating a `latest-posts.render.php` file alongside our `latest-posts.html` file that will get the latest posts and render them:
44+
45+
```php title="custom/latest-posts.render.php"
46+
<?php
47+
48+
// You custom query here
49+
$latest_posts = get_posts();
50+
51+
?>
52+
53+
<section <?= get_block_wrapper_attributes(); ?>>
54+
<?php foreach ($posts as $post) : ?>
55+
<article>
56+
<h3><?= $post->post_title; ?></h3>
57+
<a href="<?= get_permalink($post); ?>">Read more</a>
58+
</article>
59+
<?php endforeach; ?>
60+
</section>
61+
62+
```

docs/docs/extra/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"label": "Extra",
3-
"position": 5
3+
"position": 6
44
}

0 commit comments

Comments
 (0)