Skip to content

render_template

render_template(template, context)

Description

Render template with context using nunjucks.js templating library

Usage

Call or Deploy render_template ?
Call render_template directly

The easiest way to use bigfunctions

  • render_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_template in your project

Why deploy?

  • You may prefer to deploy render_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_template function can be deployed with:

pip install bigfunctions
bigfun get render_template
bigfun deploy render_template

Examples

select bigfunctions.eu.render_template("Hello {{ user }}", json '{"user": "James"}')
select bigfunctions.us.render_template("Hello {{ user }}", json '{"user": "James"}')
select bigfunctions.europe_west1.render_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 using a template.

Table Example:

customer_id customer_name last_purchase_date
1 Alice 2023-10-26
2 Bob 2023-10-27
3 Charlie 2023-10-28

Template:

Hello {{ customer_name }},

Thank you for your recent purchase on {{ last_purchase_date }}. We appreciate your business!

Sincerely,

The Team

BigQuery SQL using render_template:

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

        Thank you for your recent purchase on {{ last_purchase_date }}. We appreciate your business!

        Sincerely,

        The Team
        """,
        TO_JSON_STRING(STRUCT(customer_name, last_purchase_date))
    ) AS personalized_email
  FROM
    `your_project.your_dataset.your_customer_table`

This query will generate a new column personalized_email containing the rendered email greeting for each customer. The TO_JSON_STRING function converts the STRUCT of customer_name and last_purchase_date into a JSON string which is then used as the context for the template.

Result:

customer_id personalized_email
1 Hello Alice,

Thank you for your recent purchase on 2023-10-26. We appreciate your business!

Sincerely,

The Team
2 Hello Bob,

Thank you for your recent purchase on 2023-10-27. We appreciate your business!

Sincerely,

The Team
3 Hello Charlie,

Thank you for your recent purchase on 2023-10-28. We appreciate your business!

Sincerely,

The Team

This demonstrates how render_template can be used for dynamic content generation based on data within BigQuery, useful for various applications like personalized emails, custom reports, or dynamic SQL query generation. You can use more advanced templating features like loops and conditional logic provided by nunjucks.js as well.


Need help or Found a bug?
Get help using render_template

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about render_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