Cheetah with Cherrypy:如何加载基本模板,并在开发过程中根据变化自动执行此操作

发布于 2024-08-27 08:39:12 字数 1165 浏览 14 评论 0原文

我正在开发一个cherrypy+cheetah应用程序,希望改善开发体验。

当我事先手动编译模板时,一切都正常。 (更新:这就是生产环境的工作方式:预编译,不发送 *.tmpl 并将模板作为常规 python 模块加载。)但是,在开发过程中,我宁愿在每次引用模板时加载模板,这样我就不会不需要终止并重新启动我的应用程序。我面临几个问题:

  1. 如果我有从基本模板继承的模板,则会出现导入错误(找不到基本模板)。我认为我在实验期间确实可以使用它,但不幸的是没有保存它,现在我无法让它工作。
  2. 假设我开始工作,如何做到这一点,以便即使在基本模板中进行编辑也能在不重新启动的情况下进行编辑。

下面是我的示例应用程序,它应该演示这些问题。目录结构如下:

t.py
templates/
    base.tmpl
    index.tmpl

t.py:base.tmpl:index.tmpl

import sys

import cherrypy
from Cheetah.Template import Template

class T:
    def __init__(self, foo):
        self.foo = foo

    @cherrypy.expose
    def index(self):
        return Template(file='templates/index.tmpl',
                        searchList=[{'foo': self.foo}]).respond()

cherrypy.quickstart(T(sys.argv[1]))

:

#def body
This is the body from the base
#end def

This is the base doc

#from templates.base import base
#extends base
#def body
$base.body(self)
This is the extended body
#end def

This is from index

这样运行

python t.py Something

I am working on a cherrypy+cheetah app and would like to improve the development experience.

I have everything working when I manually compile templates beforehand. (Update: This is how things work for production: precompile, don't ship *.tmpl and load templates as regular python modules.) However, during development I'd rather just load the templates every time they are referenced so that I don't need to kill and restart my application. I have a couple of issues I am facing:

  1. If I have templates inheriting from base templates, I get import errors (can't find base templates). I think I had this actually working during my experiments, but unfortunately didn't save it and now I can't make it work.
  2. Suppose I get 1. working, how do make it so that edits even in base templates get picked up without restart.

Below is my sample application that should demonstrate the problems. The directory structure is as follows:

t.py
templates/
    base.tmpl
    index.tmpl

t.py:

import sys

import cherrypy
from Cheetah.Template import Template

class T:
    def __init__(self, foo):
        self.foo = foo

    @cherrypy.expose
    def index(self):
        return Template(file='templates/index.tmpl',
                        searchList=[{'foo': self.foo}]).respond()

cherrypy.quickstart(T(sys.argv[1]))

base.tmpl:

#def body
This is the body from the base
#end def

This is the base doc

index.tmpl:

#from templates.base import base
#extends base
#def body
$base.body(self)
This is the extended body
#end def

This is from index

Run it like this:

python t.py Something

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦旅人picnic 2024-09-03 08:39:12

试试这个:

base.tmpl 替换为:

#from Cheetah.Template import Template
#def body
  #set $base = Template(file="templates/base.tmpl") 
  $base.body()
  <br/>
This is the extended body
#end def

$body()
<br/>
This is from index

Try this:

Replace base.tmpl with:

#from Cheetah.Template import Template
#def body
  #set $base = Template(file="templates/base.tmpl") 
  $base.body()
  <br/>
This is the extended body
#end def

$body()
<br/>
This is from index
无所的.畏惧 2024-09-03 08:39:12

看起来这个问题在另一个问题中得到了回答。通过使用 cheetah_import 函数,我可以这样编写我的方法:

@cherrypy.expose
def index(self):
    templates = cheetah_import('templates.index')
    t = getattr(getattr(templates, 'index'), 'index')(searchList=[{'foo': self.foo}])
    return t.respond()

我还需要将 __init__.py 添加到模板目录中。此方法还要求将 Cherrypy 的 engine.autoreload_on 设置设为 True

为了提高生产效率,我可以使用 cheetah_import = __import__ 并且不替换默认的 __builtin__.import

Looks like this question was kind of answered in another SO question. By using the cheetah_import function, I can write my methods like this:

@cherrypy.expose
def index(self):
    templates = cheetah_import('templates.index')
    t = getattr(getattr(templates, 'index'), 'index')(searchList=[{'foo': self.foo}])
    return t.respond()

I will also need to add an __init__.py into the templates directory. This approach also requires that Cherrypy's engine.autoreload_on setting is set to True.

To make production more efficient, I can make cheetah_import = __import__ and not replace the default __builtin__.import.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文