@opengis/fastify-hb
v1.6.1
Published
Іs a plugin for Fastify that integrates the Handlebars templating engine into your Fastify application. It also provides a set of ready-made helpers that can be used when compiling templates.
Downloads
1,818
Maintainers
Keywords
Readme
Fastify Hb
@opengis/fastify-hb
- is a plugin for Fastify that integrates the Handlebars templating engine into your Fastify application. It also provides a set of ready-made helpers that can be used when compiling templates.
Key Features
Integration with Handlebars: Seamlessly integrates Handlebars, a popular templating engine, into your Fastify application, allowing you to create dynamic HTML views with ease.
Ready-Made Helpers: The plugin comes with a collection of built-in helpers to simplify common tasks such as formatting dates, iterating over data, and more.
Support for Asynchronous Helpers: By utilizing the promised-handlebars library, the plugin now supports the creation of asynchronous helpers. This allows you to perform asynchronous operations, such as fetching data from a database, directly within your Handlebars templates.
Install
To install the plugin, use npm:
npm i @opengis/fastify-hb
Usage
To register the plugin in your Fastify application:
fastify.register(import('@opengis/fastify-hb'));
After this, you will get a handlebars object that you can use to compile templates. It will be available in the fastify object.
Example:
export default async function (fastify, opts) {
fastify.get('*', async function (request, reply) {
const { url, fastify } = request;
const { handlebars } = fastify;
const templateContent = await fs.readFile('home.html', 'utf8');
const template = handlebars.compile(templateContent);
const sql = `SELECT * FROM ${table} WHERE gid = $1`;
const result = await pg.query(sql, [id]);
const data = result.rows[0];
const htmlContent = template(data);
return reply.type('text/html').send(htmlContent);
})
}
List of Helpers and Their Usage
contentList
The contentList
helper allows you to query a PostgreSQL database and display the results in your Handlebars templates. It supports various options such as filtering, limiting the number of results, debugging, and dynamically inserting values into the query.
You can use the contentList
helper to fetch data from a specified table and render it within a Handlebars block.
{{#contentList table="gis.dataset" query="enabled = true" limit=5}}
{{#each rows}}
<div>
<strong>{{dataset_name}}</strong>
<p>{{note}}</p>
</div>
{{/each}}
{{/contentList}}
This helper requires the @opengis/fastify-table
library to function properly. Ensure that this package is installed and configured in your Fastify application before using the contentList
helper.
select
The select
helper allows you to query a PostgreSQL database to retrieve specific data based on provided IDs. It dynamically selects the first two columns from the query results and renames them to id
and text
. This helper is useful when you need to fetch and display specific values from a database using Handlebars templates.
You can use the select
helper to fetch data from a specified query and render the results within your Handlebars block.
{{select ids data="your_query_identifier"}}
ids
: The ID or an array of IDs for which you want to fetch the corresponding text values.data
: A reference to the query to execute, as defined in your application's settings.
Example:
{{select ids="1,2,3" data="my_classifier_query"}}
This will fetch and return the text
values for the given IDs (1, 2, 3
) from the query identified by my_classifier_query
.
formatDate
Formats a date according to the specified condition and allows for date and time offsets. It can also output the current date by using the argument "0".
{{formatDate "2021-09-08T12:22:27.983" format='dd.MM.YY hh:mi:sec'}}
formatDigit
Formats numbers by padding them with zeros to the specified length.
{{formatDigit '11' rank=4}}
formatNum
Formatting from number to string
{{formatNumber geom.coordinates.[1] round=5}}
formatRelative
Formats time to show how much time has passed or is left from the specified time.
{{formatRelative '2022-01-13T13:05:40.841795' locate='en'}}
formatUnit
Returns the file size in appropriate units (TB, GB, MB).
{{formatUnit '123.45678' number="2"}}
num_format
Formats numbers to a standardized format.
{{num_format 1 fixed="4"}}
_math
Performs mathematical operations. Allows for simple arithmetic operations.
{{_math operator='-' arg1=10 arg2=5}}
empty
Returns an empty string instead of the content of another helper when compiling a template (page). It is written at the beginning of the helper and allows ignoring further content.
{{empty num_format 1 fixed="4"}}
ifCond
Creates a template or part of it based on the value from a web request and a pre-defined value.
It allows changing the content of the page through a series of checks. There is also an option to specify what to do if the condition is not met.
| Оператор | Опис | | -------- | ------------------------------------------------------------------ | | == | Checks if two values are equal (without type checking). | | != | Checks if two values are not equal (without type checking). | | === | Checks if two values are equal (with type checking). | | !== | Checks if two values are not equal (with type checking). | | > | Checks if the first value is greater than the second. | | < | Checks if the first value is less than the second. | | >= | Checks if the first value is greater than or equal to the second. | | <= | Checks if the first value is less than or equal to the second. | | && | Logical "AND" – checks if both values are true. | | & | Checks if two arrays have common elements. | | !~ | Checks if the second value is not contained in the first (string). | | ~ | Checks if the second value is contained in the first (string). | | in | Checks if the first value is in the second (array or string). | | not in | Checks if the first value is not in the second (array or string). | | period | Checks if the current date is within the specified time period. |
{{#ifCond 'debug' 'in' core.setting}}Condition met{{^}}Condition not met{{/ifCond}}
{{#ifCond 'debug' '==' 'debug'}}Condition met{{/ifCond}}
json
Returns the argument data on the web page in JSON format.
{{json page}}
round
Rounds a number to a certain precision. It can also round down.
{{round 3.14159265359 dec="6"}}
coalesce
Returns the first non-null element. Can be used with other helpers.
{{coalesce color '#337ab7'}}
concat
Concatenates several arguments into one string. Can be used to build links.
{{concat 'https://spending.gov.ua/new/disposers/' edrpou '/agreements/' id}}
split
The split() method splits a String object into an array of strings by separating the string into substrings.
{{split basemap ','}}
str_replace
Performs a replacement of a part of the string with the value of the last argument.
{{str_replace 'Перший рядок<br>Наступний рядок' br=hr}}
translit
Formats characters from Cyrillic to Latin.
{{translit data_name}}
This package is under testing, new helpers will be added soon.