On Fri, May 25, 2012 at 2:44 PM, Edward K. Ream <[email protected]> wrote:
> supporting templates is almost too easy.
One can imagine a whole bunch of buttons, or better, @command nodes,
each of which creates a unique file. Each @command node would specify
A. The "source" outline.
B. The template data and (optionally) the name of the file to be written.
The format of either A or B is completely up to the writer of the
@command node, but here is the simplest thing that could possibly
work:
A. [Finding the source outline] Use g.findNodeAnywhere(c,headline)
B. [Getting the template data] Put the data in a dict.
C. [Executing the common code] Create a node, "@common jinja code" containing::
def run(fn,h,d):
'''Write the template in node with headline h
using data in dict d to the file whose name is fn.
'''
from jinja2 import Template
p = g.findNodeAnywhere(c,h)
if p:
s = g.getScript(c,p,useSelectedText=False,useSentinels=False)
s = Template(s).render(d)
print(s) # fn not used.
else:
print('not found',h)
Actually, as you can see, the output is printed instead of being sent to fn.
Each @command node would have the form::
exec(g.findTestScript(c,'@common jinja code'))
d = {whatever}
run(fileName,headline,d)
As an actual example::
exec(g.findTestScript(c,'@common jinja code'))
d = {'name':'Fred','rank':'colonel','serial_number':'3.14159'}
run('fn','jinja test',d)
If the node "jinja test' contains::
Hello again, {{ name }}.
Your rank is {{rank}} and your tag number is {{serial_number}}.
The output from the trace in run will be::
Hello again, Fred.
Your rank is colonel and your tag number is 3.14159.
That's it. This is all tested code.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/leo-editor?hl=en.