Skip to content

bigfunctions > render_template

render_template

Call or Deploy render_template ?

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

  • This render_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_template(template, context)

Description

Render template with context using nunjucks.js templating library

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

Need help using render_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_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 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.

Spread the word

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

Share on Add a on