ScalaHandlebarsを使用しています。
このサイトを見ながらヘルパーを自作したのですが、ヘルパーからはStringしか返せないと記述があり実際に作成しメソッドからBoolenaを返そうとしたらエラーになりました。
やりたいこととしては、
0. 引数を取得
0. 値が存在しなければtrueを返す
のような実装です。
http://d.hatena.ne.jp/Kazuhira/20140920/1411202608
実装したコード
scala
1import com.gilt.handlebars.scala.binding.dynamic._ 2import com.gilt.handlebars.scala.Handlebars 3import com.gilt.handlebars.scala.helper.{Helper, HelperOptions} 4import com.gilt.handlebars.scala.binding.{Binding, BindingFactory} 5 6class MyHelper[T] extends Helper[T] { 7 override def apply(binding: Binding[T], options: HelperOptions[T])(implicit contextFactory: [T]): Boolean = { 8 val arg = options.argument(0).getOrElse("MyHelper".asInstanceOf[T]) 9 sample.length match{ 10 case 0 => { true } 11 case _ => { false } 12 } 13 } 14} 15object Sample { 16 def main(args: Array[String]): Unit = { 17 val html = 18 """ 19 | <html> 20 | <head></head> 21 | <body> 22 | <p>{{sample}}</p> 23 | <p>{{#my sample}}{{/my}}</p> 24 | </body> 25 | </html> 26 """.stripMargin.mkString 27 val handle = Handlebars(html) 28 println(handle( 29 Map("sample"->"sampleText"), 30 helpers = Map("my"->new MyHelper) 31 )) 32 } 33}
実行結果
[error] /Code/service/PdfLayoutService.scala:19: overriding method apply in trait Helper of type (binding: com.gilt.handlebars.scala.binding.Binding[T], options: com.gilt.handlebars.scala.helper.HelperOptions[T])(implicit contextFactory: com.gilt.handlebars.scala.binding.BindingFactory[T])String; [error] method apply has incompatible type [error] override def apply(binding: Binding[T], options: HelperOptions[T])(implicit contextFactory: BindingFactory[T]): Boolean = { [error] ^ [error] one error found [error] (compile:compileIncremental) Compilation failed
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。