【问题标题】:Check value of the mustache variable in helper检查助手中 mustache 变量的值
【发布时间】:2013-03-12 11:09:42
【问题描述】:

我有带有价格和货币名称的 JSON,如下所示:

[{"price": 123, "currency": "RUR"},
{"price": 456, "currency": "USD"},
{"price": 789, "currency": "EUR"}]

另外,我使用 Mustache.php 来渲染它们。 {{价格}} {{货币}} 作为模板并得到: 第123章 456 美元 789 欧元

但是,我想将“RUR”、“USD”、“EUR”替换为“Russian rubles”、“US Dollar”、“Euro”并得到

123 Russian rubles
456 US dollars
789 Euro

我认为,我可以使用助手

$mustache->addHelper('_curstyle', function($text) {
if ($text == "RUR") {return ("Russian rubles")};
if ($text == "USD") {return ("US dollars")};
if ($text == "EUR") {return ("Euro")};
});

$text 等于“{{currency}}”。而且我不能使用 if 构造。如何将 {{currency}} 转换为值,或预渲染以在方程式中使用?

【问题讨论】:

    标签: mustache mustache.php


    【解决方案1】:

    假设您使用的是 Mustache.php v2.1,您可以使用作为可选第二个参数传递给您的助手的 LambdaHelper 来渲染原始块体:

    $mustache->addHelper('_curstyle', function($text, $mustache) {
        switch($mustache->render($text)) {
            case 'RUR':
                return 'Russian rubles';
            case 'USD':
                return 'US dollars';
            case 'EUR':
                return 'Euro';
            default:
                return $text;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 2012-05-11
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多