Description


Utility pghtml recursively scans the specified directory when finds files with .pg* extensions (configurable) reads them, replaces tags and variables (see below), and saves to files of the same name, removing pg prefix from the extension. For example, file example.pghtml will be replaced and saved to file example.html.

Typically used depending on server configuration

Execution occurs according to the schedule from cron under the user postgres. Utility pghtml compares contents of the existing file with new one, and if the contents of the files have not updates, then the file is not overwritten and redeploy does not occur.


Tag <pghtml-sql>

Synopsis
<pghtml-sql>[SQL query]</pghtml-sql>

Action
Content of tag is executed as SQL query, result obtained from the database is replaced instead of tag


Example
<pghtml-sql>
select now() - pg_postmaster_start_time()
</pghtml-sql>

<pghtml-sql>
select datname from pg_database order by 1
</pghtml-sql>

<a href="<pghtml-sql>select 'http://mysite/'||'${directory}'</pghtml-sql>">Page</a>

<pghtml-sql>
select json_agg(t) from (
  select schemaname "schema",indexrelname "index",idx_scan scans 
    from pg_catalog.pg_stat_all_indexes
    order by idx_scan desc limit 5
) t
</pghtml-sql>





37 days 21:19:21.25639


postgres
template0
template1


<a href="http://mysite/example/">Page</a>

[{"schema":"pg_catalog","index":"pg_proc_oid_index","scans":361}, 
 {"schema":"pg_catalog","index":"pg_proc_proname_args_nsp_index","scans":345}, 
 {"schema":"pg_catalog","index":"pg_attribute_relid_attnum_index","scans":198}, 
 {"schema":"pg_catalog","index":"pg_class_oid_index","scans":196}, 
 {"schema":"pg_catalog","index":"pg_cast_source_target_index","scans":120}]



Тег <pghtml-include>

Synopsis
<pghtml-include [var_name]="[var_value]">[/][path/][filename]</pghtml-include>

Action
Content of the tag is parsed as file path, which is reeded and replaced instead of tag. Tag attributes are converted to variables. If file path starts with a slash "/", then the file location is taken from the directory which is passed as a parameter when calling the utility (usually this is the root directory of the site). Otherwise, from the directory of the source file. Inclusions is supported regardless of the extension.

Example
<!-- inclusion of styles for acceleration -->
<style type="text/css">
<pghtml-include>/css/common.css</pghtml-include>
</style>

<!-- inclusion of title with two variables "G_SITE" and "description" -->
<pghtml-include description="Example">title.html</pghtml-include>


--
file title.html: 
<title>${G_SITE} | ${description}</title>





<!-- inclusion of styles for acceleration -->
<style type="text/css">
html {
  font: 12px Verdana, Arial, Helvetica, sans-serif;
}

p {
  font-size: 16px; font-weight: bold;
}
</style>

<!-- inclusion of title with two variables "G_SITE" and "description" -->
<title>PGHtml | Example</title>
                     



Variable ${variable}

Synopsis

${[name]}


Action
Variables are replaced with their values and set in one of four methods:
  • сontent of element <pghtml-var name="var_name">[var_value]</pghtml-var>
  • parameters -G_[name] [value] on the command line when executing utility
  • tag attributes <pghtml-include [var_name]="[var_value]">
  • built-in variables
    ${directory_root}
    ${directory}
    ${filename}
    ${filepath_source}
    ${filepath_dest}
    -
    -
    -
    -
    -
    root directory passed on the command line
    directory of processed file
    processed file
    absolute path to source file
    absolute path to destination file

Example
<!-- built-in variables -->
directory_root   = ${directory_root}
directory        = ${directory}
filename         = ${filename}
filepath_source  = ${filepath_source}
filepath_dest    = ${filepath_dest}

<!-- multiline variable -->
<pghtml-var name="text">
Line 1
<br>
Line 2
</pghtml-var>

text:
${text}

<!-- using variable "directory" in SQL query -->
length(directory) = <pghtml-sql>select length('$[directory]')</pghtml-sql>

<!-- inclusion of title with two variables "G_SITE" and "description" -->
<pghtml-include description="Example">title.html</pghtml-include>

--
file title.html: 
<title>${G_SITE} | ${description}</title>







<!-- built-in variables -->
directory_root   = /mysite/
directory        = static/
filename         = example.pghtml
filepath_source  = /mysite/static/example.pghtml
filepath_dest    = /mysite/static/example.html
                 
<!-- multiline variable -->
text:

Line 1
<br>
Line 2

<!-- using variable "directory" in SQL query -->
length(directory) = 7

<!-- inclusion of title with two variables "G_SITE" and "description" -->
<title>PGHtml | Example</title>                     



Command line

Command line options (after installation, can view them by executing pghtml --help)
[root@pgsuite ~]# pghtml --help
PGHtml is HTML template engine using PostgreSQL
version 24.2.7, linux 64 bits

Usage:
  pghtml {COMMAND} [OPTIONS]

Commands:
  start        start execution in another process
  execute      execute in current process
  status       show runtime status
  stop         stop execution
  sync         only synchronize directory and exit (some options are ignored)
  help|man     print this help

HTTP options:
  -hd DIRECTORY       site directory (default: /site)
  -hi INTERVAL        synchronization interval in seconds (default: 600)
  -hp PORT            HTTP port (default: 5480)

Connection options:
  -h HOSTNAME        database server host (default: server IP address)
  -p PORT            database server port (default: 5432)
  -d DBNAME          database name (default: site)
  -U USERNAME        username for service only (default: postgres)
  -W PASSWORD        password of user for service (if nessesary)

Logging options:
  -l  FILE           log file

Examples:
  pghtml start -d sitedb -l /var/log/pghtml.log
  pghtml execute -hd /mysite -h server-db.mycompany.com -d sitedb -U admin -W 12345
  pghtml sync -hd /site/db -d sitedb