Skip to content

bigfunctions > render_handlebars_template

render_handlebars_template

Call or Deploy render_handlebars_template ?

✅ You can call this render_handlebars_template bigfunction directly from your Google Cloud Project (no install required).

  • This render_handlebars_template function is deployed in bigfunctions GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error).
  • Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • You may prefer to deploy the BigFunction in your own project if you want 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). Discover the framework

Public BigFunctions Datasets:

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...

Description

Signature

render_handlebars_template(template, context)

Description

Render template with context using handlebars.js templating library

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      |
+------------------+

Need help using render_handlebars_template?

The community can help! Engage the conversation on Slack

For professional suppport, don't hesitate to chat with us.

Found a bug using 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.

For professional suppport, don't hesitate to chat with us.

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.

Spread the word

BigFunctions is fully open-source. Help make it a success by spreading the word!

Share on Add a on