Sitecore Next.js Placeholder
A placeholder in Sitecore is a section of the page where we wish to allow content editors to add components to.
So far we've only been able to add components to the jss-main
placeholder. In a typical article page, you would have a 2 column layout with
main content in the middle and some additional content on the side.
For this lesson, we will create two placeholders: main-content
and aside
.
Create placeholder settings
- Navigate to
/sitecore/layout/Placeholder Settings/Project/xmnextjs
- Insert a placeholder
aside
- Add
CallToAction
to Allowed Controls
- Add
- Insert a placeholder
main-content
- Add
Article
to Allowed Controls - Add
CallToAction
to Allowed Controls
- Add
Create rendering to hold the layout
- Navigate to
/sitecore/layout/Renderings/Project/xmnextjs
- Create a rendering folder
Structure
- Insert rendering
ArticleLayout
- Scroll to the bottom to
Layout Service Placeholders
- Add
main-content
andaside
- Scroll to the bottom to
- Once we've created this rendering, we'll need to allow this to be added to the
jss-main
placeholder- Navigate to
/sitecore/layout/Placeholder Settings/Project/xmnextjs/jss-main
- Remember the display name is
Main
- Remember the display name is
- Add
ArticleLayout
which you just created to the Allowed Controls
- Navigate to
Create the component
To create placeholders in our component, import the Placeholder
component from @sitecore-jss/sitecore-jss-nextjs
.
Make sure when specifying where the placeholder goes to match the name prop with the placeholder key specified under the placeholder settings.
import { Placeholder } from '@sitecore-jss/sitecore-jss-nextjs';
import { ComponentProps } from 'lib/component-props';
const ArticleLayout = ({ rendering }: ComponentProps): JSX.Element => {
return (
<div className="row">
<section className="col-8">
<Placeholder name="main-content" rendering={rendering} />
</section>
<aside className="col-4">
<Placeholder name="aside" rendering={rendering} />
</aside>
</div>
);
};
export default ArticleLayout;
Update page rendering
At this point, we can start rearranging the components to our article pages. We will update the Standard Values item so that all of our article pages are updated.
- Navigate to
/sitecore/templates/Project/xmnextjs/Article Route/__Standard Values
- Edit this item in Experience Editor
- Publish > Experience Editor
- Note: To get Experience Editor to work with Standard Values, add
&sc_jssapp=xmnextjs
to the URL wherexmnextjs
is the name of your site.
- You should see our previous components (from previous lessons)
Article
,CallToAction
, and checkered areas on the page. The checkered areas are our new placeholders. - Click somewhere on a blank spot to the right of the Article component.
Some options should show up. Click on theMove component
.- Notice there is only one placeholder where you can move this component to. Move it to the
main-content
placeholder.
- Notice there is only one placeholder where you can move this component to. Move it to the
- Select the
CallToAction
component and click on theMove component
- Notice there are multiple places where you can move this component to. Why is this?
- Move it to the
aside
placeholder
- Save the page and publish
- Visit any of your article pages and notice that your components are now side by side.
Knowledge check:
- What is a placeholder?
- How do we restrict what renderings are allowed in a placeholder?
- What is a placeholder key?