JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q52557F
  • License ISC

І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.

Package Exports

  • @opengis/fastify-hb
  • @opengis/fastify-hb/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@opengis/fastify-hb) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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.

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

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.