mustache-d icon indicating copy to clipboard operation
mustache-d copied to clipboard

Lazy lookup for section names

Open KingDuckZ opened this issue 11 years ago • 1 comments
trafficstars

Hello, I'm trying this library for the first time, and my question is: how can I rergister some sort of callback for section names? I wrote this code:

import mustache;
import std.stdio;

alias MustacheEngine!(string) Mustache;

string myFind (string parKey) {
    writefln("%s not found", parKey);
    return parKey;
}
int main() {
    Mustache mustache;
    auto context = new Mustache.Context;
    mustache.handler = delegate string(string a) { return myFind(a); };
    mustache.level = Mustache.CacheLevel.check;
    stdout.rawWrite(mustache.render("basic", context));
    return 0;
}

and I expected to see "in_ca" printed on stdout, but I don't. Thinking about it, it wouldn't make much sense either, but then what's the correct way to have mustache-d lookup in my code instead of searching the context for stuff? In my real code the context will be empty, just like in the example I posted.

Speaking of my custom handler myFind(), how can I signal an error if the key is not valid? Returning an empty string will not do it because the empty string could be a valid value. Should I just throw?

KingDuckZ avatar Sep 30 '14 17:09 KingDuckZ

how can I rergister some sort of callback for section names? [snip] I expected to see "in_ca" printed on stdout, but I don't.

No support because mustache section is disabled when key is not exist. So current behaviour is expected.

In my real code the context will be empty, just like in the example I posted.

Sorry, I can't imagine your case. Why do you use template engine with empty context?

how can I signal an error if the key is not valid?

Raising an error in your handler doesn't work?

repeatedly avatar Oct 04 '14 21:10 repeatedly