A PostgreSQL extension for rendering the Tera HTML templating language.

Related tags

Command-line pgtera
Overview

PGTera

PGTera is a PostgreSQL extension that provides functions for using Tera to render HTML templates.

When used with a tool like Postgrest, you can serve HTML directly from the database without managing an intermediate layer.

Usage

Template files in: /var/www

/var/www/
  index.html
  about.html

Code in index.html

<html>
    <title>Home Page</title>
    <body>
        {% for link in links %}
            <a href="{{ link.href | safe }}">{{ link.name }}</a>
        {% endfor %}
    </body>
</html>

Render the template in your SQL code. The Second argument of pgtera_render is the context which must be passed in as an array of JSON objects that conforms to the following type:

struct CtxInput {
    name: String,
    value: Value
}

Where Value is the serde_json Value enum.

create extension pgtera;

select pgtera_set_render_path('/var/www/**/*.html');

select pgtera_render(
    'index.html',
    $ctx$
    [{
        "name": "links",
        "value": [
            { "name": "Tera Docs", "href": "https://keats.github.io/tera/docs/" },
            { "name": "PGTera", "href": "https://github.com/f-prime/pgtera" }
        ]
    }]
    $ctx$
);

Which will return

 <html>                                                                
     <title>Home Page</title>                                          
     <body>                                                            
                                                                       
             <a href="https://keats.github.io/tera/docs/">Tera Docs</a>
                                                                       
             <a href="https://github.com/f-prime/pgtera">PGTera</a>    
                                                                       
     </body>                                                           
 </html>                                                               

If you would like to write your HTML directly in your SQL, you can use the pgtera_render_str function:

select pgtera_render_str(
    $template$
    <ul>
        {% for x in people %}
            <li>{{ x }}</li>
        {% endfor %}
    </ul>
    $template$,
    $ctx$
    [
        { "name": "people", "value": ["John", "Jake", "James", "Jeff"] }
    ]
    $ctx$
);

Installation

This will install pgtera to the default Postgres installation on your system. For more detailed installation options, checkout the cargo-pgrx README

  1. cargo install cargo-pgrx
  2. cargo pgrx install
You might also like...
A Rust 🦀️ font loading, positioning and rendering toolkit

Toolkit used to load, match, measure and render texts. NOTE: This project is a work in progress. Text measuring and positioning is a complex topic. A

A rust crate for rendering large text to the terminal using font8x8 and ratatui.
A rust crate for rendering large text to the terminal using font8x8 and ratatui.

tui-big-text tui-big-text is a rust crate that renders large pixel text as a ratatui widget using the glyphs from the font8x8 crate. Installation carg

A binary that bootstraps a Leptos application with client side rendering, tailwind, and vercel

create-leptos-csr-tw This CLI provides a quick setup to start building web applications using the Leptos web framework integrated with TailwindCSS. It

💫 List-rendering component utilizing FLIP position transitions for Leptos
💫 List-rendering component utilizing FLIP position transitions for Leptos

AnimatedFor / component for Leptos FLIP animations for element and component groups inspired by Vue's TransitionGroup. This crate exports a compon

General purpose cross-platform GIS-rendering library written in Rust
General purpose cross-platform GIS-rendering library written in Rust

Galileo is a general purpose cross-platform geo-rendering library. Web examples Raster tile layer (OSM) Vector tile layer (Maplibre) Use buttons at th

Fermyon Spin + Dioxus - Client Side Rendering (CSR) template with TailwindCSS
Fermyon Spin + Dioxus - Client Side Rendering (CSR) template with TailwindCSS

spin-dioxus-csr Fermyon Spin + Dioxus - Client Side Rendering (CSR) template with Tailwind Setup Accomodate "chicken & egg" issue for https://github.c

Tools - The Rome Toolchain. A linter, compiler, bundler, and more for JavaScript, TypeScript, HTML, Markdown, and CSS.
Tools - The Rome Toolchain. A linter, compiler, bundler, and more for JavaScript, TypeScript, HTML, Markdown, and CSS.

Rome is currently being rewritten in Rust. Read more about it in our latest blog post. The documentation below is out of date and available for poster

Minimal Pandoc compiler - HTML

Minimal Pandoc compiler - HTML

An html macro for dioxus applications.

dioxus html macro This crate offers an html! like macro for dioxus applications. It expands to the equivalent rsx! macro call you would have made othe

Comments
Owner
Frankie
I like building things.
Frankie
Experimental extension that brings OpenAI API to your PostgreSQL to run queries in human language.

Postgres <> ChatGPT Experimental PostgreSQL extension that enables the use of OpenAI GPT API inside PostgreSQL, allowing for queries to be written usi

CloudQuery 315 Apr 10, 2023
Discord-to-Cohost compiler written in Rust using Tera

Cohoard - Post formatted chatlogs to Cohost! Cohoard is a tool for turning chatlogs into formatted posts on Cohost. You can use Cohoard to easily form

Aaron Kofsky 14 Dec 15, 2022
A minimal browser with a super simple rendering engine for HTML and CSS, using Rust.

minimal-browser A minimal browser with a super simple rendering engine for HTML and CSS, using Rust. Credit: https://github.com/mbrubeck and https://l

Federico Baldini 3 Jan 15, 2023
Cornucopia is a small CLI utility resting on tokio-postgres and designed to facilitate PostgreSQL workflows in Rust

Cornucopia Generate type checked Rust from your SQL Install | Example Cornucopia is a small CLI utility resting on tokio-postgres and designed to faci

Louis Gariépy 1 Dec 20, 2022
Background task processing for Rust applications with Tokio, Diesel, and PostgreSQL.

Async persistent background task processing for Rust applications with Tokio. Queue asynchronous tasks to be processed by workers. It's designed to be

Rafael Carício 22 Mar 27, 2023
This crate provides a set of functions to generate SQL statements for various PostgreSQL schema objects

This crate provides a set of functions to generate SQL statements for various PostgreSQL schema objects, such as tables, views, materialized views, functions, triggers, and indexes. The generated SQL statements can be useful for schema introspection, documentation, or migration purposes.

Tyr Chen 11 Apr 4, 2023
A utility for exporting administrative/moderation statistics from your Lemmy instance's PostgreSQL database to InfluxDB!

Lemmy (Stats) Data Exporter About This Project This project aims to act as a bridge between Lemmy's PostgreSQL database and InfluxDB, primarily to tra

Russell 3 Jul 5, 2023
Metaballs (blobs) coded in Rust. 100% software rendering.

Metaballs (blobs) coded in Rust. 100% software rendering. It is basically a Rust version of my old demo effect from the 90s (back then we'd use a fake Phong shading, though).

Maciej Sinilo 7 Dec 4, 2022
A CLI tool used for rendering Blender files to a cropped spritesheet

BuilderGenerator A CLI tool used for rendering Blender files to a cropped spritesheet Clients Rust

Eric Olson 1 Feb 18, 2022
Make data-driven table rendering easy with Dioxus

Dioxus Table Make data-driven table rendering easy with Dioxus Installation Until the next release of Dioxus this requires Dioxus nightly from git. Th

null 9 Oct 9, 2022