It's not very elegant to write a program for each page to be displayed and write HTML code in it. So, I would like to make it until the template is read and displayed.
When creating a framework, I want to divide it into class files for each function. At that time, when creating and operating the class autoload, set it to some extent in the configuration file. There is also a method of writing in an ini file, but since I want to operate the created project at the deployment destination without editing, specify the project root directory and specify the location of the setting file based on it. So, first, describe the implementation for specifying the project root directory.
The directory structure this time is as follows. +PROJECR_ROOT ++app | +app | + templates (template file storage location) | + index.tpl (template file) ++public | + index.py (program body) | +uwsgi.ini | +uwsgi.pid |+uwsgi.log +templates + index.tpl (template file) You can get the directory one level above the public directory, that is, the project root directory by writing as follows.
index.py (excerpt)
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
logger.info('project_root:' + project_root)
Now you can get the route directory of your project.
Now that the project root directory has been identified, we will specify the location of the configuration file and template file from this point. The full path of the file will be decided later, including the name of the controller, but for the time being, PROJEC_ROOT / app / templates / index.tpl is read. In the Framework created this time, the loaded template is saved in an array, read line by line, interpreted and displayed, so in order to improve the visibility of the program source, the View creation method and the file reading method are separated. The file read method reads the file and returns the result as a list type to the View creation method, the View creation method concatenates it to an array string and returns it to the application method, and returns View from application to uwsgi. The code is below.
index.py
# index.py
# coding:utf-8
import logging
import datetime
import uuid
import os, sys
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def main():
# log
logger.info('test-log-dayo')
def application(env, start_response):
main()
logger.info('project_root:' + project_root)
start_response('200 OK', [('Content-type', 'text/html')])
str = createView('index.tpl')
return [str.encode("utf-8")]
def createView(file_name):
arr = readTemplate(project_root + '/app/templates/' + file_name)
str = ""
for line in arr:
str = str + line
return str
def readTemplate(file_name):
f = open(file_name, 'r')
arr = []
for line in f:
arr.append(line)
f.close()
return arr
With this, if the contents of the template are displayed like "Hello World !!!" on the browser, it is successful.
Recommended Posts