2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hogan.jsからHandlebars.jsに乗り換える

Posted at

動機

絶対に必要な作業ではありません。
ほぼ同じ機能を持つテンプレートエンジンです。
どちらか1つにまとめたい気持ちです。

Release状況

枯れているライブラリであり、大きな変更があるとは思えません。
ですが、万が一セキュリティに関わるようなバグがあった場合に対応が遅いと困ります。
近年のRelease状況を確認してみます。

Handlebars

2017年5月にリリースあり
https://github.com/wycats/handlebars.js/releases

Mustache

2016年にリリースあり
https://github.com/janl/mustache.js/releases

Hogan.js

2014年(3年前)で止まっている
https://github.com/twitter/hogan.js/releases

Twitter社製のライブラリとはいえ、メンテナンスが控えめになることがあるようです。

手順

次の手順で、Hogan.jsからHandlebars.jsに乗り換えます。

  1. npm i handlebars
  2. require('hogan.js')require('handlebars')に置き換え
  3. template.render(data)template(data)に置き換え
  4. npm uninstall hogan.js

環境

npmでインストールして(ブラウザで動かす場合は)browserify等で変換して使う想定です。
scriptタグで読み込んで使っている場合は、適当に頑張ってください。

APIの差

上記の手順を確認するため、両者のAPIの差を整理します。
Hogan.jsのexampleを参考にして比較してみましょう。

Handlebars.js

const data = {
  screenName: "dhg",
}

const template = Handlebars.compile("Follow @{{screenName}}.")
const output = template(data)

Hogan.js

const data = {
  screenName: "dhg",
}

const template = Hogan.compile("Follow @{{screenName}}.")
const output = template.render(data)

export先がdefaultかrednder関数かの違いぐらいしかありません。

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?