JavaScriptから複雑なJSONを動的に生成するテンプレートエンジンを必要としています。
もしおすすめのライブラリをご存知でしたらコメントください。
目的としているテンプレートエンジンの仕様ですが、以下を必要としています。
- JSONまたは文字列をテンプレートとし、JSONを入力値とする
- 出力結果はJSON
- テンプレート中の指定キーワードをJSONの入力値で差し替える
- foreach/if/unless/optional などの制御構文を持つ
から既存のライブラリを調査中です。
json-templates
サンプル1:
// Context values could be objects and arrays.consttemplate=parse("{{foo:baz}}");console.log(template.parameters);// Prints [{ key: "foo", defaultValue: "baz" }]console.log(template());// Prints "baz", using the default value.console.log(template({foo:{value:'bar'}}));// Prints { value: 'bar' } , using the given value.
サンプル2:
consttemplate=parse({match:{title:"{{myTitle}}"}});console.log(template.parameters);// Prints [{ key: "myTitle" }]console.log(template({myTitle:"test"}));// Prints { match: { title: 'test' } }
読み取れる大まかな仕様ですが、
- テンプレートは JSONまたは文字列
- 出力結果はJSONまたは文字列
- キーワード差し替え機能あり
- 制御構文なし
json-templater
json-templates
の派生ライブラリ。
機能を変えずに使い勝手を変えただけ。
サンプル1:
lettemplate={"magic_key_{{magic}}":{"key":"interpolation is nice {{value}}"}}letobject=require('json-templater/object');console.log(object(template,{magic:'key',value:'value'}));// =>// {// magic_key_key: {// key: 'interpolation is nice value'// }//}
@biothings-explorer/json-transformer
サンプル1:
constjson_doc={'ensemblgene':1017};// the template is a JSON object, with value as the field from json_doc to be transformed and the key as the field to be transformed toconsttemplate={'ensembl':'ensemblgene'};console.log(transform(json_doc,template));// returns {'ensembl': 1017}
サンプル2:
letjson_doc={'ensembl':{'gene':1017},'wikipathway':[{'id':'WP123','name':'aaa'},{'id':'WP1234','name':'aaaa'}]};lettemplate={'ensembl':'ensembl.gene','pathway':{'id':'wikipathway.id','name':'wikipathway.name'}};letres=transform(json_doc,template);//returns {'ensembl': 1017, 'pathway': [{'id': 'WP123', 'name': 'aaa'}, {'id': 'WP1234', 'name': 'aaaa'}]}
- テンプレートは JSON
- 出力結果はJSON
- キーワード差し替え機能あり
- 制御構文なし
json-map-transform
consttransform=require('json-map-transform');consttemplate={title:{path:'name',transform:(val)=>val.toUpperCase()},label:{path:['category','categories'],omitValues:['',undefined,'ERROR']},vendor:{path:'meta.vendor',default:'No vendor'},'meta.photos':{path:'photos',transform:(val)=>val.map(photo=>photo.photoUrl)},'meta.id':{path:'code'}}//The json objects to be transformedconstproduct1={name:'Hello world',code:'BOOK01',category:'books',price:'200',photos:[{title:'photo1',photoUrl:'http://photo1.jpg',isCover:true},{title:'photo2',photoUrl:'http://photo2.jpg'}],meta:{vendor:'Author name'}};console.log(transform(product1,template))// =>// {// "title": "HELLO WORLD",// "label": "books",// "vendor": "Author name",// "meta": {// "photos": [// "http://photo1.jpg",// "http://photo2.jpg"// ],// "id": "BOOK01"// }// }
- テンプレートは JSON
- 出力結果はJSON
- キーワード差し替え機能あり
- 制御構文なし
- テンプレート中に関数を直接記述できるのはちょっと面白い
jsonpath-template
constjpt=require('jsonpath-template');vartplString="Hello there [[$.person.name]], isn't it a [[$.daytype]] day?";varjson={person:{name:"Adam"},daytype:"nice"};vartemplate=newjpt(tplString);console.log(template.tags("[[","]]").apply(json));// => Hello there Adam, isn't is a nice day?
- テンプレートはテキスト
- 出力結果はテキスト
- キーワード差し替え機能あり
- 制御構文なし
- タグの変更機能あり
jsonapter
サンプル1
varbbj2j=require('jsonapter');varj2j=bbj2j.instance();varinput={a:{b:{c:'value_0'},c:'value_2',d:'value_1'}};varr=j2j.run(template,input);console.log(r);// => {dest_a: 'value_2', dest_b: {dest_b0: 'VALUE_0', dest_b1: 'VALUE_1'}}
サンプル2
varnameTemplate={arrayContent:[{dataKey:'familyName'},{dataKey:'givenName'}]};vartemplate={content:{name:nameTemplate,age:{value:function(input){return2015-input;},dataKey:'birthYear'}}};varr=j2j.run(template,{familyName:'DOE',givenName:'JOE',birthYear:1980});console.log(r);// => {name: ['DOE', 'JOE'], age: 35}
サンプル3
var_=require('lodash');vartemplate={content:{dest_a:{dataKey:'a'},dest_b:{dataKey:'b',existsWhen:_.partialRight(_.has,'c')}},existsWhen:'public'};varr0=j2j.run(template,{a:'value_a',b:'value_b',public:true});console.log(r0.dest_a);// => 'value_a'console.log(r0.dest_b);// => undefinedvarr1=j2j.run(template,{a:'value_a',b:'value_b',c:0,public:true});console.log(r1.dest_a);// 'value_a'console.log(r1.dest_b);// 'value_b'varr2=j2j.run(template,{a:'value_a',b:'value_b',c:0});console.log(r2);// null because public is not presentvarr3=j2j.run(template,{a:'value_a',b:'value_b'},{public:true,c:0});console.log(r3.dest_a);// => 'value_a'console.log(r3.dest_b);// => 'value_b'
- テンプレートはJSON
- 出力結果はJSON
- キーワード差し替え機能あり
- 制御構文あり
- 書き方に癖がある
- lodashが必要?
templatizejs
vartemplatize=require('templatizejs')varjson={fizzBuzz:'{{fizz}}{{buzz}}',}varsecondaries=[{fizz:'Fizz'},{buzz:'Buzz'}]console.log(templatize.json(json,secondaries))// => { fizzBuzz: 'FizzBuzz' }
- テンプレートはJSON
- 出力結果はJSON
- キーワード差し替え機能あり
- 制御構文なし
- タグの変更機能あり