
By Matt Milburn
Populate custom snippets into API response data.
released February 7, 2025
npm install strapi-plugin-snippetscode automatically updates all of your data entries in Strapi with the new code.yarn add strapi-plugin-snippets@latestDon't forget to restart or rebuild your Strapi app when installing a new plugin.
| Strapi version | Plugin version |
|---|---|
| v5 | v2 |
| v4 | v1 |
| property | type (default) | description |
|---|---|---|
| contentTypes | object (null) | An optional config object that allows configuring which additional models should or should not support snippets. |
| contentTypes.allow | array (null) | An array of either model, plugin, or component UIDs which support snippets. |
| contentTypes.deny | array (null) | An array of either model, plugin, or component UIDs which DO NOT support snippets. |
| ignoreUnmatched | bool (false) | If true, unmatched codes will remain unparsed in response data, otherwise they are replaced with an empty string. |
| uppercase | bool (true) | If true, the plugin will apply uppercase formatting to the code value when a snippet is created or updated. |
contentTypesBy default, all API models and components are parsed by the snippets plugin. However, models defined in plugins are not automatically included.
Use the allow and deny props of contentTypes to include or exclude certain UIDs which can include API content types, plugin content types, and components.
// ./config/plugins.ts`
export default () => ({
snippets: {
config: {
contentTypes: {
allow: ['plugin::menus.menu', 'plugin::menus.menu-item'],
deny: ['api::example.example', 'category.example-component'],
},
},
},
});| UID type | Format |
|---|---|
| API model | api::name.name |
| Plugin model | plugin::name.name |
| Component model | category.name |
ignoreUnmatchedIf true, unmatched codes will remain unparsed in response data, otherwise they are replaced with an empty string.
// ./config/plugins.ts`
export default () => ({
snippets: {
config: {
ignoreUnmatched: false,
},
},
});Consider a scenario where we have 2 snippets, one called SNIPPET_ONE and another called SNIPPET_TWO and both will be replaced with the string "foobar". We also have an entity with a title and summary fields where both fields use snippets.
The snippet named SNIPPET_MISSING does not actually exist and will either be replaced with an empty string or ignored completely. See the example below.
{
"data": {
"id": 1,
"title": "Testing snippet {SNIPPET_ONE} and {SNIPPET_MISSING}.",
"summary": "Testing snippet {SNIPPET_ONE} and {SNIPPET_TWO}."
},
"meta": {}
}ignoreUnmatched set to true{
"data": {
"id": 1,
"title": "Testing snippet foobar and {SNIPPET_MISSING}.",
"summary": "Testing snippet foobar and foobar."
},
"meta": {}
}ignoreUnmatched set to false{
"data": {
"id": 1,
"title": "Testing snippet foobar and .",
"summary": "Testing snippet foobar and foobar."
},
"meta": {}
}uppercaseIf true, the plugin will apply uppercase formatting to the code value when a snippet is created or updated. This is also enforced while typing in the code input field.
// ./config/plugins.ts`
export default () => ({
snippets: {
config: {
uppercase: false,
},
},
});While typing the snippet code you will notice the input value is masked to the following conditions:
Uniqueness is validated upon saving.
After updating the code value of a snippet, it will be automatically updated across your content so you do not have to revisit every entry to update the snippet code to the new value.
To use snippets in your content, you can type a snippet code wrapped in curly braces {} in string or text fields and it will be replaced with the replacement value when it appears in API response data.
Remember to rebuild your app after making changes to some config or other code.
yarn build
# OR
yarn developFollow the migration guides to keep your snippets plugin up-to-date.
If you are enjoying this plugin and feel extra appreciative, you can buy me a beer or 3 πΊπΊπΊ.
CURRENT_YEAR.Share your work with the community and get it listed in the Strapi ecosystem for everyone to discover and use.
Submit
