fizzed / rocker

Java 8 optimized, memory efficient, speedy template engine producing statically typed, plain java objects
742 stars 89 forks source link

Propose to remove the template extension restriction for ".rocker.html" #128

Open iceant opened 4 years ago

iceant commented 4 years ago

I am using rocker as a generic engine for rendering .html, .js files, and generate dynamic sql. for example, I have a file with path "static/js/app/app.main.js", I want to keep the extension as it is, so we can benefit the IDE features to coding easily. I have modified the implementation of rocker to generate a source file with path "static/js/app/app_dot_main_dot_js.java" in "generated-sources" directory.

Is it possible to modify the code base to support this feature?

attach the code I modified. diff.txt

jjlauer commented 4 years ago

@iceant I'm open to ideas on allowing additional file extensions or conventions. For example, allowing '.js' in the build process to be processed by Rocker. However, the replacing of dots and spaces with "dot" and "space" feels a bit hacky. Could you just rename "app.main.js" to "app_main.js"? Not sure I understand why not being able to rename the file is a huge deal.

iceant commented 4 years ago

@jjlauer thanks, the reason we want to name the java file for 'app.js' like 'app_dot_js.java' is because we defined a web fragment as a combination of .html + .css + .js, for example, we have a web fragment called 'timeline', it has this file structure: common/timeline.html, common/timeline.css, common/timeline.js. base on rocker's strategy, the .html, .css and .js will be compiled as the same unit 'timeline.java' that could be an issue. To add 'dot' is the design strategy to avoid this issue.

RockerBootstrap is good design, we can provide a new implementation of it to use different 'path to class' resolve strategy. But the compiler is the issue to stop us to use different strategy. It's better to remove the restriction for temlate file with '.html' or '.raw' extension. Even better to have a new interface called 'TemplateResolver' wich has 'public String templateNameToClassName(String templateName)', 'public ContentType templateNameToContentType(String templateName)', 'public String templatePathToClassName(String templatePath)' methods. These interfaces that can be used in DefaultRockerBootstrap, TemplateParser and TemplateModel. The user can provide different implementation for it.

xenoterracide commented 3 years ago

hmm... looking at possibly using rocker for project scaffolding, including .java files, though I find intellij at least is not so good once you put other syntax in there.

yeah

java.lang.IllegalArgumentException: Invalid template extension '.scaf/templates/TestApplication.java.raw'. Expecting something like 'views/app/index.rocker.html')
xenoterracide commented 3 years ago

oh, man, this extension parsing is just a terrible experience

java.lang.IllegalArgumentException: Invalid template extension '.scaf/templates/TestApplication_java.rocker.html'. Expecting something like 'views/app/index.rocker.html')

I'm going to move on to a different templating system, performance doesn't concern me, I was just hoping for the static typing, without a massive "we only do html" experience.

ccidral commented 1 month ago

It would be awesome if Rocker could handle other file types in addition to *.rocker.html. I'm using Rocker for server-side rendering and I need a template engine for processing SQL template file, but I want at all costs to avoid using two different template engines for different file types. I realize it supports *.rocker.raw but then I'd lose the benefits of SQL editor support (e.g. syntax highlighting). I tried changing suffixRegex to ^.*\.rocker\.(html|sql)$ but it gives this error:

Caused by: java.lang.IllegalArgumentException: Unsupported content type for extension [sql] for template name [query.rocker.sql]
    at com.fizzed.rocker.compiler.RockerUtil.templateNameToContentType (RockerUtil.java:152)
    at com.fizzed.rocker.compiler.TemplateParser.parseIdentity (TemplateParser.java:90)
    at com.fizzed.rocker.compiler.TemplateParser.parse (TemplateParser.java:100)
    at com.fizzed.rocker.compiler.JavaGeneratorRunnable.run (JavaGeneratorRunnable.java:85)
    at com.fizzed.rocker.maven.GenerateMojo.execute (GenerateMojo.java:168)

So I guess that's not really an option right now.