|
| 1 | +--- |
| 2 | +title: "Dynamic content" |
| 3 | +date: 2025-09-09 |
| 4 | +tags: ["Contribute"] |
| 5 | +showTableOfContents: true |
| 6 | +showAuthor: false |
| 7 | +authors: |
| 8 | + - "kirill-chalov" |
| 9 | +--- |
| 10 | + |
| 11 | +For pages that change frequently, it is useful to bypass git and inject the updated content dynamically instead of creating PRs every time. As of now, this is helpful for hardware and software product support pages that can update weekly. This article will provide a workflow for injecting dynamic content focusing on the product support pages. |
| 12 | + |
| 13 | + |
| 14 | +## Plan your page |
| 15 | + |
| 16 | +First of all, the content on a product support page should be classified into the following types: |
| 17 | + |
| 18 | +- **Static**: Rarely changing content |
| 19 | + - Committed to the git repo |
| 20 | + - Stored under |
| 21 | + `content/{hardware|software}/product-x/index.md` |
| 22 | +- **Dymanic**: Frequently changing content |
| 23 | + - Dynamically injected into HTML pages using the [dynamic-block](https://github.com/espressif/developer-portal/blob/main/layouts/shortcodes/dynamic-block.html) shortcode |
| 24 | + - Stored in the root of the web server under |
| 25 | + `./persist/{hardware|software}/product-x/product-x.json` |
| 26 | + - Updated either manually or using CI |
| 27 | + - For uploading to the web server, talk to the project maintainers |
| 28 | + |
| 29 | +The dynamic content must be stored in the root of the web server under `persist`. All other folders are fully overwritten daily. |
| 30 | + |
| 31 | + |
| 32 | +## Arrange your content |
| 33 | + |
| 34 | +From the hints in [Plan your page](#plan-your-page), it is not hard to understand the idea: |
| 35 | + |
| 36 | +- In your markdown file where a dynamic part needs to be injected, you add a `dynamic-block` shortcode with part's `jsonKey` |
| 37 | +- In your JSON file, you add all dynamic parts in markdown |
| 38 | + |
| 39 | +### Simplified syntax |
| 40 | + |
| 41 | +As you can see from [Example files](#example-files) below, raw markdown lists (`feature_list`) and tables (`periph_support_table`) are not reasonable to store in JSON. A simplified syntax provides a way to strip unnecessary characters for storing, and add them back during rendering. This way, the JSON source is easier to read and `dynamic-block` still renders it correctly. |
| 42 | + |
| 43 | +To use simplified syntax, mark the `jsonKey` accordingly. In each case, the raw and simplified versions render identically: |
| 44 | + |
| 45 | +- **Simplified list** -- append `list_simple` to its `jsonKey`<br> |
| 46 | + Output: `feature_list` = `feature_list_simple` |
| 47 | +- **Simplified table** -- append `table_simple` to its `jsonKey`<br> |
| 48 | + Output: `periph_support_table` = `periph_support_table_simple` |
| 49 | + |
| 50 | +### Emojis |
| 51 | + |
| 52 | +In JSON, include emojis as Unicode characters ✅ instaed of `: white_check_mark :`. This considerably reduces the render time of injected content. |
| 53 | + |
| 54 | +### Example files |
| 55 | + |
| 56 | +Git repo: `content/software/product-x/index.md` |
| 57 | +``` |
| 58 | +--- |
| 59 | +title: "Product X" |
| 60 | +date: 2025-08-28 |
| 61 | +--- |
| 62 | +
|
| 63 | +**Last updated:** {{</* dynamic-block contentPath="persist/software/product-x/product-x.json" jsonKey="timestamp" */>}} |
| 64 | +
|
| 65 | +This is a product status page for Product X. |
| 66 | +
|
| 67 | +The following features are supported as of now: |
| 68 | +
|
| 69 | +{{</* dynamic-block contentPath="persist/software/product-x/product-x.json" jsonKey="feature_list_simple" */>}} |
| 70 | +
|
| 71 | +## Peripheral support table |
| 72 | +
|
| 73 | +{{</* dynamic-block contentPath="persist/software/product-x/product-x.json" jsonKey="periph_support_table_simple" */>}} |
| 74 | +``` |
| 75 | + |
| 76 | +Web server: `persist/software/product-x/product-x.json` |
| 77 | + |
| 78 | +```json |
| 79 | +{ |
| 80 | + "timestamp": "2025-08-28T00:07:19.716630Z", |
| 81 | + "feature_list": "- Supported SDKs\n - ✅ [ESP-IDF](https://github.com/espressif/esp-idf/)\n - ⏳ SDK 2", |
| 82 | + "periph_support_table": "| Peripheral | ESP32 |\n| :--- | :---: |\n| UART | ✅ |\n| LCD | ❌ |", |
| 83 | + "feature_list_simple": [ |
| 84 | + "- Supported SDKs", |
| 85 | + " - ✅ [ESP-IDF](https://github.com/espressif/esp-idf/)", |
| 86 | + " - ⏳ SDK 2" |
| 87 | + ], |
| 88 | + "periph_support_table_simple": [ |
| 89 | + "Peripheral,ESP32", |
| 90 | + ":---,:---:", |
| 91 | + "UART,✅", |
| 92 | + "LCD,❌" |
| 93 | + ] |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +The final page with the dynamic content should look somewhat like this: |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +<span style="font-size:2em; font-weight:bold;">Product X</span> |
| 102 | + |
| 103 | +**Last updated:** 28 Aug 2025, 8:07 am |
| 104 | + |
| 105 | +This is a product status page for Product X. |
| 106 | + |
| 107 | +The following features are supported as of now: |
| 108 | + |
| 109 | +- Supported SDKs |
| 110 | + - ✅ [ESP-IDF](https://github.com/espressif/esp-idf/) |
| 111 | + - ⏳ SDK 2 |
| 112 | + |
| 113 | +<span style="font-size:1.5em; font-weight:bold;">Peripheral support table</span> |
| 114 | + |
| 115 | +| Peripheral | ESP32 | |
| 116 | +| :--- | :---: | |
| 117 | +| UART | ✅ | |
| 118 | +| LCD | ❌ | |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | + |
| 123 | +## Test dynamic content |
| 124 | + |
| 125 | +Test your `.json` file locally before uploading to the web server: |
| 126 | + |
| 127 | +- In your git repo, place your `.json` file at the same path as on the server: |
| 128 | + ```sh |
| 129 | + 📂 content/software/ |
| 130 | + ├── 📝 _index.md |
| 131 | + └── 📂 product-x/ |
| 132 | + ├── 📝 index.md |
| 133 | + └── 🧩 persist/software/product-x/product-x.json # remove after testing |
| 134 | + ``` |
| 135 | +- In your git repo's `layouts/shortcodes/dynamic-block.html`, adjust the toggle for testing: |
| 136 | + ```javascript |
| 137 | + {{ $localMode := true }} <!-- change to true for local --> |
| 138 | + ``` |
| 139 | + |
| 140 | +After you run `hugo server` locally, the JSON content should be injected dynamically on your page. |
| 141 | + |
| 142 | +**If you update JSON**, do this for the changes to show up: |
| 143 | + |
| 144 | +- Restart `hugo server` |
| 145 | +- Refresh your browser tab |
| 146 | +- If no effect: clear the page cache |
0 commit comments