Question

I'm creating a workflow app with pyramid and i'm searching how to make the application modulable : meaning create a core app with sqlalchemy models, base forms with wtforms, and some base templates with mako.

The basic structure of the "Core" app is:

    App_Core/core.ini
            /setup.py
            /...
            /App_Core/
                    /__init__.py
                    /models.py
                    /forms.py
                    /utils.py
                    /templates/
                    /templates/base.mako...
                    /static/
                    /static/staticfiles...

My goal is to create 1 application per workflow which will be included in the Core app : it seems possible to do that via the includeme function provided with pyramid.

I want to include each workflow via the core.ini file, for example:

pyramid.includes =       
   workflow_app1
   workflow_app2
   workflow_app3
   ...

I defined an new app called workflow_app1 with the following structure:

worflow_app1/
            /setup.py
            /...
            /workflow_app1/
                         /__init__.py
                         /models.py
                         /forms.py
                         /views.py
                         /templates/
                         /templates/workflow_app1.mako
                         /...

And the _init_.py file will contain the includeme function and will define new routes:

def includeme(config):
    config.add_route('route1', '/route1/')
    config.add_route('route2', '/route2/')
    config.scan()

When i'm writing a view for the worflow_app1, i'm rendering to a template included with that app, but when i'm calling it from the core app, it can't render the template and gives the following error:

TopLevelLookupException: Cant locate template for uri 'workflow-app1.mako'

This error quite logical cause the mako.directories directive is given with the path App_Core_PATH/templates so my template should be in the same folder.

Question1: Is it possible to make mako searching in each folder of modules the wanted templates?

Question2: Is it possible to make the workflow-app1.mako inheriting of the base.mako from the core app?

Thanks by advance for your answer.

Was it helpful?

Solution

The solution that I would recommend is switching to asset specs for your templates. They are explicit, allow overriding, and provide better control over your template hierarchy. This means that you would stop using mako.directories and instead use 'workflow_app1:templates/workflow_app1.mako' in your inherits or include or renderer arguments. Given this, it's obvious that you can inherit from your base.mako in your core app, whereas managing the mako.directories option is more difficult.

If you're deadset on mako.directories then you can add a line to it every time you add a package to pyramid.includes.

mako.directores = 
    App_Core:templates
    workflow_app1:templates
    workflow_app2:templates

Another option is to switch to jinja2, as its plugin has the ability to add search paths after the fact. Thus your included modules can config.add_jinja2_search_path(...) throwing themselves into the lookup order. Pyramid's mako integration does not offer this option right now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top