Inline Sample (JSON)

This example demonstrates how to use an inline template, as well as accessing the HTML context available to all datatemplate directives.

Data File

{
    "key1": "value1",
    "key2": [
        "list item 1",
        "list item 2",
        "list item 3"
    ],
    "nested-list": [
        ["a", "b", "c"],
        ["A", "B", "C"]
    ],
    "mapping-series": [
        {"cola": "a", "colb": "b", "colc": "c"},
        {"cola": "A", "colb": "B", "colc": "C"}
    ]
}

HTML Context

# from conf.py
html_context = {
    'sample': 'Sample context value set in conf.py',
}

Template File

Individual Item
~~~~~~~~~~~~~~~

{{ data['key1'] }}

List of Items
~~~~~~~~~~~~~

{% for item in data['key2'] %}
- {{item}}
{% endfor %}

HTML Context
~~~~~~~~~~~~

{% for key, value in config.html_context.items() %}
- ``{{key}}`` = ``{{value}}``
{% endfor %}

Loading the Template

.. datatemplate:json::
   :source: sample.json

   Individual Item
   ~~~~~~~~~~~~~~~

   {{ data['key1'] }}

   List of Items
   ~~~~~~~~~~~~~

   {% for item in data['key2'] %}
   - {{item}}
   {% endfor %}

   HTML Context
   ~~~~~~~~~~~~

   {% for key, value in config.html_context.items() %}
   - ``{{key}}`` = ``{{value}}``
   {% endfor %}

Rendered Output

Individual Item

value1

List of Items

  • list item 1
  • list item 2
  • list item 3

HTML Context

  • sample = Sample context value set in conf.py
  • using_theme = False
  • html_theme = sphinx_rtd_theme
  • current_version = 0.7.1.post1
  • version_slug = 0.7.1.post1
  • MEDIA_URL = https://media.readthedocs.org/
  • STATIC_URL = https://assets.readthedocs.org/static/
  • PRODUCTION_DOMAIN = readthedocs.org
  • versions = [('latest', '/en/latest/'), ('stable', '/en/stable/'), ('0.7.1.post1', '/en/0.7.1.post1/'), ('0.7.1', '/en/0.7.1/'), ('0.7.0', '/en/0.7.0/'), ('0.6.1', '/en/0.6.1/'), ('0.6.0', '/en/0.6.0/'), ('0.5.0', '/en/0.5.0/'), ('0.4.0', '/en/0.4.0/'), ('0.3.0', '/en/0.3.0/'), ('0.2.0', '/en/0.2.0/'), ('0.1.0', '/en/0.1.0/')]
  • downloads = []
  • subprojects = []
  • slug = sphinxcontribdatatemplates
  • name = sphinxcontrib.datatemplates
  • rtd_language = en
  • programming_language = py
  • canonical_url = https://sphinxcontribdatatemplates.readthedocs.io/en/latest/
  • analytics_code = None
  • single_version = False
  • conf_py_path = /doc/source/
  • api_host = https://readthedocs.org
  • github_user = sphinx-contrib
  • proxied_api_host = /_
  • github_repo = datatemplates
  • github_version = 0.7.1.post1
  • display_github = True
  • bitbucket_user = None
  • bitbucket_repo = None
  • bitbucket_version = 0.7.1.post1
  • display_bitbucket = False
  • gitlab_user = None
  • gitlab_repo = None
  • gitlab_version = 0.7.1.post1
  • display_gitlab = False
  • READTHEDOCS = True
  • new_theme = True
  • source_suffix = .rst
  • ad_free = False
  • docsearch_disabled = False
  • user_analytics_code = ````
  • global_analytics_code = UA-17997319-1
  • commit = 0fc5c6b5