首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我们如何预编译猎豹中的基本模板,使#include、#extends和#import在Weby中正常工作

我们如何预编译猎豹中的基本模板,使#include、#extends和#import在Weby中正常工作
EN

Stack Overflow用户
提问于 2009-05-28 07:02:48
回答 2查看 1.3K关注 0票数 2

你是如何在 production中为猎豹服务的

伙计们,你能分享一下如何在生产中预编译和服务猎豹吗?

因为我们没有在webpy中编译模板,所以它正在获取上游超时错误。如果你能分享一个好的最佳实践,那会有帮助的。

*

杰里米写道:对于一个生产网站,我使用猎豹和预编译模板-它非常快(模板导入特别快时,python编译和优化)。对于imp模块来说,有一点神奇之处在于它需要一个模板名和一个基本目录(在站点特定的配置中配置),并加载该模板,负责#扩展和

酌情导入指令。我不使用内置的支持

不过,猎豹。导入新模板库也只是为了显示调试器错误页。

*

EN

回答 2

Stack Overflow用户

发布于 2009-06-01 06:18:12

可以根据需要自动编译:

代码语言:javascript
复制
import sys
import os
from os import path
import logging
from Cheetah.Template import Template
from Cheetah.Compiler import Compiler

log = logging.getLogger(__name__)

_import_save = __import__
def cheetah_import(name, *args, **kw):
  """Import function which search for Cheetah templates.

  When template ``*.tmpl`` is found in ``sys.path`` matching module
  name (and corresponding generated Python module is outdated or
  not existent) it will be compiled prior to actual import.
  """
  name_parts = name.split('.')
  for p in sys.path:
    basename = path.join(p, *name_parts)
    tmpl_path = basename+'.tmpl'
    py_path = basename+'.py'
    if path.exists(tmpl_path):
      log.debug("%s found in %r", name, tmpl_path)
      if not path.exists(py_path) or newer(tmpl_path, py_path):
        log.info("cheetah compile %r -> %r", tmpl_path, py_path)
        output = Compiler(
            file=tmpl_path,
            moduleName=name,
            mainClassName=name_parts[-1],
            )
        open(py_path, 'wb').write(str(output))
      break
  return _import_save(name, *args, **kw)

def newer(new, old):
    """Whether file with path ``new`` is newer then at ``old``."""
    return os.stat(new).st_mtime > os.stat(old).st_mtime

import __builtin__
__builtin__.__import__ = cheetah_import
票数 1
EN

Stack Overflow用户

发布于 2009-06-09 06:00:17

这工作

代码语言:javascript
复制
try:web.render('mafbase.tmpl', None, True, 'mafbase')
except:pass

,这就是我对你做的代码

代码语言:javascript
复制
from cheetahimport import *
sys.path.append('./templates')
cheetah_import('mafbase')

包括在给定方法中不工作。

,这是我得到的错误

代码语言:javascript
复制
    localhost pop]$ vi code.py
    [mark@localhost pop]$ ./code.py 9911
    http://0.0.0.0:9911/
    Traceback (most recent call last):
     File "/home/mark/work/common/web/application.py", line 241, in process
     return self.handle()
    File "/home/mark/work/common/web/application.py", line 232, in handle
    return self._delegate(fn, self.fvars, args)
    File "/home/mark/work/common/web/application.py", line 411, in _delegate
    return handle_class(cls)
    File "/home/mark/work/common/web/application.py", line 386, in handle_class
    return tocall(*args)
    File "user.py", line 264, in proxyfunc
    return func(self, *args, **kw)
    File "/home/mark/work/pop/code.py", line 1801, in GET
    return web.render('subclass.html')
    File "/home/mark/work/common/web/cheetah.py", line 104, in render
    return str(compiled_tmpl)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 982, in __str__
    def __str__(self): return getattr(self, mainMethName)()
    File "templates/mafbase.py", line 713, in respond
    self._handleCheetahInclude("widgetbox.html", trans=trans, includeFrom="file", raw=False)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 1512, in _handleCheetahInclude
    nestedTemplateClass = compiler.compile(source=source,file=file)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 693, in compile
     fileHash = str(hash(file))+str(os.path.getmtime(file))
   File "/usr/lib/python2.5/posixpath.py", line 143, in getmtime
    return os.stat(filename).st_mtime
   OSError: [Errno 2] No such file or directory: '/home/mark/work/pop/widgetbox.html'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/919539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档