LoginSignup
29

More than 5 years have passed since last update.

Bashでテンプレートエンジン{{Mustache}}を使ってみる

Last updated at Posted at 2015-06-20

Mustacheとは

Mustacheとはロジックレスのテンプレートエンジンで、その最大のウリはMustacheの記法を覚えると様々な言語で使えるから便利だよ―!ということみたいです。

特定の言語に依存しない記法になっているのは素晴らしいコンセプトで心惹かれます。

様々な言語…

これらの言語で使えるようです(2015/06/20時点)
{{ mustache }}

Ruby, JavaScript, Python, Erlang, node.js, PHP, Perl, Perl6, Objective-C, Java, C#/.NET, Android, C++, Go, Lua, ooc, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, Haskell, XQuery, ASP, Io, Dart, Haxe, Delphi, Racket, Rust, OCaml, Swift, and for Bash

mustacheトップ.png

・・・Bash!
試すしかない!

Mo -Mustache Templates in Bash

モジュールはこちらMo - Mustache Templates in Bash - Githubで公開されています。
「mustache bash github」ググるとmushというプロジェクトの方が上位にヒットしますが、本家のリンクのほうが正式だと信じて、Moを利用します。

環境構築

moをDLして権限与えます。

ダウンロード
wget https://raw.githubusercontent.com/tests-always-included/mo/master/mo
chmod 744 mo

実行

実行方法

デモ用のファイルを作成します

demo.mo
Hello, {{NAME}}

I hope your {{TIME_PERIOD}} was fun.

{{NAME}}にTylerを、{{TIME_PERIOD}}にweekendを与えて実行します。
ワンラインで実行します。

ワンライン実行結果
% NAME=Tyler TIME_PERIOD=weekend ./mo demo.mo
Hello, Tyler

I hope your weekend was fun.

また、埋め込む値はグローバル変数に与えることによっても動くようなので、exportで設定します。

グローバル変数渡し
 % export NAME=Yamada TIME_PERIOD=Friday                                                      
 % ./mo demo.mo                                                                            
Hello, Yamada

I hope your Friday was fun.

テンプレートはパイプ渡し出来るようです。
埋め込み文字列はグローバル変数にexportしておきます。

テンプレートのパイプ渡し
% export NAME=Yamada
% echo "Hello {{NAME}}! Thank you for coming today." | ./mo
Hello Yamada! Thank you for coming today.

配列

配列用のテンプレートを作成します。
Mustacheは{{#hoge}}{{/hoge}}で埋め込む変数が配列の場合は繰り返し処理する仕様なようです。
埋め込む値がなければ出力しないので、分岐処理も兼ねています。
{{.}}で配列の現在の値を取得できます。

array.mo
Here are the items in the array:
{{#ARRAY}}
    * {{.}}
{{/ARRAY}}

配列はexport出来ないので、shellファイルを作成します。
./moの前の.はsourceの役割をするらしく無ければ動きませんでした。

arrayexe.sh
#!/bin/bash
cd "$(dirname "$0")"
export ARRAY=(one two "three three three" four five)
. ./mo array.mo

実行します。
繰り返しされて出力されてますね。

配列実行結果
 % ./arrayexe.sh                                                                                  
Here are the items in the array:
    * one
    * two
    * three three three
    * four
    * five

公式のリファレンスには記載がないように見えますが、配列の添字を取得する方法は{{@index}}らいしいですが、上手く動きませんでした。Moではまだ未実装なんですかね。

In Mustache, How to get the index of the current Section - stackoverflow

{{@index}}利用版のarray.mo
Here are the items in the array:
{{#ARRAY}}
    {{@index}}. {{.}}
{{/ARRAY}}

実行しても上手く動きません。本当は1. oneという形で出力したかったのですが、
ループ中の{{}}は全て配列の要素に置換される動きに現時点ではなっているようです。残念!

実行結果
 % ./arrayexe.sh                                                                                   
Here are the items in the array:
    one. one
    two. two
    three three three. three three three
    four. four
    five. five

おしまい

  • 単純な変数の埋め込みであればわざわざ使う必要は無さそうですが、簡単な繰り返しが出てくるときは使えそうですね!
  • {{Mustache}}は他の言語処理系でも存在するので他に転用が聞くのと、学習コストが抑えられそうなのが良いです。

参考情報

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
29