2014-07-25 2 views
1

NodeJS와 Jade를 사용하여 작은 CMS 응용 프로그램을 작성했습니다. 다양한 이유 때문에 Jade 템플릿 파일을 디스크가 아닌 데이터베이스 (예 : MongoDB)에 저장하려고합니다.디스크에 저장되지 않은 Jade 템플릿은 어떻게 확장 할 수 있습니까?

이것은 단순한 자체 포함 비취 템플릿에서 잘 작동합니다. 데이터베이스에서 옥 문자열을 간단히 가져올 수 있으며 jade.compile입니다.

그러나 템플릿이 다른 템플릿 extends 인 경우 디스크에 내 템플릿을 저장해야합니다. jade.js에서 :

parseExtends: function(){ 
    var fs = _dereq_('fs'); 

    var path = this.resolvePath(this.expect('extends').val.trim(), 'extends'); 
    if ('.jade' != path.substr(-5)) path += '.jade'; 

    var str = fs.readFileSync(path, 'utf8'); 
    ... 

그래서 extends 키워드는 명시 적으로 확장 된 템플릿을 디스크에 저장됩니다 가정합니다.

제이드 템플릿이 데이터베이스 또는 다른 저장소에 저장된 다른 템플릿을 확장하는 가장 쉬운 방법은 무엇입니까?

+0

나는 github 프로젝트에서 Jade 프로젝트를 포크하고 기능을 구현하려고합니다. 그렇지 않으면 요청을 가져올 수 있습니다. – InferOn

답변

2

by overriding parseExtends and parseImports 수 있습니다.

그러나 Jade는 모든 컴파일을 동 기적으로 수행하기 때문에 일종의 동기식 저장 장치가 필요합니다. 즉, 콜백이 없습니다.

나는 결국 모든 템플릿 프리 페치와 templateMap 객체에 저장하여이 문제를 "해결"

{ 
    "path": "/templates/frontpage.jade", 
    "content": "<html><body>Here's some Jade</body></html>" 
}, 
... 

그런 무시 parseExtends 그래서 그들은 디스크에 templateMap에서 템플릿 경로를하지 보이는 것을 parseImports :

jade.Parser.prototype.parseExtends = function() { 
    var path = this.expect('extends').val.trim(); 
    path = Path.normalize(path).replace("\\", "/"); // Necessary on Windows 
    var self = this; 
    var str = templatesMap[path]; 
    if (!str) { 
     return callback(new RendererError("Could not find template " + path)); 
    } 
    var parser = new this.constructor(str, path, this.options); 

    parser.blocks = this.blocks; 
    parser.contexts = this.contexts; 
    self.extending = parser; 
    return new Nodes.Literal(''); 
} 
관련 문제