I'm trying to set up an object instance that will provide values to the Cheetah3 text templating engine.
This is my text template script...
#filename: py_text_template.py
from traits.api import String, Range
from traits.api import HasTraits
from loguru import logger
from Cheetah.Template import Template
class Info(HasTraits):
keyword_integer = Range(value=0, low=1, high=60)
keyword_string = String(value="snack", regex=r"^\w+")
@logger.catch(onerror=lambda _: sys.exit(1))
def generate_text_template():
myinfo = Info(keyword_integer=10, keyword_string="snack-attack")
t = Template("On the first $myinfo.keyword_string, my true love")
print(t)
generate_text_template()
I expected to use the myinfo instance of the Info() class to populate the Template, but I get the following error...
Cheetah.NameMapper.NotFound: cannot find 'myinfo'
For reasons I don't yet understand,
Cheetahdoes not follow normal conventions to access object instance attributes and methods.To fix the problem, I had to replace the
$myinfo.keyword_stringcall with$keyword_string. Then I addedsearchList=[myinfo]to theTemplate()call...The object instances in
searchListare searched for the dictionary key, attribute, or method in question.The complete working
Cheetahscript was:Running this under python3.7 yields the expected results...
This script requires installing:
CheetahTraitsloguruI'm using:
Linux mudslide 5.10.0-9-amd64 #1 SMP Debian 5.10.70-1 (2021-09-30) x86_64 GNU/Linux