Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion langfun/core/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ def _preprocess_template(self, template_str: str) -> str:
"""
return template_str

def _postprocess_rendered(self, rendered_text: str) -> str:
"""Postprocesses the rendered text."""
return rendered_text

def vars(
self,
specified: bool | None = None,
Expand Down Expand Up @@ -441,7 +445,9 @@ def render(
# natural language when they are directly returned as rendering
# elements in the template.
with modality.format_modality_as_ref():
rendered_text = self._template.render(**inputs)
rendered_text = self._postprocess_rendered(
self._template.render(**inputs)
)

# Carry the modality references passed from the constructor.
# This is to support modality objects that is already rendered
Expand Down
16 changes: 16 additions & 0 deletions langfun/core/template_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ def _preprocess_template(self, template_str: str) -> str:
'Google is good'
)

def test_postprocess_rendered(self):

class MyTemplate(Template):
"""My template with postprocess.

$COMPANY {{x}}
"""

def _postprocess_rendered(self, rendered_text: str) -> str:
return rendered_text.replace('$COMPANY', 'Google')

self.assertEqual(
MyTemplate(x='is $COMPANY').render(),
'Google is Google'
)


class FromValueTest(unittest.TestCase):

Expand Down
Loading