Templating¶
Sphinx-Ads allows you to write your own Jinja template files which you can use in your layouts to customise how you want to display your ads.
You assign the template filename as a value to the template
key under a layout. For example, if you create a template called my-custom-template.html, you can use it like this under the presentation object in your ad JSON file:
{
"presentations": {
"horizontal": {
"template": "my-custom-template.html",
}
}
}
Note
You should store the template file under the _templates folder in your Sphinx documentation directory.
You should have one of the following file extensions: .html, .jinja2, or .j2.
The plugin provides the following variables which you can use in your custom Jinja template:
- ads - a Python set-like object containing the key/value pairs (i.e. (ad_ID, ad_value)) of the advertisement data. Example of the
ads
object: ads = dict_items([('sphinx_needs', {'title': 'Sphinx Needs', 'description': 'Sphinx-Needs is an extension for the Python-based documentation framework Sphinx, which you can simply extend by different extensions to fulfill nearly any requirement of a software development team.', 'target_url': 'https://sphinx-needs.readthedocs.io'})])
- ads - a Python set-like object containing the key/value pairs (i.e. (ad_ID, ad_value)) of the advertisement data. Example of the
- layout - a dictionary object containing the following information about the layout you selected:
template - a
str
containing the filename of a Jinja template file.advertisements -
list
containing ad IDs for the ads you want to display.selector - a
str
that contains a CSS selector to select where the ad items will be shown on the web page.