Insert code samples to content (example)
2 min read
Download PDFLearn how to model and insert code samples in Kontent.ai.
Table of contents
Create a content type for the code sample
The first step is to create a content type for your piece of code.
- In Content model, click Create new.
- Type Code sample in Content type name.
- Add a Text element for the code itself.
- Name it Code.
- Make it required.
- Add a Multiple choice element for selecting the code's programming language.
- Name it Programming language.
- Add options for every programming language you want to support.
- Set the element to show its options as Radio buttons.
- Make it required.
- Click Save changes.
The result can look similar to what you see in the image below.

Insert code sample in your content
When you have your code sample defined, you can add it to one of your texts as a component.
- In Content & assets, open a content item with a rich text element.
- In the rich text, place your cursor to where you want to insert your code.
- On the editor toolbar, click Insert... and then Insert new component.
- Choose Code sample from the list of content types.
- Fill in the two elements.
Your code sample now exists as a component within the item you're editing.
From single-use to re-use
If you later find another use for the content in your component, you can always convert it to a content item by clicking Reuse as content item.
This conversion cannot be undone. If done by accident, archive the new item and create your component again.
After you finish typing the code, you can continue editing the content item. All changes are saved automatically.
