https://github.com/thammin/freemarker-to-json2.js
Get data easier from freemarker templates :zap:
https://github.com/thammin/freemarker-to-json2.js
convert freemarker ftl javascript json transform
Last synced: 5 months ago
JSON representation
Get data easier from freemarker templates :zap:
- Host: GitHub
- URL: https://github.com/thammin/freemarker-to-json2.js
- Owner: thammin
- Created: 2017-02-17T06:03:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-28T06:07:40.000Z (over 9 years ago)
- Last Synced: 2025-08-24T20:23:48.462Z (10 months ago)
- Topics: convert, freemarker, ftl, javascript, json, transform
- Language: JavaScript
- Size: 9.77 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# freemarker-to-json2.js
This module take a yaml file or a json file and convert it to a freemarker file that output the data as valid json format.
Example:
Convert this
```yaml
name: paul
id: 1
favorite:
- type: food
value: ramen
- type: drink
value: milk-tea
```
or
```json
{
"name": "paul",
"id": 1,
"favorite": [{
"type": "food",
"value": "ramen"
}, {
"type": "drink",
"value": "milk-tea"
}]
}
```
to
```ftl
{
"name": ${get(name)},
"id": ${get(id)},
"favorite":
<@arrayFrame favorite; item>{
"type": ${get(item, 'type')},
"value": ${get(item, 'value')}
}@arrayFrame>
}
```
which the `get`, `arrayFrame` is just a convenient way to transform ftl data to valid json.
```ftl
<#-- A simple helper to convert ftl data to valid json value -->
<#function value input="">
<#if input?is_number>
<#return input?c>
<#elseif input?is_boolean>
<#return input?string>
<#elseif input?is_string>
<#return '"' + input?js_string + '"'>
<#elseif input?is_date>
<#return '"' + input?string["yyyy/MM/dd HH:mm:ss"] + '"'>
#if>
#function>
<#-- A lodash.get alike helper -->
<#function get object="" path="" default='""'>
<#if object?is_hash && path != "">
<#local childs = path?split(".")>
<#list childs as child>
<#if object[child]??>
<#local object = object[child]>
<#else>
<#return default>
#if>
#list>
#if>
<#return value(object)>
#function>
<#-- A simple helper to wrap freemarker `#list` with json array -->
<#macro arrayFrame items=[]>
<#compress>
[
<#list items as item>
<#nested item><#if item_has_next>,#if>
#list>
]
#compress>
#macro>
```
# Usage
```js
const transform = require('freemarker-to-json2')
transform('input.yaml', 'output.ftl')
.then(result => console.log(result)) // same as output.ftl
```
# Test
```sh
npm test
```