Multiple Data Sources

Data Files

Part details, indexed by a part number:

1WB0002:
  description: "Rear Window"
2DR0013:
  description: "Rear Door"
1DX0077:
  description: "Hatchback Door"

Inventory counts:

1WB0002,1001
2DR0013,401
1DX0077,14

Using load() in a Template

The load() function visible in the template context can be used to load static data sources.

.. -*- mode: rst -*-

{# The details about parts in inventory are kept in a separate data
   file from the stock quantity. #}
{% set parts = load('part-details.yaml') %}

.. list-table:: What's in stock
   :header-rows: 1

   * - part-num
     - quantity
     - description
   {% for item in data | sort %}
   * - {{ item[0] }}
     - {{ item[1] }}
     - {{ parts[item[0]].description }}
   {% endfor %}

load() will attempt to guess the format for a data source based on the name by looking at file extensions. To explicitly select a format, pass the name in the data_format argument.

{% set parts = load('part-details.dat', data_format='yaml') %}

The arguments given to the datatemplate directive are also passed to the loader used by load(). To override those settings, or provide different values for a different format, pass the arguments directly to load().

{% set parts = load('part-details.dat', data_format='json', encoding='UTF-8') %}

Note

Database formats like dbm are not supported, and should be used as the primary data source for a datatemplate directive.

Loading the Template

.. datatemplate:csv:: inventory.csv
   :template: inventory.tmpl

Rendered Output

What’s in stock

part-num

quantity

description

1DX0077

14

Hatchback Door

1WB0002

1001

Rear Window

2DR0013

401

Rear Door