Using a Bookshop component on your Eleventy site is very similar to using a regular Liquid include, with a few small differences.
The Bookshop tag#
The Bookshop plugin provides a bookshop
tag for use in your layouts.
If you've been following along with this guide, add the following snippet to a layout to use the sample component we created:
This component matches the syntax of the standard Liquid include, but takes a Bookshop component name rather than a file path.
The Bookshop name of a component is the path to its directory.
The name for components/sample/sample.eleventy.liquid
is sample
,
and the name for components/generic/button/button.eleventy.liquid
would be generic/button
.
There are no limitations on referencing Bookshop comonents from other components — the {% bookshop %}
tag can be used anywhere
on your site or in your components.
Bookshop's bind syntax#
Often when working with Bookshop components your data will live in a front-matter object, with all keys passed through. This might end up looking like:
Passing through each key can quickly become unwieldy. To help with this, Bookshop provides the bind attribute:
This functions similar to the spread operator in JavaScript, passing all object keys as props to the component.
Rendering dynamic components#
The Bookshop tag supports interpolating Liquid, so if you have your component name in a variable you can use:
Later in this guide we'll cover creating CloudCannon Structures from your Bookshop components.
When doing do, the generated Structures will automatically include this _bookshop_name
value for you to use.
Passing Data to Bookshop Components#
In order to live edit Bookshop components, Bookshop needs a clear path between a component and the data it draws from.
In general, you should avoid adding logic around your Bookshop components in your site layouts, and instead move that logic into a Bookshop component or helper.
For example:
This component can make the connection between the text
prop and the page's front matter,
and will work as expected in the visual editor.
This component doesn't have the context to find the origin of the text
prop,
and will error in the visual editor.
(Reassignments inside Bookshop components will work correctly).