Skip to content

f-prime/pgtera

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A PostgreSQL extension for rendering the Tera HTML templating language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages