npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@opengis/fastify-hb

v1.7.0

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

757

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.