Question

I've JSON with the price and currency name, like this:

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

Also, i use Mustache.php to render them. {{price}} {{currency}} as template and get: 123 RUR 456 USD 789 EUR

But, I want to replace "RUR", "USD", "EUR" to "Russian rubles", "US dollars", "Euro" and get

123 Russian rubles
456 US dollars
789 Euro

I think, that i can use helper

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

but $text equal "{{currency}}". And I can't use if construct. How I can convert {{currency}} to value, or prerender it to use in equations?

Was it helpful?

Solution

Assuming you're using Mustache.php v2.1, you can use the LambdaHelper passed as an optional second argument to your helper for rendering the original block body:

$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;
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top