ninja icon indicating copy to clipboard operation
ninja copied to clipboard

can not configure the freemarker template path to local disk path.

Open koujun2012 opened this issue 9 years ago • 3 comments

I want to read freemarker templates on my local disk , because some times I don't want to rebuild and restart my site.

@Singleton public class FreemarkerConfiguration extends Configuration { public FreemarkerConfiguration() { try { this.setDirectoryForTemplateLoading(new File("C:\Developement\views")); } catch (Exception e) { e.printStackTrace(); }

}

} and bind(Configuration.class).to(FreemarkerConfiguration.class);

it does not work.

koujun2012 avatar Jun 03 '15 09:06 koujun2012

That's currently a not supported use case. But why do you want to do that? Ninja automatically picks up changes inside the view folders without restarting anything...

raphaelbauer avatar Jun 09 '15 09:06 raphaelbauer

Hi.

You can achieve this by modifying the Freemarker configuration in your Ninja application class. This way:

public class Ninja extends NinjaDefault {

    @Inject
    Logger logger;

    @Inject
    TemplateEngineFreemarker templateEngineFreemarker;

    @Override
    public void onFrameworkStart() {
        super.onFrameworkStart();

        try {
            templateEngineFreemarker.getConfiguration().setDirectoryForTemplateLoading(new File("/Volumes/USBKEY/work"));
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
}

I just tested it and it works... But don't forget, in the directory, to respect the file structure of ninja ("/views/ControllerName/methodName.ftl.html") or to name explicitly your templates when calling render() methods.

jlannoy avatar Jan 21 '16 21:01 jlannoy

Hi,

If I want to using setServletContextForTemplateLoading(ServletContext, String) method to load the template location. How could I get the ServletContext in Ninja?

leeaee avatar Mar 16 '16 09:03 leeaee