Skip to main content

How to use CKEditor Placeholder feature with API and Custom Function

Overview

Placeholders are a useful tool for creating flexible and customizable content. With them, we can easily adapt our content based on the situation. We can also maintain common text that is spread throughout multiple contents.

This tutorial will explain how to maintain common text or property that is spread throughout multiple contents using CKEditor Placeholder feature, API, and Custom Function. We will have two placeholders, my_name and my_address. These placeholders will be replaced with some values on the fly (in API request).

What you'll learn

You will learn how to set up WYSIWYG placeholder functionality by following these steps:

Prepare content structure

Prepare a content structure with one WYSIWYG extension field.

Click [Add] from the Content structure screen.

Image from Gyazo

Set the following:

ItemSetting
GeneralNameMy Content Structure
Field settingsField settingsField name: Content
Field settings: WYSIWYG
WYSIWYG placeholders: my_name,my_address

Note that placeholders should be eparated by commas (,) and without spaces after the commas.

Image from Gyazo

Image from Gyazo

Image from Gyazo

Create Contents

Create two contents within the content structure we created in the previous step. Make sure to put the placeholders into the content.

Click [Add] from the content list screen.

Image from Gyazo

Enter the following and click "Add":

ItemSetting
TitleMy First Content
ContentAny text using my_name and my_Address.
Once you set the placeholders, Placeholder is added to the WYSIWYG editor, so set the placeholders from here.

Image from Gyazo

Enter the following and click "Add":

ItemSetting
TitleMy Second Content
WYSIWYGAny text using my_name and my_Address.
Once you set the placeholders, Placeholder is added to the WYSIWYG editor, so set the placeholders from here.

Image from Gyazo

Create custom function

Create a custom function to replace my_name and my_address placeholders with real values.

Click [Operation] -> [Custom function].

Image from Gyazo

Click [Add].

Image from Gyazo

Enter as follows:

ItemSetting
TitleReplace placeholders in content
Identifierreplace_placeholders_in_content
ProcessThe following code
{* For every contents in the list *}
{foreach from=$json.list item='row' key='key'}
{* Replace placeholder my_name with "Diverta" *}
{assign var=json.list.$key.content
value=$json.list.$key.content|replace:"<span class=\"placeholder\">`$smarty.ldelim`my_name`$smarty.rdelim`</span>":'Diverta'}
{* Replace placeholder my_address with the real address value *}
{assign var=json.list.$key.content
value=$json.list.$key.content|replace:"<span class=\"placeholder\">`$smarty.ldelim`my_address`$smarty.rdelim`</span>":'Central Plaza 6F 1-1 Kaguragashi Shinjuku-ku, Tokyo, Japan'}

{/foreach}

{$json|@json_encode:$smarty.const.JSON_UNESCAPED_UNICODE}

Image from Gyazo

Once you have entered the information, click [Add] to add the custom processing.

Create API endpoint

Create one API endpoint with category Content, model Topics, and list Operation. Set topics_group_id parameter with the ID of content structure in the previous step.

Adding an endpoint

From the Endpoint List page, click [Add new endpoint].

Image from Gyazo

Create the following endpoint:

FieldSetting
Pathmy-content
CategoryContent
ModelTopics
Operationlist
topics_group_idID of the created content structure

Image from Gyazo

Adding post-processing to the endpoint

Use the custom function we made from the previous step as a post-process for the API endpoint. We can also add an allow list post-processing to simplify the API response

From the Endpoint List page, click [Post-processing].

Image from Gyazo

To simplify the API response, add the "Allow List" post-process and configure it as follows:

FieldSetting
Allow listlist.subject
list.content
Custom functionReplase placeholders in content

Image from Gyazo

Once the settings are configured, click [Save] to save the post-processing configuration.

See the result

Try to fetch the API endpoint in Swagger and see the result.

From the Endpoint List page, click [SwaggerUI].

Image from Gyazo

Click [Try it out] for the created endpoint.

Image from Gyazo

Click [Execute].

Image from Gyazo

Placeholders have been replaced with the real values.

Image from Gyazo

More information


Support

If you have any other questions, please contact us or check out Our Slack Community.