tardyp / sphinx-jinja

MIT License
28 stars 22 forks source link

Referencing jinja macros #14

Closed flexatone closed 2 years ago

flexatone commented 4 years ago

Greetings, and thanks for this very useful tool.

I am trying to use a jinja macro within a number of RST files. Where can I put a macros.rst file such that other RST files in the Sphinx source directory (and using the .. jinja:: directive) can import it? I tried putting the macros.rst file in source as well, but I get a jinja2.exceptions.TemplateNotFound: macros.rst error.

flexatone commented 4 years ago

The solution that worked for me was simply providing the appropriate relative path.

.. jinja:: ctx

    {% import 'doc/source/macros.jinja' as macros %}
flexatone commented 4 years ago

While I was able to get this to work, I am trying to deploy my docs on ReadTheDocs, and the relative path specification does not work as expected.

Is there a way that, when sphinx-jinja is used, the Sphinx "sources" directory can always be included in the directories searched for macros, such that macro file names can be given relative to that directory, i.e.: {% import 'macros.jinja' as macros %}?

tardyp commented 4 years ago

Hello, maybe there is a way to change the current directoy while parsing the template. Have a look at the source code, it is pretty straighforward. :)

flexatone commented 4 years ago

Many thanks for your response. Let me try what you suggest; if it works, I will submit a PR.

mathiasertl commented 2 years ago

Hi @flexatone ,

It seems to me you might have been affected by #25. If I'm right, that means that in the current release (1.2.1) you need {% import 'source/macros.jinja' as macros %}, with 1.3.0 you'd have to use path relative to the source, like you would for include or any other directive referencing a file: {% import 'macros.jinja' as macros %}.

kr, Mat

flexatone commented 2 years ago

Thank you for following up on this! This seems to have worked and I can now use my Jinja macro in both my local builds and on ReadTheDocs.