-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: Creating a Template Compiler | ||
position: 40 | ||
weight: 40 | ||
menu: | ||
docs: | ||
parent: guides | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: JSX in the Browser | ||
weight: 100 | ||
menu: | ||
docs: | ||
parent: start | ||
--- | ||
|
||
# JSX in the Browser | ||
|
||
Because Netlify CMS uses React, and React is most often used with [JSX](#), customizing the CMS | ||
generally involves a build system to transform the JSX into JavaScript. If you prefer to write JSX | ||
(say for custom widgets or preview templates) without using a build system, you can do so using | ||
[babel-standalone](https://github.com/babel/babel/tree/master/packages/babel-standalone): | ||
|
||
```html | ||
<script src="https://unpkg.com/react@16/umd/react.development.js"></script> | ||
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> | ||
<script type="text/babel"> | ||
const PreviewTemplate = () => <h1>Totally works.</h1> | ||
</script> | ||
``` | ||
|
||
Note the use of `react.development.js` - this is ideal during development for useful error messages. | ||
When you're ready for production, use `react.production.js`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
title: React Preview Templates | ||
weight: 60 | ||
menu: | ||
docs: | ||
parent: guides | ||
--- | ||
|
||
# React Preview Templates | ||
|
||
React is the default "template language" for previews. Each preview template is a component that | ||
accepts the entry and other data as `props`. For React based static site generators, like Gatsby and | ||
React Static, production site template components can often be reused as Netlify CMS preview | ||
components. | ||
|
||
**Unfamiliar with React?** You may want to [brush | ||
up](https://reactjs.org/docs/hello-world.html) on it before proceeding. | ||
|
||
**No build system?** If you want to write [JSX](#) as we do in the examples below, but are not using a | ||
build system, check out the [JSX in the browser](#) guide for tips. | ||
|
||
|
||
## Props | ||
|
||
The following props are available to React preview template components: | ||
|
||
### `props.entry` | ||
|
||
#### `props.entry.fields` | ||
|
||
An object containing all configured fields for the current entry, including nested | ||
fields. Each field object will contain up to three properties: | ||
|
||
**`value`:** the raw field value\ | ||
**`preview`:** _(optional)_ the HTML field preview\ | ||
**`data`:** _(optional)_ secondary field data\ | ||
|
||
```js | ||
const PostPreview = (props) => { | ||
const fields = props.entry.fields; | ||
return ( | ||
<div> | ||
<img src={fields.cover.data.src}/> | ||
<h1>{fields.title.value}</h1> | ||
<div>{fields.author.name.value}</div> | ||
<div>{fields.date.value}</div> | ||
<div>{fields.body.preview}</div> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
### `props.entry.collection` | ||
|
||
The name of the collection containing the current entry. | ||
|
||
### `props.collections` | ||
|
||
`props.collections` is an object containing all collections and their entries. The entry objects | ||
contain the same `fields` property as documented under [`props.fields`](#). | ||
|
||
```js | ||
const PostPreview = (props) => { | ||
const fields = props.entry.fields; | ||
const collections = props.collections; | ||
return ( | ||
<div> | ||
<h1>{fields.title.value}</h1> | ||
<div>{collections.authors.fields} | ||
<div>{fields.author.name.value}</div> | ||
<div>{fields.date.value}</div> | ||
<div>{fields.body.preview}</div> | ||
</div> | ||
) | ||
} | ||
``` |