Handlebars.js: ν•Έλ“€λ°”: "..." 속성은 λΆ€λͺ¨μ˜ "μžμ‹ μ˜ 속성"이 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— μ•‘μ„ΈμŠ€κ°€ κ±°λΆ€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2020λ…„ 01μ›” 18일  Β·  13μ½”λ©˜νŠΈ  Β·  좜처: handlebars-lang/handlebars.js

Handlebars 4.6.0λΆ€ν„° ν…œν”Œλ¦Ώμ€ 더 이상 ν”„λ‘œν† νƒ€μž… 속성 및 λ©”μ„œλ“œμ— μ•‘μ„ΈμŠ€ν•  수 μ—†μŠ΅λ‹ˆλ‹€(κΈ°λ³Έκ°’). λ…Όμ˜, 이유 및 κ·Όκ±°λŠ” #1633을 μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.

이전 λ™μž‘μ„ λ³΅μ›ν•˜κ³  μ œμ–΄ν•˜λŠ” β€‹β€‹λŸ°νƒ€μž„ μ˜΅μ…˜μ΄ μžˆμ§€λ§Œ(https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access μ°Έμ‘°), λŒ€μ‹  ν”„λ ˆμž„μ›Œν¬λ₯Ό μ‚¬μš©ν•˜λŠ” 경우 ν•Έλ“€λ°” μ½”μ–΄μ˜ 경우 μ„€μ • 방법이 λͺ…ν™•ν•˜μ§€ μ•Šμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

이 였λ₯˜ λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ˜λ©΄ λ‹€μŒ 정보와 ν•¨κ»˜ μ˜κ²¬μ„ μΆ”κ°€ν•˜μ‹­μ‹œμ˜€.

  • 였λ₯˜κ°€ 기둝될 λ•Œ μ–΄λ–€ ν”„λ ˆμž„μ›Œν¬(예: express-hbs , express-handlebars )κ°€ Handlebarsλ₯Ό ν˜ΈμΆœν•©λ‹ˆκΉŒ? (ν™•μ‹€ν•˜μ§€ μ•Šμ€ 경우 npm ls handlebars λ˜λŠ” yarn why handlebars 의 좜λ ₯을 λΆ™μ—¬λ„£μŠ΅λ‹ˆλ‹€).
  • Handlebars.compile 와 κ²°κ³Ό ν…œν”Œλ¦Ώ κΈ°λŠ₯을 직접 μ‚¬μš©ν•˜λŠ” 경우 https://handlebarsjs.com/api-reference/runtime-options.html#options -to-control-prototype-access에 닡이 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. λ¬Έμ„œκ°€ κ·€ν•˜μ˜ μ§ˆλ¬Έμ— λŒ€λ‹΅ν•˜μ§€ μ•Šκ±°λ‚˜ μ΄ν•΄ν•˜μ§€ λͺ»ν•˜λŠ” 경우 λ¬Έμ„œλ₯Ό κ°œμ„ ν•  수 μžˆλ„λ‘ (μ—¬κΈ°)에 λ¬Έμ˜ν•˜μ‹­μ‹œμ˜€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

ν”„λ ˆμž„μ›Œν¬: express-handlebars mongoose

Mongoose의 λͺ¨λΈμ€ 클래슀이며 속성은 μƒμœ„ 개체의 "자체 속성"이 μ•„λ‹™λ‹ˆλ‹€.

κ°€μž₯ κΉ”λ”ν•œ 방법은 ν•Έλ“€λ°” μž…λ ₯이 μ μ ˆν•œ 일반 μžλ°”μŠ€ν¬λ¦½νŠΈ 객체인지 ν™•μΈν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이것은 toJSON() λ˜λŠ” toObject λ₯Ό ν˜ΈμΆœν•˜μ—¬ Mongooseμ—μ„œ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

app.get('/test', function (_req, res) {
    Kitten.find({}).then(kittens => {
        res.render('test.hbs', {
            kittens: kittens.map(kitten => kitten.toJSON())
        })
    })
});

λ¬Έμ„œμ— μ–ΈκΈ‰λœ runtimeOptions (λ˜λŠ” templateOptions )λŠ” express-handlebars 섀정을 μ§€μ›ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ—¬κΈ°μ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

λΉ λ₯΄κ³  λ”λŸ¬μš΄ ν•΄ν‚Ή(정말 해킹이고 μ œμ•ˆν•˜μ§€ μ•ŠμŒ)은 λ‹€μŒμ„ μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

var handlebarsInstance = Handlebars.create();

handlebarsInstance.compile = function (templateStr, compileOptions) {
   // compile template
   // return a function that calls the compiled template with  `allowProtoPropertiesByDefault` and `allowProtoMethodsByDefault` as runtime-options
  // WARNING: People can crash your system by frabricating special templates. Don't use
  // this if your users can create templates. 
}
app.engine('hbs', expressHandlebars({
    handlebars: handlebarsInstance
}));

