Search code examples
pythonjinja2cement

How to override Jinja2 extension settings in Cement Framework?


I'm a new user of Cement Framework and Python. I try to override the default configuration of jinja2 template handler to set trim_blocks to True. I see the default value of Environment settings in the extension, but I really wonder where is the correct place to override it in Cement ?

Thank you for your help.


Solution

  • After many tests, I found a way to override Jinja2 extension. I hope it's a good way to do it.

    To avoid doing it directly in the command class, I used Cement hook "post_argument_parsing". I've defined this hook in the main class in the file "main.py" like this :

    def jinja2_settings_override(self):
       # Jinja2 Environment context
       env = self.output.templater.env
       env.trim_blocks = True
    
    class MyCommandClass(App):
        """Primary application."""
    
        class Meta:
            label = 'my_command'
    
            # ... #
    
            # register the hook to override jinja2 extension settings.
            hooks = [
                ('post_argument_parsing', jinja2_settings_override),
            ]