Hi, I'm hoping to create a new template implementa...
# http4k
a
Hi, I'm hoping to create a new template implementation (my first) and have a few questions -- I see the Dust engine is simpler but also very different than the Handlebars example and other implementations... does that mean you're ok with a different signature for the class than using the
configure
function param? Secondly, would it be ok to introduce a separate configuration class/object so certain options don't need to be passed on every call? This is challenging -- thanks for the guidance.
d
Tbh I'd avoid the dust engine - it's based on nashorn which is about to be removed from the jdk (so we will deprecate and remove the module from new releases)
a
Got it, thanks, one more question (for now) - is it required to have a 1-to-1 relationship between ViewModel and template? I am anticipating some templates that require no parameters/models, others where the params can be set on the fly (i.e. without a formal class/model), and also others where the template path might differ from the natural path of the model (i.e.
League.Team.Player
might not be associated with
<root_dir>/league/team/player.ext
)
d
you should be able to do both - if you check the ViewModel interface you can see that you can control the template name. For views without params, I generally just use an
object
a
ok... starting to wrap my head around it a bit more. thank you
d
with reference to the point of the configuration object - I think that you might be able to do nested references with handlebars?
Copy code
{{config.url}}
If not - you could also do something like use delegation:
Copy code
interface Config {
    val url : Uri
}

data class ActualConfig(override val url: Uri) : Config

class MyViewModel(config: Config) : Config by config

val a = MyViewModel(ActualConfig(Uri.of("whatever")))
a
David, is there a specific templating engine that synergizes best with http4k? Since many of the options are quite dated, or are ports from javascript, my gut feeling would be Pebble.
d
Personally I default to Handlebars. I know the syntax and it's never let me down - I haven't really looked at Pebble in detail TBH - it may or may not be better! The thing is that unless you need something particularly special, the abstraction http4k puts over the top kind of dispenses with the differences. As long as it's got expressions, loops and partials then I find that enough.
a
I have built a semi-working version of a jte template renderer. I say "semi-working" because I have only tried HotReload mode. jte compiles all templates into Java classes, anything not hot-reloading would be precompiled which is supported, but requires a gradle plugin which I don't know how to add to the sub-project's
build.gradle.kts
. Anyway, before I get too deep, it would be helpful if you'd take a look and let me know if I'm on the right track and you're ok with the code style, etc. No rush, I know you guys are very busy. It's obviously not ready for a pull request but it is available to view on Github. Thanks.
d
Happy to take a look but would like to play with the code to see how JTE works. It would definitely be worth rebasing all the changes from the main http4k repo as well (I can see your fork is pretty out of date 🙂 )
a
I thought I did that... I created a new branch and sync'ed that with today's http4k.
d
I'd really like to find a way to make this fit the existing model - it looks on first glance that we can make it fit - for instance you can override the template location in the ViewModel. I might need to play with it a bit to be sure.
a
Yes, that is what I am working towards, the same API as all the other template options. The private implementation of
TemplateRenderer
requires a
ViewModel
as the parameter for its
invoke
method, just like Handlebars and the rest, and it works with a plain
ViewModel
(tests pass). What I added is a new class
JteContent
that inherits from
ViewModel
but also adds some convenience functions. The existing implementation works with
HotReload()
renderer and the goal is to get the other two (
Caching()
and
CachingClasspath()
), I just need to figure out how to build and test with precompiled jte templates. Like I said, I know you're busy, happy to talk more at a time that is good for you. Thanks!