Code Monkey home page Code Monkey logo

Comments (11)

casid avatar casid commented on August 15, 2024 1

Hi, I've only briefly looked into Qute so far and that was half a year ago.

But I think the key differences are:

  • jte has no own expression language you have to learn, it's just Java.
  • Thus you get almost everything you have in IntelliJ for Java for jte templates as well, if you use the jte plugin.
  • jte analyzes the HTML you're building at compile time and does context-sensitive output escaping for you automatically.

You can easily call your own extensions by calling Java code :-) For instance, I usually have some kind of ViewContext that I call from templates for localizing stuff. This way you can reuse the same localization logic you use in other parts of the application.

Something like this:

@import static my.ViewContext.*

<p>
  ${localize("my.key")}
</p>
public final class ViewContext {
  public static gg.jte.Content localize(String key) {
     // access request context via thread local and localize...
  }
}

The cool thing about that is that there's no magic, just code. And IntelliJ can resolve all those references, if you want to know what's happening. Just ctrl+click on that localize method and you're instantly there. Other template engines have a lot more magic involved there and it's a lot harder to find out what's really going on.

Of course you can pass such a context as template parameters as well, that's up to your taste. Personally I find the static helper method quite productive ;-)

I don't know ERB, but I think this should be done quite easily. For such stuff the gg.jte.Content interface is pretty awesome :-)

Also, I'm following hotwire and htmx with great interest! I think they should work great together with jte. It's still on my todo list to make a demo like you linked, but haven't found the time so far.

Also, thanks for your interest in jte and asking all those questions!

from jte.

roscrl avatar roscrl commented on August 15, 2024 1

Hey awesome explanation had no idea it was that easy and was just a static method call away to format anything I want super neat. I guess for the example for a nicely formatted date time I can just create a static method and return jte.gg.Content ? Will need to see what that holds in the docs.

Would love to see the htmx or hotwire integration also! Following them super closely at the moment also, seems like a perfect stack with jte too!!

What do you mean by title 'automatically be escaped as HTML attribute, not just get a generic HTML output escaping.' what is the difference? Also for the example of value="${update ? todo.title : null}" if null gets hit does 'value=null' show up in the HTML result? Just seen docs exactly as I thought, awesome.

Thanks for fielding these questions will have to dive deeper into jte :)

from jte.

roscrl avatar roscrl commented on August 15, 2024 1

Also can I say how amazing the speed of https://mazebert.com/ is, I'm getting responses in less that 45ms on some pages from the UK, crazy when you come from a Ruby landscape.

Can you share any tricks you are using or do you have any caching in place? Super interested on how you are doing the https://mazebert.com/stats/ page, it has content that is frequently updated minutes ago.

from jte.

casid avatar casid commented on August 15, 2024 1

Thanks! Most pages on https://mazebert.com/ are statically rendered to files at startup and served by nginx. There's a scheduled thread pool executor that periodically checks / renders pages like /stats:

executorService = Executors.newScheduledThreadPool(1);

executorService.scheduleWithFixedDelay(this::refreshStats, 5L, 5L, TimeUnit.MINUTES);
executorService.scheduleWithFixedDelay(this::refreshSeasonPages, 1L, 1L, TimeUnit.SECONDS);

And there are a few pages that are not pre-rendered, because there are so many of them, like the player page:
https://mazebert.com/player/115/

from jte.

casid avatar casid commented on August 15, 2024 1

gg.jte.Content is an interface you can use if you want to programmatically do template output. If you'd use String for that, you'd need to do the output with $unsafe{myCachedContent()}, which is pretty ugly and error prone.

For instance, the built-in localization support preserves all HTML tags from localization keys but still escapes all user provided parameters. Maybe that's a good starting point.

https://github.com/casid/jte/blob/master/jte-runtime/src/main/java/gg/jte/support/LocalizationSupport.java

from jte.

casid avatar casid commented on August 15, 2024

I had a look at the qute example project you posted.

Here's a good example about what I meant that jte understands HTML:

<input type="text" name="title" class="form-control" id="title" placeholder="Title" required {#if update}value="{todo.title}"{/if}>

<input type="checkbox" name="completed" class="form-check-input" id="completed" {#if update}{#if todo.completed}checked{/if}{/if}>

Especially when you have more than just one attribute, this becomes very ugly. In jte you can simply write this for the same result:

<input type="text" name="title" class="form-control" id="title" placeholder="Title" required value="${update ? todo.title : null}">

<input type="checkbox" name="completed" class="form-check-input" id="completed" checked="${update && todo.completed}">

Since checked is a boolean attribute, jte even fails compilation if you don't provide a boolean expression for it :-)

Also, the output for todo.title will automatically be escaped as HTML attribute, not just get a generic HTML output escaping.

from jte.

renannprado avatar renannprado commented on August 15, 2024

@rosscarlisle did you manage to make jte work with Quarkus? Could you share the example?

from jte.

casid avatar casid commented on August 15, 2024

@renannprado I think Quarkus uses vert.x under the hood. Maybe this is a starting point for the integration?
https://github.com/vert-x3/vertx-web/tree/master/vertx-template-engines/vertx-web-templ-jte

from jte.

renannprado avatar renannprado commented on August 15, 2024

@casid quarkus AFAIK uses Undertow under the hood, but not vertx, so I'm not sure if it will help, but I'll have a look. Thanks!

Edit: TIL that quarkus is based on vertx. :)

from jte.

roscrl avatar roscrl commented on August 15, 2024

@renannprado Apologies, but not got around trying to integrate it yet.

from jte.

renannprado avatar renannprado commented on August 15, 2024

@casid @rosscarlisle I've created issue #61 issue to track the Quarkus integration as this was not really the purpose of this issue.

from jte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.