Skip to content

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Feb 20, 2025

Given the following jex controller

@Controller("/jstache")
public class RegularJstache {

  @Get("/hello")
  public HelloWorldZeroDependency hello() {
    Person rick = new Person("Rick", LocalDate.now().minusYears(70));
    Person morty = new Person("Morty", LocalDate.now().minusYears(14));
    Person beth = new Person("Beth", LocalDate.now().minusYears(35));
    Person jerry = new Person("Jerry", LocalDate.now().minusYears(35));
    return new HelloWorldZeroDependency("Hello alien", List.of(rick, morty, beth, jerry));
  }

  @Get("/helloRuntime")
  public HelloWorld helloRuntime() {
    Person rick = new Person("Rick", LocalDate.now().minusYears(70));
    Person morty = new Person("Morty", LocalDate.now().minusYears(14));
    Person beth = new Person("Beth", LocalDate.now().minusYears(35));
    Person jerry = new Person("Jerry", LocalDate.now().minusYears(35));
    return new HelloWorld("Hello alien", List.of(rick, morty, beth, jerry));
  }

  /*
   * Annotate the root model with an inline mustache template
   */
  @JStacheConfig(type = JStacheType.STACHE)
  @JStache(
      template =
          """
          {{#people}}
          {{message}} {{name}}! You are {{#ageInfo}}{{age}}{{/ageInfo}} years old!
          {{#-last}}
          That is all for now!
          {{/-last}}
          {{/people}}
          """)
  public record HelloWorldZeroDependency(String message, List<Person> people) implements AgeLambdaSupport {}

  public record Person(String name, LocalDate birthday) {}

  public record AgeInfo(long age, String date) {}

  public interface AgeLambdaSupport {
    @JStacheLambda
    default AgeInfo ageInfo(Person person) {
      long age = ChronoUnit.YEARS.between(person.birthday(), LocalDate.now());
      String date = person.birthday().format(DateTimeFormatter.ISO_DATE);
      return new AgeInfo(age, date);
    }
  }

  @JStache(
      template =
          """
          {{#people}}
          {{message}} {{name}}! You are {{#ageInfo}}{{age}}{{/ageInfo}} years old!
          {{#-last}}
          That is all for now!
          {{/-last}}
          {{/people}}
          """)
  public record HelloWorld(String message, List<Person> people) implements AgeLambdaSupport {}

}

this will be generated

@Generated("avaje-jex-generator")
@Component
public final class RegularJstache$Route implements Routing.HttpService {

  private final RegularJstache controller;

  public RegularJstache$Route(RegularJstache controller, Jsonb jsonb) {
    this.controller = controller;
  }

  @Override
  public void add(Routing routing) {
    routing.get("/jstache/hello", this::_hello);
    routing.get("/jstache/helloRuntime", this::_helloRuntime);
  }

  private void _hello(Context ctx) throws Exception {
    ctx.status(200);
    var result = controller.hello();
    if (result != null) {
      var content = HelloWorldZeroDependencyRenderer.of().execute(result);
      ctx.contentType("text/html").write(content);
    }
  }

  private void _helloRuntime(Context ctx) throws Exception {
    ctx.status(200);
    var result = controller.helloRuntime();
    if (result != null) {
      var content = JStachio.render(result);
      ctx.contentType("text/html").write(content);
    }
  }

}

Will resolve #566

@SentryMan SentryMan changed the title Add JStachio support for jex Add JStachio support Feb 20, 2025
@SentryMan SentryMan changed the title Add JStachio support Add Native JStachio support Feb 20, 2025
@SentryMan SentryMan self-assigned this Feb 20, 2025
@SentryMan SentryMan added the enhancement New feature or request label Feb 20, 2025
@SentryMan SentryMan added this to the 3.1 milestone Feb 20, 2025
@SentryMan SentryMan marked this pull request as ready for review February 20, 2025 04:59
@SentryMan SentryMan requested a review from rbygrave February 20, 2025 04:59
@SentryMan SentryMan enabled auto-merge (squash) February 20, 2025 04:59
SentryMan and others added 3 commits February 20, 2025 00:11
Noting that this uses TEXT_HTML_UTF8 = "text/html;charset=utf-8" for the content type
@SentryMan SentryMan merged commit 6840073 into avaje:master Feb 20, 2025
6 checks passed
@SentryMan SentryMan deleted the jex-jsonb branch February 20, 2025 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add native support for Jstachio

2 participants