Skip to content

render_handlebars_template

render_handlebars_template(template, context)

Description

Render template with context using handlebars.js templating library

Usage

Call or Deploy render_handlebars_template ?
Call render_handlebars_template directly

The easiest way to use bigfunctions

  • render_handlebars_template function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy render_handlebars_template in your project

Why deploy?

  • You may prefer to deploy render_handlebars_template in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

render_handlebars_template function can be deployed with:

pip install bigfunctions
bigfun get render_handlebars_template
bigfun deploy render_handlebars_template

Examples

select bigfunctions.eu.render_handlebars_template("Hello {{ user }}", json '{"user": "James"}')
select bigfunctions.us.render_handlebars_template("Hello {{ user }}", json '{"user": "James"}')
select bigfunctions.europe_west1.render_handlebars_template("Hello {{ user }}", json '{"user": "James"}')
+------------------+
| rendered_content |
+------------------+
| Hello James      |
+------------------+

Use cases

Let's say you have a BigQuery table with customer data, including their name and purchase history. You want to generate personalized email greetings for each customer, incorporating details from their purchase history. The render_handlebars_template function makes this easy.

Example Scenario:

Your table, customer_data, looks like this:

customer_id customer_name last_purchase_date last_purchase_amount
1 Alice 2024-03-15 50.00
2 Bob 2024-03-22 100.00
3 Carol 2024-03-29 25.00

You could use the following query:

SELECT
    customer_id,
    bigfunctions.us.render_handlebars_template(
        """
        Hello {{customer_name}},

        Thank you for your recent purchase on {{last_purchase_date}} for ${{last_purchase_amount}}. We appreciate your business!
        """,
        TO_JSON_STRING(STRUCT(customer_name, last_purchase_date, last_purchase_amount))
    ) AS personalized_email
  FROM
    `your-project.your_dataset.customer_data`;

This query would produce a table with the customer_id and personalized_email:

customer_id personalized_email
1 Hello Alice,\n\nThank you for your recent purchase on 2024-03-15 for $50.00. We appreciate your business!
2 Hello Bob,\n\nThank you for your recent purchase on 2024-03-22 for $100.00. We appreciate your business!
3 Hello Carol,\n\nThank you for your recent purchase on 2024-03-29 for $25.00. We appreciate your business!

Explanation:

  1. Template: The first argument to render_handlebars_template is the template string. It uses Handlebars syntax ({{variable_name}}) to denote placeholders that will be replaced with actual values.

  2. Context: The second argument is a JSON string representing the context. This provides the values for the placeholders in the template. TO_JSON_STRING(STRUCT(...)) is used to convert the desired columns into a JSON object.

  3. Result: The function substitutes the values from the context into the template, generating the personalized email greeting for each customer.

Other Use Cases:

  • Generating dynamic reports: Create report templates with placeholders for metrics, dates, and other data, then populate them using query results.
  • Creating custom error messages: Craft more informative error messages by incorporating dynamic context from the data.
  • Formatting data for external APIs: Prepare data in specific formats required by external services using templating.

This function provides a flexible and powerful way to generate dynamic text within BigQuery, improving tasks involving personalization, reporting, and data formatting.


Need help or Found a bug?
Get help using render_handlebars_template

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about render_handlebars_template

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on