简体   繁体   中英

Where to place Rythm template files

I am having a weird problem with Rythm templates. Currently, I have these templates placed under

myPrj/src/main/java/resources/templates folder.

And all the Java source code is under myPrj/src/main/java folder.

When I try to render, sometimes Rythm is generating the XML file and sometimes I get the file name as is.

I have the home.template set to "templates" folder:

params.put("home.template", "templates");

String myTemplateString = Rythm.render("MyTemplate.xml", parameters);

Looks like Rythm is not able to locate MyTemplate.xml and resulting in emitting MyTemplate.xml as the output.

Can you please help me on how to solve this problem?? In addition, would appreciate if you can guide me on what should be the appropriate location to place these templates.

home.template is the configuration key to initialize template engine, not the parameter to render your template.

My implementation of your app looks like

public class App {
    private static RythmEngine engine;

    private static void echo(String msg, Object ... args) {
        System.out.println(String.format(msg, args));
    }

    private static void init() {
        echo("initializing rythmengine");
        Map<String, Object> conf = new HashMap<String, Object>();
        conf.put("home.template", "templates");
        engine = new RythmEngine(conf);
        echo("engine initialized");
    }

    private static void render() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("foo", "FOO");
        params.put("bar", "BAR");
        String result = engine.render("MyTemplate.xml", params);
        echo(result);
    }

    private static void doJob() {
        echo("start doing real job now...");
        render();
    }

    public static void main( String[] args ) {
        init();
        doJob();
    }
}

The complete sample code could be found at https://github.com/greenlaw110/Rythm/tree/master/samples/demo_fo_SO_150529 . Download the sample code and run mvn compile exec:java to see the result

It seems your problem lies within the path for the home.template . The example on their website might help.

If I'm not mistaken, you should use params.put("home.template", "resources/templates"); rather than params.put("home.template", "templates"); .

Generally speaking, this kind of behaviour takes place any time Rythm can't find the template. I found it is best to check both, the path and file name. If necessary, simply use an absolute path to your template to make sure it points to the right directory. After you got the right path, you might want to change it back to be relative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM