Comments re @jinja extension to viewrendered.py

24 views
Skip to first unread message

Edward K. Ream

unread,
Nov 17, 2020, 12:08:39 PM11/17/20
to leo-editor

Sprajagopal, you have begun an excellent project, namely adding on-the-fly rendering of Jinja templates to the viewrendered (VR) plugin. PR 1738 records the present status.

Just today I started learning about Jinja, by doing the hello-world example. Jinja is an appropriate sabbatical topic, because Django can use Jinja as a rendering engine. Reading between the lines, it would seem that Jinja is safer than DTL, but that's just my guess.

My next step was to convert the hello-world example to the format required for VR. This revealed some problems...

Error recovery

Imagine you have never used @jinja before, and imagine that you have never read any documentation. (I don't see any in the PR). You might do what I did:

- Enable VR.
- Add an @jinja node.
- Open the VR pane (Alt-0)

Oops:

Traceback (most recent call last):
  File "c:\leo.repo\leo-editor\leo\core\leoPlugins.py", line 322, in callTagHandler
    result = handler(tag, keywords)
  File "c:\leo.repo\leo-editor\leo\plugins\viewrendered.py", line 905, in update
    f(s, keywords)
  File "c:\leo.repo\leo-editor\leo\plugins\viewrendered.py", line 1536, in update_jinja
    if not template_path:
UnboundLocalError: local variable 'template_path' referenced before assignment


No big deal. Now trying to add all the various pieces needed, as I did. I think you'll find problems at each step. For example, sometimes rendering can replace body text.

It was difficult for me to understand how to make c.getNodePath(p) return the correct values. The @path directive influences this, so it can be set by hand if required, but doing so is clumsy. It might be better to use some kind of "jinja base directory" setting.

Suggestions

Rendering jinja is more complicated than rendering other kinds of VR nodes because of the various settings involved:

jinja2 must know where to write the template file. As stated above, @path, that is, c.getNodePath(p), are involved. How about putting key-value pairs in the @jinja node itself?

Hmm. For testing we could dispense with an external template file by creating a custom loader that gets the template from an @jinja-template node.

Or better, get the template from an @data jinja-template node. Leo can "compose" the contents of the @data node from child nodes. It's a cool, little-known, capability. Note: within @data nodes, lines that start with '#' are comments.

Summary

I like the idea of rendering jinja in the VR plugin. The update_jinja method, and its helpers, are an excellent start. My next step is to get name substitutions to work, hehe.

The Imo, there should be easier ways of specifying options and the template itself. This post has only provided some hints. Other ways may be simpler. Whatever choices are made, the following problems must be corrected:

- Simple newbie mistakes should give clear error messages.
- Rendering should never replace body text, except possibly in a single, designated, node.

All comments and suggestions welcome.

Edward

Edward K. Ream

unread,
Nov 17, 2020, 12:27:33 PM11/17/20
to leo-editor
On Tuesday, November 17, 2020 at 11:08:39 AM UTC-6 Edward K. Ream wrote:

It was difficult for me to understand how to make c.getNodePath(p) return the correct values. The @path directive influences this, so it can be set by hand if required, but doing so is clumsy.

You could say I was "lucky" in putting @jinja nodes in a file in my home directory. This revealed various problems with paths. The code should report missing directories and files without crashing. More importantly, the code should be tested in "unusual" places. On windows, I got several access errors.

First successful test

- @jinja (body will contain rendered output, as does the VR pane)
  - @jinja template (body should contain path to template)
  - @jinja inputs
    - @jinja variable name (body contains value)

Example:

- Body of `@jinja template` node contains `c:\test\JinjaTemplates\TestTemplate.txt`
- TestTemplate.txt contains `Hello {{ name }}!`
- Body of `@jinja variable name` contains `World`
- The output (in the body of @jinja node, and the VR pane) is `Hello World`.

Edward
Reply all
Reply to author
Forward
0 new messages