2016-04-20 25 views
1

我有一个json数据文件,其中包含具有指定键的多个对象。使用Metalsmith和swig访问模型中的单个对象

{ 
    "berlin:" : { 
    "location": "Berlin", 
    "folder": "berlin-2016" 
    }, 
    "seattle" : { 
    "location": "Seattle ", 
    "folder": "seattle-2016" 
    } 
} 

在我的内容文件中,我想指定要使用的模型中的哪个对象,然后在swig中引用该对象。类似这样的:

--- 
model: 
    conference: conferences['berlin'] 
---  

{{ model.conference.location }} 

这可能吗?

+0

您可以使用自定义插件做你想做的事。 – Luksprog

回答

0

这对金属匠来说绝对是可能的。我没有你的构建过程的完整画面,但对于这个解决方案,您就必须使用metalsmith javascript api

./data.json

{ 
    "berlin:" : { 
    "location": "Berlin", 
    "folder": "berlin-2016" 
    }, 
    "seattle" : { 
    "location": "Seattle ", 
    "folder": "seattle-2016" 
    } 
} 

./build.js

// Dependencies 
var metalsmith = require('metalsmith'); 
var layouts = require('metalsmith-layouts'); 

// Import metadata 
var metadata = require('./data.json'); 

// Build 
metalsmith(__dirname) 
    // Make data available 
    .metadata(data) 

    // Process templates 
    .use(layouts('swig')) 

    // Build site 
    .build(function(err){ 
    if (err) throw err; 
    }); 

然后在要生成的根项目文件夹中运行node build.js。在您的模板中,data.json的数据将作为{{ berlin.location }}提供。

你也可以做到这一点没有JavaScript API的(我不建议,因为你失去了一些灵活性),在这种情况下,你可以使用一个插件(例如:metalsmith-json