져니의 개발 정원 가꾸기

[Mustache] 은근 까먹는 Mustache 문법 (생각나는대로 업데이트) 본문

개발노트/JavaScript | Mustache

[Mustache] 은근 까먹는 Mustache 문법 (생각나는대로 업데이트)

전전쪄니 2022. 12. 31. 22:33

1. mustache 파일에서 렌더링 된 data 값을 Js 함수의 인자로 넘기기

.mustache 확장자의 파일 내부에서 js 함수를 호출해야 하는 일이 있었다.

js 함수 인자로 렌더링된 데이터 값을 주기 위해서는 인자로 넘기는 녀석을 single quotation으로 감싸주면 된다.

 

예) 

// calculator.js
let data = {
    'tokyo' : '31',
    'sydney' : '4',
    'seoul' : '30'
}; 

// 렌더링 부분
let rendered = Mustache.render(template, data);
documnet.getElementById('nation-thermos-template').html(rendered);

function getFahrenheit(value) {
    const converted = ((value × 9/5) + 32) + "°F";
    const element = document.getElementById('convert');
    element.innerHTML += '<div>converted<div>';
}
...
<div>
    <h1 id="convert" onclick="getFahrenheit('{{seoul}}')"> seoul 의 현재 기온 화씨로 보기 </h1>
</div>
...

 

<참고>

1. js 함수 인자로 mustache data 값 넘기기

    https://stackoverflow.com/questions/38694678/mustache-js-syntax-to-call-a-function-with-parameter