Handlebars.createWithAllowedProtoAccess λ˜λŠ” 이와 μœ μ‚¬ν•œ 것을 μ›ν•˜μ‹œλ©΄ 이 λŒ“κΈ€μ— λ‘œμΌ“ 이λͺ¨ν‹°μ½˜μ„ μΆ”κ°€ν•˜μ„Έμš”.

λͺ¨λ“  13 λŒ“κΈ€

ν”„λ ˆμž„μ›Œν¬: express-handlebars mongoose

Mongoose의 λͺ¨λΈμ€ 클래슀이며 속성은 μƒμœ„ 개체의 "자체 속성"이 μ•„λ‹™λ‹ˆλ‹€.

κ°€μž₯ κΉ”λ”ν•œ 방법은 ν•Έλ“€λ°” μž…λ ₯이 μ μ ˆν•œ 일반 μžλ°”μŠ€ν¬λ¦½νŠΈ 객체인지 ν™•μΈν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이것은 toJSON() λ˜λŠ” toObject λ₯Ό ν˜ΈμΆœν•˜μ—¬ Mongooseμ—μ„œ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

app.get('/test', function (_req, res) {
    Kitten.find({}).then(kittens => {
        res.render('test.hbs', {
            kittens: kittens.map(kitten => kitten.toJSON())
        })
    })
});

λ¬Έμ„œμ— μ–ΈκΈ‰λœ runtimeOptions (λ˜λŠ” templateOptions )λŠ” express-handlebars 섀정을 μ§€μ›ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ—¬κΈ°μ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

λΉ λ₯΄κ³  λ”λŸ¬μš΄ ν•΄ν‚Ή(정말 해킹이고 μ œμ•ˆν•˜μ§€ μ•ŠμŒ)은 λ‹€μŒμ„ μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

var handlebarsInstance = Handlebars.create();

handlebarsInstance.compile = function (templateStr, compileOptions) {
   // compile template
   // return a function that calls the compiled template with  `allowProtoPropertiesByDefault` and `allowProtoMethodsByDefault` as runtime-options
  // WARNING: People can crash your system by frabricating special templates. Don't use
  // this if your users can create templates. 
}
app.engine('hbs', expressHandlebars({
    handlebars: handlebarsInstance
}));

Handlebars.createWithAllowedProtoAccess λ˜λŠ” 이와 μœ μ‚¬ν•œ 것을 μ›ν•˜μ‹œλ©΄ 이 λŒ“κΈ€μ— λ‘œμΌ“ 이λͺ¨ν‹°μ½˜μ„ μΆ”κ°€ν•˜μ„Έμš”.

이 λ¬Έμ œκ°€ μžˆλŠ”λ° ν•΄κ²° 방법을 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€. λ„μ™€μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ?

ν•Έλ“€λ°”λ₯Ό μ‚¬μš©ν•  λ•Œ 이 였λ₯˜λ₯Ό μˆ˜μ •ν•˜λŠ” 방법을 μ•Œλ €μ€„ 수 μžˆλŠ” μ‚¬λžŒμ΄ μžˆμŠ΅λ‹ˆκΉŒ?
μ‹€μˆ˜:
En-US: ν•Έλ“€λ°”: λΆ€λͺ¨μ˜ "μžμ‹ μ˜ 속성"이 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— 속성 "이름"을 ν™•μΈν•˜κΈ° μœ„ν•œ μ•‘μ„ΈμŠ€κ°€ κ±°λΆ€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
확인 λ˜λŠ” 이 κ²½κ³ λ₯Ό λΉ„ν™œμ„±ν™”ν•˜λŠ” λŸ°νƒ€μž„ μ˜΅μ…˜μ„ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν•Έλ“€λ°”λ₯Ό μ‚¬μš©ν•  λ•Œ 이 였λ₯˜λ₯Ό λ³΄κ³ ν•˜λŠ” 방법을 μ•„λŠ” μ‚¬λžŒμ΄ μžˆμŠ΅λ‹ˆκΉŒ?
μ‹€μˆ˜:
En: ν•Έλ“€λ°”: "이름" 속성은 μ•„λ²„μ§€μ˜ "μžμ‹ μ˜ 속성"이 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— μ•‘μ„ΈμŠ€κ°€ κ±°λΆ€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

확인 λ˜λŠ” 이 κ²½κ³ λ₯Ό λΉ„ν™œμ„±ν™”ν•˜λŠ” λŸ°νƒ€μž„ μ˜΅μ…˜μ„ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ‚΄ 것은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
const ν•Έλ“€λ°” = require('μ΅μŠ€ν”„λ ˆμŠ€ ν•Έλ“€λ°”')

