【问题标题】:Cheetah template engine calling python base function猎豹模板引擎调用python基函数
【发布时间】:2011-05-10 12:48:23
【问题描述】:

我正在使用 Cheetah 模板和 Cherrypy,下面是我的主要 python 文件

Main.py:
def multiple(a,b):
    return a*b

def index(self):
    t = Template('template.tmpl')
    #blah implementation here

在我的模板文件中,我希望实现

<body>
    <div>
       $multiple(2,3)
    </div>
</body>

有人知道我怎样才能得到这个工具吗?非常感谢。

问候, 安迪。

【问题讨论】:

    标签: python cherrypy cheetah


    【解决方案1】:
    t = Template("template.tmpl")
    t.multiple = multiple
    

    这应该可以解决问题。

    【讨论】:

    • 是的,这个有效。我之前尝试过,但我没有在全局范围内声明它或使用 self.multiple。
    【解决方案2】:

    尝试使用 searchList 参数:

    def index(self):
        t = Template('template.tmpl', searchList=[multiple])
    

    它允许您定义可以在模板定义中使用的“占位符”。

    【讨论】:

      【解决方案3】:

      这可能会回答:

      import Cheetah
      import Cheetah.Template
      
      
      def multiple(a,b):
          return a*b
      
      print Cheetah.Template.Template(file='template.tmpl',
                                      searchList=[dict(multiple=multiple)])
      

      【讨论】:

        【解决方案4】:

        为什么不直接在模板中导入 Main?

        #from Main import multiple
        <body>
            <div>
               $multiple(2,3)
            </div>
        </body>
        

        【讨论】:

          猜你喜欢
          • 2012-01-12
          • 2011-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-11
          • 2012-03-06
          • 2012-01-10
          • 1970-01-01
          相关资源
          最近更新 更多