tardyp / sphinx-jinja

MIT License
28 stars 22 forks source link

jinja directive inline #37

Open besessener opened 1 year ago

besessener commented 1 year ago

I am using this directive to include tables into my rst docs

    .. datatemplate:yaml:: _tables/data_dict.yaml
        :template: data_dict.tmpl

In the yaml I can use inline directive like :math: or :code:.

Not sure now wether there is something similar for .. jinja::. I tried wrapping the jinja directive arouond datatemplate, but it did nor resolve the expressions.

Is there a way to use it inline in the described context? It would be even better if I could not only use it for textual conten in the yaml file but also to add/remove some items within yaml with this approach.

Does that make sense?

salomon-chung commented 2 months ago

Hello,

If you want to combine sphinx-jinja with sphinxcontrib.datatemplates I would say I have the same issue.

Here is the workaround I implemented though: https://gitlab.com/uranie-cea/documentation/data-templates

Configurations

In conf.py, I defined:

jinja_contexts = {
    'sentence': {'open': '{{', 'close': '}}', 'verb': 'have'}
}

And my elements.json is simply:

{
    "object": "1 hat"
}

Combination

Then I combined Jinja and Data Templates, see test.rst#L31:

.. jinja:: sentence
    :debug:

    .. datatemplate:json::
        :source: elements.json

        I {{ verb }} {{ open }} data['object'] {{ close }}.

The order of substitutions seems to be:

  1. Jinja
  2. Data Templates

So I used the open/close trick.

I also tested the other way around: Data Templates then Jinja, see _templates/test.tmpl.

Result

Here is the demo: https://data-templates-uranie-cea-documentation-cbd39ac5388997abccdac07.gitlab.io/test.html

compositions

Has someone a cleaner solution?

Best regards,