亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 jmustache 庫獲取模板參數?

如何使用 jmustache 庫獲取模板參數?

莫回無 2023-03-17 14:11:03
我正在嘗試獲取所有模板參數 - {{}} 中的參數名稱。例如,對于此模板:{{pet}}追著{{toy}}我想要“寵物”和“玩具”我只能使用samskivert/jmustache 庫,所以我不能使用不同的 mustache 庫。有沒有辦法用 jmustache 做到這一點,這樣我就不必用正則表達式解析字符串了?
查看完整描述

2 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

Mustache.Visitor用于訪問模板中的標簽而不執行它


例子:


List<String> vars = new ArrayList<>();

Template tmpl = ... // compile your template

tmpl.visit(new Mustache.Visitor() {


  // I assume you don't care about the raw text or include directives

  public void visitText(String text) {}


  // You do care about variables, per your example

  public void visitVariable(String name) {vars.add("Variable: " + name); }


  // Also makes sense to visit nested templates.

  public boolean visitInclude(String name) { vars.add("Include: " + name); return true; }


  // I assume you also care about sections and inverted sections

  public boolean visitSection(String name) { vars.add("Section: " + name); return true; }


  public boolean visitInvertedSection(String name) { vars.add("Inverted Section: " + name); return true; }

});

Visitor從 1.15 版開始可用jmustache:


<groupId>com.samskivert</groupId>

<artifactId>jmustache</artifactId>

<version>1.15</version>


查看完整回答
反對 回復 2023-03-17
?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

查看完整回答
反對 回復 2023-03-17
  • 2 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號