app.engine('ν•Έλ“€λ°”', ν•Έλ“€λ°”({defaultLayout: '메인'}))
app.set('보기 엔진', 'ν•Έλ“€λ°”')

app.get('/결제', function(req, res){
Payment.findAll().then(ν•¨μˆ˜(결제){
res.render('결제', {결제: 결제})
})

κ°•ν™”λ₯Ό μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. ~에
κ°•ν™”λ₯Ό μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. 브

이것을 λ³΄μ‹­μ‹œμ˜€: https://www.npmjs.com/package/@handlebars/allow -prototype-access

λ¬Έμ„œμ— μ–ΈκΈ‰λœ runtimeOptions (λ˜λŠ” templateOptions )λŠ” express-handlebars 섀정을 μ§€μ›ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ—¬κΈ°μ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

express-handlebarsμ—μ„œλŠ” μ΄λŸ¬ν•œ λŸ°νƒ€μž„ ꡬ성 μ˜΅μ…˜μ„ μ„€μ •ν•  수 μ—†κΈ° λ•Œλ¬Έμ— express-handlebarsκ°€ ν•Έλ“€λ°” 4.5.3을 μ‚¬μš©ν•˜λ„λ‘ κ°•μ œν•˜λŠ” 방법을 μ•Œκ³  μžˆλŠ” μ‚¬λžŒμ΄ μžˆμŠ΅λ‹ˆκΉŒ?

https://www.npmjs.com/package/@handlebars/allow -prototype-accessλ₯Ό express-handlebars 와 ν•¨κ»˜ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. κΈ°λ³Έ λ§€κ°œλ³€μˆ˜κ°€ μ„€μ •λ˜λ„λ‘ 컴파일된 ν…œν”Œλ¦Ώμ„ λž˜ν•‘ν•©λ‹ˆλ‹€.

Hi Nils, ν•΄λ‹Ή νŒ¨ν‚€μ§€λ₯Ό μž‘λ™μ‹œν‚¬ 수 μ—†μŠ΅λ‹ˆλ‹€. ν•΄λ‹Ή νŒ¨ν‚€μ§€λ‘œ μ‹€ν–‰ν•˜λ €κ³  ν•˜λ©΄ "였λ₯˜: "ν•Έλ“€λ°”" λͺ¨λ“ˆμ΄ 보기 엔진을 μ œκ³΅ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."λΌλŠ” λ©”μ‹œμ§€κ°€ λ‚˜νƒ€λ‚©λ‹ˆλ‹€. 였λ₯˜. λ‹€λ₯Έ μ‚¬μš©μžκ°€ https://github.com/wycats/handlebars.js/issues/1648 μ—μ„œ λΉ„μŠ·ν•œ 문제λ₯Ό κ²ͺκ³  μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. 같은 μŠ€λ ˆλ“œμ— κ²°κ³Όλ₯Ό κ²Œμ‹œν–ˆμŠ΅λ‹ˆλ‹€. 감사 ν•΄μš”

@craj1031tx #1648μ—μ„œ λ‹΅λ³€λ“œλ ΈμŠ΅λ‹ˆλ‹€.

ν”„λ ˆμž„μ›Œν¬: express-handlebars mongoose

Mongoose의 λͺ¨λΈμ€ 클래슀이며 속성은 μƒμœ„ 개체의 "자체 속성"이 μ•„λ‹™λ‹ˆλ‹€.

κ°€μž₯ κΉ”λ”ν•œ 방법은 ν•Έλ“€λ°” μž…λ ₯이 μ μ ˆν•œ 일반 μžλ°”μŠ€ν¬λ¦½νŠΈ 객체인지 ν™•μΈν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이것은 toJSON() λ˜λŠ” toObject λ₯Ό ν˜ΈμΆœν•˜μ—¬ Mongooseμ—μ„œ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

app.get('/test', function (_req, res) {
    Kitten.find({}).then(kittens => {
        res.render('test.hbs', {
            kittens: kittens.map(kitten => kitten.toJSON())
        })
    })
});

λ¬Έμ„œμ— μ–ΈκΈ‰λœ runtimeOptions (λ˜λŠ” templateOptions )λŠ” express-handlebars 섀정을 μ§€μ›ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ—¬κΈ°μ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

λΉ λ₯΄κ³  λ”λŸ¬μš΄ ν•΄ν‚Ή(정말 해킹이고 μ œμ•ˆν•˜μ§€ μ•ŠμŒ)은 λ‹€μŒμ„ μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

var handlebarsInstance = Handlebars.create();

handlebarsInstance.compile = function (templateStr, compileOptions) {
   // compile template
   // return a function that calls the compiled template with  `allowProtoPropertiesByDefault` and `allowProtoMethodsByDefault` as runtime-options
  // WARNING: People can crash your system by frabricating special templates. Don't use
  // this if your users can create templates. 
}
app.engine('hbs', expressHandlebars({
    handlebars: handlebarsInstance
}));

Handlebars.createWithAllowedProtoAccess λ˜λŠ” 이와 μœ μ‚¬ν•œ 것을 μ›ν•˜μ‹œλ©΄ 이 λŒ“κΈ€μ— λ‘œμΌ“ 이λͺ¨ν‹°μ½˜μ„ μΆ”κ°€ν•˜μ„Έμš”.

κ³ λ§ˆμ›Œ 친ꡬ :)

λ‚˜λŠ” λ‘œμΌ“μ„ κ±°κΈ° 두고 κ°„λ‹€!

μ €λŠ” Sequelize와 ν•¨κ»˜ Express-Handlebarsλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
npm ls ν•Έλ“€λ°”:

+-- [email protected]
| `-- [email protected] 
`-- [email protected] 

μ½˜μ†” 좜λ ₯:

Handlebars: Access has been denied to resolve the property "name" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "id" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "email" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "url" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "description" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "createdAt" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "updatedAt" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details

λ‚˜λŠ” λͺ½κ΅¬μŠ€μ— λŒ€ν•œ λ‹Ήμ‹ μ˜ μ†”λ£¨μ…˜μœΌλ‘œ 당신을 μ‹œν—˜ν•΄ λ³Ό κ²ƒμž…λ‹ˆλ‹€

ν”„λ ˆμž„μ›Œν¬: express-handlebars mongoose

Mongoose의 λͺ¨λΈμ€ 클래슀이며 속성은 μƒμœ„ 개체의 "자체 속성"이 μ•„λ‹™λ‹ˆλ‹€.

κ°€μž₯ κΉ”λ”ν•œ 방법은 ν•Έλ“€λ°” μž…λ ₯이 μ μ ˆν•œ 일반 μžλ°”μŠ€ν¬λ¦½νŠΈ 객체인지 ν™•μΈν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이것은 toJSON() λ˜λŠ” toObject λ₯Ό ν˜ΈμΆœν•˜μ—¬ Mongooseμ—μ„œ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

app.get('/test', function (_req, res) {
    Kitten.find({}).then(kittens => {
        res.render('test.hbs', {
            kittens: kittens.map(kitten => kitten.toJSON())
        })
    })
});

λ¬Έμ„œμ— μ–ΈκΈ‰λœ runtimeOptions (λ˜λŠ” templateOptions )λŠ” express-handlebars 섀정을 μ§€μ›ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ—¬κΈ°μ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

λΉ λ₯΄κ³  λ”λŸ¬μš΄ ν•΄ν‚Ή(정말 해킹이고 μ œμ•ˆν•˜μ§€ μ•ŠμŒ)은 λ‹€μŒμ„ μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

var handlebarsInstance = Handlebars.create();

handlebarsInstance.compile = function (templateStr, compileOptions) {
   // compile template
   // return a function that calls the compiled template with  `allowProtoPropertiesByDefault` and `allowProtoMethodsByDefault` as runtime-options
  // WARNING: People can crash your system by frabricating special templates. Don't use
  // this if your users can create templates. 
}
app.engine('hbs', expressHandlebars({
    handlebars: handlebarsInstance
}));

Handlebars.createWithAllowedProtoAccess λ˜λŠ” 이와 μœ μ‚¬ν•œ 것을 μ›ν•˜μ‹œλ©΄ 이 λŒ“κΈ€μ— λ‘œμΌ“ 이λͺ¨ν‹°μ½˜μ„ μΆ”κ°€ν•˜μ„Έμš”.

μ•„λž˜μ™€ 같이 lean() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

 app.get('/test', function (_req, res) {
    Kitten.find().lean().then(kittens => res.render('test.hbs', { kittens: kittens}));
});

express-handlebarsμ—μ„œλŠ” μ΄λŸ¬ν•œ λŸ°νƒ€μž„ ꡬ성 μ˜΅μ…˜μ„ μ„€μ •ν•  수 μ—†κΈ° λ•Œλ¬Έμ— express-handlebarsκ°€ ν•Έλ“€λ°” 4.5.3을 μ‚¬μš©ν•˜λ„λ‘ κ°•μ œν•˜λŠ” 방법을 μ•Œκ³  μžˆλŠ” μ‚¬λžŒμ΄ μžˆμŠ΅λ‹ˆκΉŒ?

μ΅μŠ€ν”„λ ˆμŠ€ 핸듀바에 λŸ°νƒ€μž„ ꡬ성 μ˜΅μ…˜μ„ μΆ”κ°€ν•  수 μžˆλ„λ‘ PR을 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. https://github.com/express-handlebars/express-handlebars/pull/53

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