Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to use subdirectories for the views #1146

Open
parg-programador opened this issue Jun 8, 2019 · 1 comment
Open

Method to use subdirectories for the views #1146

parg-programador opened this issue Jun 8, 2019 · 1 comment

Comments

@parg-programador
Copy link

parg-programador commented Jun 8, 2019

I created um method in my application for improve the organization of my views, I don't know if the code it's interesting for be used as internal resource of bottle library.

import bottle
from os import listdir
from os.path import dirname, realpath, join, isdir

def config_views_directory(all_subdirs=False):
    """ config the path of views """
    abs_app_dir_path = dirname(realpath(__file__))
    abs_views_path = join(abs_app_dir_path, 'views')
    index = 0

    # insert the path of views
    bottle.TEMPLATE_PATH.insert(index, abs_views_path)

    # verify the directory
    def process_directory(index, path):
        """ process the directory """
        for file_name in listdir(path):
            next_path = join(path, file_name)

            # verify the directory
            if isdir(next_path):
                index += 1

                # insert the path of views
                bottle.TEMPLATE_PATH.insert(index, next_path)

                # checks to process all subdirectories
                if all_subdirs:
                    index = process_directory(index, next_path)
        
        return index

    process_directory(index, abs_views_path)
@parg-programador parg-programador changed the title Method to use subdirectories for views Method to use subdirectories for the views Jun 8, 2019
@parg-programador
Copy link
Author

One thing that I forgot comment it's that when I create new views files I take care to not create files with same names, even if they are in different subdirectories.

views
|----backend
----|----login.tpl
|----fronend
----|----front_login.tpl

@view('login')
def backend_login():
    pass

@view('front_login')
def frontend_login():
    pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant