Skip to content

Rendering of JSX based views in C++

License

Notifications You must be signed in to change notification settings

tmehnert/complate-cpp

Repository files navigation

complate-cpp

- Rendering of JSX based views in C++

Build codecov License

complate adapter that can be used in C++ 17 projects to render JSX based HTML views. This project was inspired by complate-java.

Getting started

Usage

For a detailed description how to use this library checkout out the User Guide. I recommend you to check out the dedicated Sample Project to try out complate-cpp.

Define your views using JSX

import {createElement} from 'complate-stream';

export default function Greeting({person}) {
    return <html>
    <head>
        <meta charSet="UTF-8"/>
        <title>Greeting | Example</title>
    </head>
    <body>
    <h1>Hello {person.name}</h1>
    </body>
    </html>
}

Using them to render HTML inside your C++ application

// Please read User Guide - Instantiate a Renderer, for more details.
unique_ptr<Renderer> renderer;
string html = renderer->renderToString("Greeting", Object{
  { "person", Object{
    { "name", "John Doe" }
  }}
});

Installation

# install dependencies
sudo apt install -y build-essential git cmake libv8-dev
# build and install the library
git clone https://github.com/tmehnert/complate-cpp.git && cd complate-cpp
cmake -B build
cmake --build build -j4
sudo cmake --install build/

CMake link installed library

When you have installed the library, you can use find_package and target_link_libraries as follows.

find_package(complate REQUIRED CONFIG)
# and
target_link_libraries(your_app PRIVATE complate::quickjs)
# or
target_link_libraries(your_app PRIVATE complate::v8)

CMake use FetchContent

Another possibility, without having to install the library, is let CMake fetch it. When you use this method, you may not use find_package(complate). Just put the following commands in your CMakeLists.txt file and replace the GIT_TAG HEAD with the commit-sha or git-tag you want.

message("-- Fetching complate...")
FetchContent_Declare(
    complate
    GIT_REPOSITORY https://github.com/tmehnert/complate-cpp.git
    GIT_TAG HEAD
    GIT_SHALLOW 1
)
FetchContent_MakeAvailable(complate)
message("-- Fetching complate - done")
# and
target_link_libraries(your_app PRIVATE complate::quickjs)
# or
target_link_libraries(your_app PRIVATE complate::v8)

Running tests and example

The unittests and the example are not built by default, you have to enable them using BUILD_TESTS=on and BUILD_EXAMPLE=on as shown below.

# install dependencies
sudo apt install -y build-essential git cmake libv8-dev
# build the project with tests and example enabled
git clone https://github.com/tmehnert/complate-cpp.git && cd complate-cpp
cmake -B build -DBUILD_TESTS=on -DBUILD_EXAMPLE=on
cmake --build build -j4
cd build/

# execute tests
ctest --output-on-failure
# run example, which starts a webserver which serves views/greeting.jsx.
example/complate-example

Compatibility

The library is tested with following compilers.

Ubuntu Linux

  • GCC 7 - 10
  • Clang 7 - 12

Microsoft Windows

Only QuickJS supported, build with -DBUILD_V8_RENDERER=off. ThreadLocalRenderer is not supported using MinGW and will raise a compiler error is you accidentally use it.

  • MinGW latest installed from MSYS2.

Apple macOS

Only QuickJS supported, build with -DBUILD_V8_RENDERER=off.

  • AppleClang on macOS 10.15.

Dependencies

  • QuickJS, MIT Licence (bundled)
  • V8, MIT Licence

Dev dependencies

License

complate-cpp is Open Source software released under the Apache 2.0 license.

See LICENCE for more information about bundled software.