Sprajagopal, you have begun an excellent project, namely adding on-the-fly 
rendering of Jinja <https://jinja.palletsprojects.com/en/2.11.x/> templates 
to the viewrendered (VR) plugin. PR 1738 
<https://github.com/leo-editor/leo-editor/pull/1738> records the present 
status.

Just today I started learning about Jinja, by doing the hello-world example 
<https://jinja.palletsprojects.com/en/2.11.x/intro/#basic-api-usage>. Jinja 
is an appropriate sabbatical topic, because Django 
<https://www.djangoproject.com/start/overview/>can use Jinja as a rendering 
engine. Reading between the lines, it would seem that Jinja is safer than 
DTL <https://docs.djangoproject.com/en/3.1/ref/templates/language/>, 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

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/a1c70658-ad7e-4fa4-a08c-c371b23c495an%40googlegroups.com.

Reply via email to