-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2375 add jinja2 render functionality
- Loading branch information
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
import re | ||
|
||
from pylons import config | ||
|
||
_template_info_cache = {} | ||
|
||
def find_template(template_name): | ||
''' looks through the possible template paths to find a template | ||
returns the full path is it exists. ''' | ||
template_paths = config['pylons.app_globals'].template_paths | ||
for path in template_paths: | ||
if os.path.exists(os.path.join(path, template_name)): | ||
return os.path.join(path, template_name) | ||
|
||
def template_type(template_path): | ||
''' returns best guess for template type | ||
returns 'jinja2', 'genshi', 'genshi-text' ''' | ||
if template_path.endswith('.txt'): | ||
return 'genshi-text' | ||
f = open(template_path, 'r') | ||
source = f.read() | ||
if re.search('\{\[|\%', source): | ||
return 'jinja2' | ||
return 'genshi' | ||
|
||
def template_info(template_name): | ||
''' Returns the path and type for a template ''' | ||
|
||
if template_name in _template_info_cache: | ||
t_data = _template_info_cache[template_name] | ||
return t_data['template_type'], t_data['template_type'] | ||
|
||
template_path = find_template(template_name) | ||
t_type = template_type(template_path) | ||
|
||
t_data = {'template_path' : template_path, | ||
'template_type' : t_type,} | ||
_template_info_cache[template_name] = t_data | ||
return template_path, t_type |