DEV Community

Jorge L, Morla
Jorge L, Morla

Posted on

3

Kiwi Compiler

Kiwi is a template engine preprocessor that integrates React components into Java template engines. It simplifies the incorporation of React components within Java-based applications. This document provides an overview of the Kiwi project, its purpose, and an example of its usage.

Overview

Purpose

Kiwi aims to streamline the process of incorporating React components into Java-based projects that utilize template engines. By generating JavaScript code from provided HTML source files, Kiwi facilitates the seamless integration of React components, enhancing the development workflow for Java developers working with modern web technologies.

Example

source

// ./components/Greeting
import React from 'react';

export default function Greeting ({ message, value, clickable }) {
    return (<div>
        <p>Message: {message}</p>
        <p>Value: {value || 0}</p>
        <p>{`Is ${!clikable && "not"} clikable`}</p>
    </div>);
}
Enter fullscreen mode Exit fullscreen mode
// Example.java
var source = """
        @import('Greeting', './components/Greeting')
        @render(<Greeting message="Hello {{name}}!" value:num="10" />)
        @render(<Greeting message="Hello Strange" clickable:bool="true" />)
        """;

var generator = KiwiCompiler.withDefault();
String output = generator.generateJs(source);
String htmlOutput = generator.generateHtml(source);
Enter fullscreen mode Exit fullscreen mode

generated html

<div data-kiwi-id="Greeting1111559717" data-prop-message="Hello {{name}}!" data-prop-value="10"></div>
<div data-kiwi-id="Greeting626337202" data-prop-message="Hello Strange" data-prop-clickable="true"></div>
Enter fullscreen mode Exit fullscreen mode

generated Js

/*
    WARNING: AUTO-GENERATED CODE BY KIWI-GENERATOR
    Modifying this code may result in unintended behavior. 
    Please make changes in the source template or consult 
    the kiwi-react-generator documentation for customization options.
*/
import React from 'react';
import { createRoot } from 'react-dom/client';
import Greeting from './components/Greeting';

const greeting1111559717_elements = document.querySelectorAll('[data-kiwi-id="Greeting1111559717"]');
for(let element of greeting1111559717_elements) {
    const props = {
        message: (element.getAttribute('data-prop-message')),
        value: Number(element.getAttribute('data-prop-value')),
    };
    const root = createRoot(element);
    root.render(<Greeting {...props} />);
}

const greeting626337202_elements = document.querySelectorAll('[data-kiwi-id="Greeting626337202"]');
for(let element of greeting626337202_elements) {
    const props = {
        message: (element.getAttribute('data-prop-message')),
        clickable: 'true' == (element.getAttribute('data-prop-clickable')),
    };
    const root = createRoot(element);
    root.render(<Greeting {...props} />);
}
Enter fullscreen mode Exit fullscreen mode

License

This project is licensed under the MIT License. Feel free to explore, use, and contribute to Kiwi!

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging →

Top comments (0)

Image of DataStax

AI Agents Made Easy with Langflow

Connect models, vector stores, memory and other AI building blocks with the click of a button to build and deploy AI-powered agents.

Get started for free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay