TurboGears – Flash 消息

TurboGears 提供了一种非常方便的消息系统,可以以非侵入方式向用户通知信息。tg 模块中的 TGFlash 类支持存储在普通 cookie 中的 Flash 消息。此类支持通过 JavaScript 在服务器端和客户端获取 Flash 消息。

TGFlash 类的 render() 方法在 Python 本身中使用时,可以从模板调用以呈现 Flash 消息。如果在 JavaScript 上使用,它会提供一个 WebFlash 对象。它公开 payload()render() 方法来获取当前 Flash 消息并从 JavaScript 呈现它。

使用"quickstart"创建 TurboGears 项目时,它有一个 Master.html 模板。它包含该 Flash 对象变量的定义。从控制器收到的此 Flash 消息的内容将替换此模板中标记的占位符。

<py:with vars = "flash = tg.flash_obj.render('flash', use_js = False)">
    <div py:if = "flash" py:replace = "Markup(flash)" />
</py:with>

tg.flash_obj 是 WebFlash 对象,可通过包含 master.html 模板在任何呈现的模板中使用。此对象允许检索当前的 Flash 消息并显示它。

使用 tg.flash() 方法将 Flash 消息存储在 cookie(其默认名称为 webflash)中。然后将消息和状态参数传递给它。

tg.flash('Message', 'status')

如果调用 flash 的方法执行重定向,则 flash 将在重定向页面内可见。如果该方法直接公开模板,则 flash 将在模板本身内可见。

可以通过将 CSS 样式应用于状态代码来定制 flash 消息的外观。"快速启动"项目包含由样式表 public/css/style.css 定制的错误、警告、信息和 ok 状态代码。还可以添加更多带有样式的状态代码。

#flash > .warning {
   color: #c09853;
   background-color: #fcf8e3;
   border-color: #fbeed5;
}

#flash > .ok {
   color: #468847;
   background-color: #dff0d8;
   border-color: #d6e9c6;
}

#flash > .error {
   color: #b94a48;
   background-color: #f2dede;
   border-color: #eed3d7;
}

#flash > .info {
   color: #3a87ad;
   background-color: #d9edf7;
   border-color: #bce8f1;
}

此外部样式表需要包含在模板中 −

<link rel = "stylesheet" type = "text/css" media = "screen"
    href = "${tg.url('/css/style.css')}" />

任何 Flash 消息支持的配置都可以通过设置 TGFlash 对象的 configure() 方法或在 app_cfg.py(在 config 文件夹中)中的参数来实现。可配置的参数是 −

Sr.No. 参数和说明
1

flash.cookie_name

用于存储 Flash 消息的 cookie 的名称。默认为 webflash

2

flash.default_status

如果未指定,则为默认消息状态(默认为 ok)

3

flash.template

渲染时用作 flash 模板

4

flash.allow_html

打开/关闭 Flash 消息中的转义,默认情况下不允许使用 HTML。

5

flash.js_call

从 JavaScript 显示 Flash 时将运行的 JavaScript 代码。默认为 webflash.render()

6

flash.js_template

string.Template 实例用于替换 Flash 消息的完整 JavaScript 支持。

  • pop_payload() − 函数获取当前 Flash 消息、状态和相关信息。获取 Flash 消息将删除 cookie。

  • render(container_id, use_js=True) −在模板内呈现 flash 消息或为其提供 Javascript 支持。

  • container_id 是显示消息的 DIV,而 use_js 则在将 flash 呈现为 HTML 或 JavaScript 使用之间切换。

  • status − 仅获取当前 flash 状态,获取 flash 状态将删除 cookie。

  • message − 仅获取当前 flash 消息,获取 flash 消息将删除 cookie。

如何制作简单的 Flash 消息?

在下面的示例中,根控制器类中提供了一个 flash() 方法。它调用一个 flash() 消息,该消息被呈现到公开的模板 flash.html

from hello.lib.base import BaseController
from tg import expose, flash, redirect, request

class RootController(BaseController):
   @expose('hello.templates.flash')
   def flash(self, user = None):
      
      if user:
         flash(message = "Welcome "+user,status = "ok")
      else:
         flash(message = "Welcome Guest",status = "info")
      return {}

templates文件夹下制作flash.html的代码如下

<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:py = "http://genshi.edgewall.org/"
   xmlns:xi = "http://www.w3.org/2001/XInclude">

   <head>
      <title>TurboGears 2.3: Flash messages>/title>
      <link rel = "stylesheet" type = "text/css" media = "screen"
         href = "${tg.url('/css/style.css')}" />
			
      <py:with vars = "flash = tg.flash_obj.render('flash', use_js = False)">
         <div py:if = "flash" py:replace = "Markup(flash)" />
      </py:with>
		
   </head>

   <body>
      <h2>Hello TurboGears</h2>
   </body>
	
</html>

启动服务器并在浏览器中输入 http://localhost:8080/flash?user=MVL

Flash Message

将 URL 更改为 http://localhost:8080/flash 并查看根据 style.css 中的定义以不同格式显示的 Flash 消息

Message