|
2 | 2 |  |
3 | 3 |  |
4 | 4 |  |
| 5 | + |
| 6 | +# Contentstack JavaScript Utils SDK: |
| 7 | + |
| 8 | +Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. Read More. |
| 9 | + |
| 10 | +This guide will help you get started with Contentstack JavaScript Utils SDK to build apps powered by Contentstack. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +To get started with JavaScript, you will need the following: |
| 15 | +- Node.js 10 or later |
| 16 | + |
| 17 | +## SDK Installation and Setup |
| 18 | +> Note: If you are using JavaScript Contentstack SDK, you don’t need to run the command as ‘@contentstack/utils’ is already imported in the SDK. |
| 19 | +
|
| 20 | +Use the following command to install Contentstack JavaScript Utils SDK: |
| 21 | +```sh |
| 22 | +npm i @contentstack/utils |
| 23 | +``` |
| 24 | +## Usage |
| 25 | +Let’s learn how you can use Utils SDK to render embedded items. |
| 26 | + |
| 27 | +### Create Render Option |
| 28 | +To render embedded items on the front-end, use the renderOptions function, and define the UI elements you want to show in the front-end of your website, as shown in the example below: |
| 29 | +```js |
| 30 | +const renderOptions = { |
| 31 | + //to render block-type embedded items |
| 32 | + block: { |
| 33 | + 'product': (entry, metadata) => { |
| 34 | + `<div> |
| 35 | + <h2 >${entry.title}</h2> |
| 36 | + <img src=${entry.product_image.url} alt=${entry.product_image.title}/> |
| 37 | + <p>${entry.price}</p> |
| 38 | + </div>` |
| 39 | + }, |
| 40 | + //to render the default |
| 41 | + '$default': (entry, metadata) => { |
| 42 | + `<div> |
| 43 | + <h2>${entry.title}</h2> |
| 44 | + <p>${ntry.description}</p> |
| 45 | + </div>` |
| 46 | + } |
| 47 | + }, |
| 48 | +//to display inline embedded items |
| 49 | + inline: { |
| 50 | + '$default': (entry) => { |
| 51 | + `<span><b>${entry.title}</b> - ${entry.description}</span>` |
| 52 | + } |
| 53 | + }, |
| 54 | + //to display embedded items inserted via link |
| 55 | + link: (entry, metadata) => { |
| 56 | + `<a href="${metadata.attributes.href}">${metadata.text}</a>` |
| 57 | + }, |
| 58 | + // to display assets |
| 59 | + display: (asset, metadata) => { |
| 60 | + `<img src=${metadata.attributes.src} alt=${metadata.alt} />` |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Basic Queries |
| 66 | +Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry. |
| 67 | + |
| 68 | +### Fetch Embedded Item(s) from a Single Entry |
| 69 | +To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the includeEmbeddedItems and Contentstack.Utils.render functions as shown below: |
| 70 | +```js |
| 71 | +import * as Contentstack from 'contentstack' |
| 72 | +const stack = Contentstack.Stack({ |
| 73 | + api_key: '<API_KEY>', |
| 74 | + delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', |
| 75 | + environment: '<ENVIRONMENT>'}) |
| 76 | + |
| 77 | +stack.ContentType('<CONTENT_TYPE_UID>') |
| 78 | + .Entry('<ENTRY_UID>') |
| 79 | + .toJSON() |
| 80 | + .includeEmbeddedItems() // include embedded items |
| 81 | + .fetch() |
| 82 | + .then(entry => { |
| 83 | + Contentstack.Utils.render({ entry, renderOption }) |
| 84 | + }) |
| 85 | +``` |
| 86 | +If you have multiple RTE fields in an entry and want to fetch the embedded items from a particular RTE field, you need to provide a path of those RTE fields. |
| 87 | + |
| 88 | +Refer to the example code below: |
| 89 | +```js |
| 90 | +//code to render embedded item from an RTE field and from another RTE field nested within a group field |
| 91 | +Contentstack.Utils.render({ entry, path: ["rte_fieldUid", "group.rtefieldUID"], renderOption }) |
| 92 | +``` |
| 93 | +### Fetch Embedded Item(s) from Multiple Entries |
| 94 | +To get embedded items from multiple entries, you need to provide the content type UID. You can also use the path variable in case the entries have multiple RTE fields. |
| 95 | +```js |
| 96 | +import Contentstack from 'contentstack' |
| 97 | +const stack = Contentstack.Stack({ |
| 98 | + api_key: '<API_KEY>', |
| 99 | + delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', |
| 100 | + environment: '<ENVIRONMENT>'}) |
| 101 | + |
| 102 | +stack.ContentType('<CONTENT_TYPE_UID>') |
| 103 | + .Query() |
| 104 | + .toJSON() |
| 105 | + .where('title', '<entry_title_to_search>') |
| 106 | + .includeEmbeddedItems() // include embedded items |
| 107 | + .find() |
| 108 | + .then(result => { |
| 109 | + result.forEach(entry => { |
| 110 | + Contentstack.Utils.render({ entry, path: ['rte', 'group.rtefieldUID'], renderOption }) |
| 111 | + }) |
| 112 | + }) |
| 113 | +``` |
0 commit comments