简体   繁体   English

瓶子模板

[英]bottle templating

I recently got into bottlepy and this is pretty much my first experience with a template engine.(Prior I would simply make what I needed in traditional PHP) 我最近遇到了瓶装问题,这几乎是我第一次使用模板引擎。(之前我只是简单地制作我需要的传统PHP)

My question is this, let's say I have a base layout(A simple HTML/CSS layout), and, for example; 我的问题是这个,假设我有一个基本布局(一个简单的HTML / CSS布局),例如; a login box on the side. 一边是登录框。 How do I keep the login box dynamically up to date without having to pass values to it on every route? 如何使登录框动态更新,而不必在每条路径上传递值?

Take this for example: 以此为例:

from bottle import route,template

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

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

layout.tpl: layout.tpl:

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

I guess my main question here is how do I make sure the login box runs without having to insert the variables for it at every single page 我想我的主要问题是如何确保登录框运行而不必在每个页面插入变量

To make a variable available in all templates, put something like this before your routes: 要使变量在所有模板中可用,请在路径之前输入类似的内容:

from bottle import BaseTemplate

BaseTemplate.defaults['symbol_name'] = value

I use this to add helper functions for my templates. 我用它来为我的模板添加辅助函数。

A more correct solution may involve writing a plugin, which is not terribly difficult and is covered in the Bottle documentation. 更正确的解决方案可能涉及编写插件,这不是非常困难,并且在Bottle文档中有所介绍。

Source 资源

I'm not familiar with bottle, but for other templating engine this sort of thing is done via inheritance. 我不熟悉瓶子,但对于其他模板引擎,这种事情是通过继承来完成的。

For example, 例如,

Imagine you have a base template called content.tpl that looks something like what you have for your layout.tpl : 想象一下,你有一个名为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>

Note that this is pseudocode - the stuff marked out by %% ... %% is made up & doesn't correspond to any particular templating engine (AFAIK). 请注意,这是伪代码 - 由%% ... %%标记的内容组成并且不对应于任何特定的模板引擎(AFAIK)。

For your login page, you would have another template file called login.tpl : 对于您的登录页面,您将拥有另一个名为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" %%

Then, in your route for your login page, you would do something like this: 然后,在您的登录页面的路线中,您将执行以下操作:

`return template("login.tpl")`

This will cause the template engine to load login.tpl , fill in the values it has, and then load the content.tpl and stick the stuff it put together for the "sidebar" and "content" sections into the appropriate spots. 这将导致模板引擎加载login.tpl ,填写它拥有的值,然后加载content.tpl并将它放在一起的东西粘贴到“侧边栏”和“内容”部分到适当的位置。

There is another mechanism for composing a page from multiple templates: including a file. 还有另一种机制可以从多个模板组成页面:包括文件。 This would be similar to the example above, exception your login page would look something like this: 这与上面的示例类似,例外,您的登录页面看起来像这样:

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

The main differences between them are: 它们之间的主要区别是:

  1. when you're inheriting, you don't have repeat everything on every page - stuff that's constant for all pages can be inherited from the parent layout. 当你继承时,你没有在每个页面上重复所有内容 - 所有页面的常量都可以从父布局继承。

  2. when you're inheriting, you can also include stuff from multiple templates into one. 当您继承时,您还可以将多个模板中的内容合并为一个。 For example, imagine that you have templates that inherit like this "main.tpl" -> "template1.tpl" -> "template2.tpl". 例如,假设您有继承像“main.tpl” - >“template1.tpl” - >“template2.tpl”的模板。 It is possible in some template engines to set it up so that each template can add to a section, so that the contents can be a composite of the whole family of templates. 在某些模板引擎中可以对其进行设置,以便每个模板都可以添加到一个部分,以便内容可以是整个模板系列的组合。

Of course, it is possible to use a mix of these techniques as well. 当然,也可以使用这些技术的混合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM