Skip navigation

Insert code samples to content (example)

2 min read
Download PDF

Learn 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.

    1. In Content model, click Create new.
    2. Type Code sample in Content type name.
    3. Add a Text element for the code itself.
      1. Name it Code.
      2. Make it required.
    4. Add a Multiple choice element for selecting the code's programming language.
      1. Name it Programming language.
      2. Add options for every programming language you want to support.
      3. Set the element to show its options as Radio buttons.
      4. Make it required.
    5. Click Save changes.

    The result can look similar to what you see in the image below.

    Code sample content type.

    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.

    1. In Content & assets, open a content item with a rich text element.
    2. In the rich text, place your cursor to where you want to insert your code.
    3. On the editor toolbar, click and then .
    4. Choose Code sample from the list of content types.
    5. 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 .

    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.

    Code sample inserted as component in a content item.

    What's next?

    You can create single-use code samples or reuse them in multiple places if needed. In any case, you need to make sure the code sample is displayed correctly within the text and add syntax highlight to improve readability.