【问题标题】:bottle templating瓶子模板
【发布时间】:2015-06-16 17:59:30
【问题描述】:

我最近接触到了 Bottlepy,这几乎是我第一次使用模板引擎。(在此之前我会简单地用传统 PHP 制作我需要的东西)

我的问题是,假设我有一个基本布局(一个简单的 HTML/CSS 布局),例如;侧面的登录框。如何使登录框保持动态更新,而不必在每条路由上都向其传递值?

以此为例:

from bottle import route,template

@route('/')
def main():

    return template("content.tpl") #rebase from layout.tpl

layout.tpl:

<html>
    <head>
        <title>{{title}}</title>
    </head>
    <body>
        %include
        <div id='sidebar'><!-- login box --></div>
    </body>
</html>

我想我的主要问题是如何确保登录框运行而不必在每个页面上为其插入变量

【问题讨论】:

    标签: python bottle


    【解决方案1】:

    要使变量在所有模板中都可用,请在您的路由之前添加如下内容:

    from bottle import BaseTemplate
    
    BaseTemplate.defaults['symbol_name'] = value
    

    我用它来为我的模板添加辅助函数。

    更正确的解决方案可能是编写一个插件,这并不难,并且在 Bottle 文档中有所介绍。

    Source

    【讨论】:

      【解决方案2】:

      我不熟悉瓶子,但是对于其他模板引擎来说,这种事情是通过继承来完成的。

      例如,

      假设您有一个名为 content.tpl 的基本模板,看起来与您的 layout.tpl 类似:

      <html>
          <head>
              <title>{{title}}</title>
          </head>
          <body>
              <div id='sidebar'>%% section "sidebar" %%</div>
              <div id='content'>%% section "content" %%</div>
          </body>
      </html>
      

      请注意,这是伪代码 - %% ... %% 标记的内容是虚构的,与任何特定的模板引擎 (AFAIK) 不对应。

      对于您的登录页面,您将有另一个名为 login.tpl 的模板文件:

      %% extends "content.tpl" %%
      
      %% start section "sidebar" %%
          <!-- whatever you put here will be rendered -->
          <!-- in the "sidebar" section of the parent -->
      %% end section "sidebar" %%
      
      %% start section "content" %%
          <!-- whatever you put here will be rendered -->
          <!-- in the "content" section of the parent -->
      
          <form> ... </form>
      %% end section "content" %%
      

      然后,在登录页面的路径中,您将执行以下操作:

      `return template("login.tpl")`
      

      这将导致模板引擎加载login.tpl,填写它的值,然后加载content.tpl,并将它为“侧边栏”和“内容”部分放在一起的东西粘贴到适当的位置.

      还有另一种机制可以从多个模板组成页面:包括一个文件。这将类似于上面的示例,但您的登录页面将如下所示:

      <html>
          <head>
              <title>{{title}}</title>
          </head>
          <body>
              <div id='sidebar'>%% include "sidebar.tpl" %%</div>
              <div id='content'>%% include "content.tpl" %%</div>
          </body>
      </html>
      

      它们之间的主要区别是:

      1. 当您继承时,您不必在每个页面上重复所有内容 - 所有页面的不变内容可以从父布局继承。

      2. 在继承时,您还可以将多个模板中的内容合并为一个。例如,假设您有像“main.tpl”->“template1.tpl”->“template2.tpl”这样继承的模板。在某些模板引擎中可以设置它,以便每个模板都可以添加到一个部分,以便内容可以是整个模板系列的组合。

      当然,也可以混合使用这些技术。

      【讨论】:

      • 我的意思是,假设您正在调用这样的模板:template("some_page.tpl",title="something"),即使在继承时也无法将值传递给登录名盒子。 (例如 cookie 信息)。那是除非我在这里遗漏了什么
      • 在我使用过的模板引擎中(主要是 PHP:Smarty、Blade 和其他一些简单的),将值传递给template() 方法使其在所有模板中都可用:继承或包含.
      • 是的,但假设我正在制作这个页面:@route('/testpage') def testpage(): return template('testpage.tpl',title="test"),当 testpage.tpl 依赖于基本布局时,我如何也将值传递给登录框而不重复它?
      • 啊,我明白了问题所在。在 Laravel(PHP 框架)中,我使用“视图编写器”完成了此操作,该代码是每次呈现视图时执行的代码 - 我使用它来插入每个页面所需的一些值。我不知道瓶子是否具有相同的功能。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多