diff --git a/doc/guide/ko/template_engine_wrappers.md b/doc/guide/ko/template_engine_wrappers.md new file mode 100644 index 00000000..6fc032f2 --- /dev/null +++ b/doc/guide/ko/template_engine_wrappers.md @@ -0,0 +1,199 @@ + +# 템플릿 엔진 래퍼 + + + + +나만의 템플릿 엔진 래퍼를 만드는 것은 쉽습니다. 이 짤막한 튜토리얼에서는, [ss-hogan](https://github.com/socketstream/ss-hogan)을 살펴볼 것입니다. ss-hogan은 트위터의 [Hogan library](http://twitter.github.com/hogan.js/)용 래퍼입니다. + + + +[ss-hogan repository]([ss-hogan](https://github.com/socketstream/ss-hogan)) 이곳에 보면 템플릿 엔진 래퍼의 파일 구조를 볼 수 있습니다. 핵심 파일인 `engine.js`을 함께 살펴 봅시다: + + + +```자바스크립트 +/* engine.js */ + +// 소켓스트림 0.3 용 호건 템플릿 엔진 래퍼 + + +var fs = require('fs'), + path = require('path'), + hogan = require('hogan.js'); + +exports.init = function(ss, config) { + + // 클라이언트 측에 Hogan VM을 보낸다. + var clientCode = fs.readFileSync(path.join(__dirname, 'client.js'), 'utf8'); + ss.client.send('lib', 'hogan-template', clientCode); + + return { + + name: 'Hogan', + + // 처음으로 Hogan 템플릿이 호출되었을 때, 코드를 사용할 수 있게 열어준다. + prefix: function() { + return ''; + }, + + // 템플릿을 함수로 컴파일해준 후, 이것을 ss.tmpl에 추가시켜준다. + process: function(template, path, id) { + + var compiledTemplate; + + try { + compiledTemplate = hogan.compile(template, {asString: true}); + } catch (e) { + var message = '! Error compiling the ' + path + ' template into Hogan'; + console.log(String.prototype.hasOwnProperty('red') && message.red || message); + throw new Error(e); + compiledTemplate = '
Error
'; + } + + return 't[\'' + id + '\']=new ht(' + compiledTemplate + ');'; + } + } +} +``` + + + +여기에서 `prefix`와 `suffix`는 각각 한 번씩 호출됩니다. 이 호출되는 것 사이에 이 템플릿 엔진이 핸들링하는 모든 파일에 대해서 `process`가 호출됩니다. + + + +이 예제에서, 래퍼는 템플릿을 서버 측에서 컴파일하고 있습니다. 그리고 나서 컴파일된 템플릿을 클라이언트 쪽에 넘겨주고 있습니다. `prefix`와 `suffix` 함수는 컴파일된 템플릿을 `