> In order to do blocks, I'm going to have to modify
> BoundTemplate._rawTemplate or
> BoundTemplate._translatedTemplate. I'd rather not be
> modifying objects that begin with an underscore. Or
> should I make my wrapper class a subclass and then I
> won't feel guilty? If I modify _rawTemplate, I'll have
> to call "something" to have it retranslate the template.
I'm hoping to come up a wrapper that preparses for display
logic, and then generates a PSP style class:
[- for item in list: -]
[- end -]
or [% delims %] or [# delims #] or [~ delims ~]
> Does your template-translation code work if the template
> includes a literal "%". Seems like you would have to do
> a first pass and change "%" to "%%" before changing the
> placeholders to "%(key)s". Of course, if the delimeters
> have a "%" the first pass could be problematic.
That's one way of doing it, but it requires an extra regex
search. % is fairly rare on most pages, so for now I'd
like to stick with %% where the intended output is %.
> Is there a reason you prefer to convert the template to
> "%(key)s" and then do percent substitution rather than
> doing it yourself? s = self._rawTemplate
> for key, value in self._nameMappings().values():
> placeholder_list = [left, key, right]
> placeholder = "".join(placeholder_list)
> s = s.replace(placeholder, value)
> There are tradeoffs either way, and maybe the percent
> operator is faster since it's all in C, but on the other
> hand you gotta translate the placeholders so maybe it's a
I did it this way so that the delim substitution is done
one time only then cached. Everytime .fill() is called the
name/value substitution is done again, but using the
sprintf %()s cached version of the template string.
> My block method will have to extract the block from the
> template and replace it with a placeholder in the
> template. Then it will take the extracted portion and
> create a new TemplateFiller for it, returning the new
> object. The caller will be responsible for putting the
> final block result in a place where the first
> TemplateFiller object can see it.
That's a very elegant way of doing it! Forget what I said
about psp preparsing above. Nested logic blocks and loops
shouldn't too difficult. I'm off to play with that idea...