页面

在本章中,您将学习如何声明静态页面。

默认页面

在 Odoo 中,网站附带一些默认的静态页面(首页、联系我们、404 等)。它们的构建方式如下。

/website/data/website_data.xml
<template id="website.homepage" name="Home">
   <t t-call="website.layout"
      pageName.f="homepage">
      <div id="wrap" class="oe_structure oe_empty" />
   </t>
</template>

每个默认页面是一个模板,其内容保存在一个记录中。这就是为什么: 自定义页面是在记录内创建的 。

<t-call='website.layout'> 有一些可以设置的变量:

定义元标题。

<t t-call="website.layout"
   additional_title.translate="My Page Title">
   <div id="wrap" class="oe_structure oe_empty" />
</t>

小技巧

由于 t-call 接受参数,这些参数可以作为变量、字符串(*.f)或可翻译字符串(*.translate)传递。

定义元描述。

<t t-call="website.layout"
   meta_description.translate="This is the description of the page that will appear on Search
   Engines.">
   <div id="wrap" class="oe_structure oe_empty" />
</t>

为页面添加一个 CSS 类。

<t t-call="website.layout"
   pageName.f="homepage">
   <div id="wrap" class="oe_structure oe_empty" />
</t>

如有需要,停用默认页面。

/website_airproof/data/pages/home.xml
<record id="website.homepage" model="ir.ui.view">
    <field name="active" eval="False" />
</record>
/website_airproof/data/pages/contactus.xml
<record id="website.contactus" model="ir.ui.view">
    <field name="active" eval="False" />
</record>

或者,使用 XPath 替换这些页面的默认内容。

/website_airproof/data/pages/404.xml
<template id="404" inherit_id="http_routing.404">
   <!-- Change the meta title parameter through the t-call -->
   <xpath position="attributes" expr="//t[@t-call='web.frontend_layout">
      <attribute name="additional_title.translate">404 - Not found</attribute>
   </xpath>
   <!-- Replace the default content -->
   <xpath expr="//*[@id='wrap']" position="replace">
      <div id="wrap" class="oe_structure">
         <!-- Content -->
      </div>
   </xpath>
</template>

另请参阅

主题页面

您可以向网站添加任意数量的页面。与其定义 <template>,不如创建一个页面记录。

声明

/website_airproof/data/pages/about_us.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
   <record id="page_about_us" model="website.page">
      <field name="name">About us</field>
      <field name="is_published" eval="True" />
      <field name="key">website_airproof.page_about_us</field>
      <field name="url">/about-us</field>
      <field name="website_id" eval="1" />
      <field name="type">qweb</field>
      <field name="arch" type="xml">
         <t t-name="website_airproof.page_about_us">
            <t t-call="website.layout">
               <div id="wrap" class="oe_structure">
                  <!-- Content -->
               </div>
            </t>
         </t>
      </field>
   </record>
</odoo>

小技巧

要将该页面设置为首页,请设置以下字段:

<field name="is_homepage" eval="True" />

小技巧

页眉和页脚默认可见,但可以通过将 header_visible 和/或 footer_visible 字段设置为 false 在特定页面上隐藏它们。这样它们在视觉上被隐藏,但仍然存在于 DOM 中。两者都可以通过网站构建器再次显示,就像 可见性条件 下隐藏的任何其他隐藏元素一样。

<field name="header_visible" eval="False" />
<field name="footer_visible" eval="False" />

可以通过启用此视图来在所有页面上隐藏页眉(另请参阅:预设):

/website_airproof/data/presets.xml
<record id="website.option_layout_hide_header" model="ir.ui.view">
   <field name="active" eval="True" />
</record>

多网站和 website_id

在模块上下文中,默认情况下,上述记录在数据库中的每个可用网站上都是可用的。最好指定页面可被找到的网站的 website_id 。

字段

描述

名称

页面名称(人类可读)。

是否已发布

定义页面是否已发布(对访问者可见)。

键

视图键(必须唯一)

URL

页面可访问的相对路径。

类型

视图类型

架构

视图架构(页面的标记)

通过 <t t-call="website.layout"> ,您可以使用 Odoo 默认页面布局与您的代码结合。

noupdate 属性

此属性防止数据被覆盖。它可以添加到包装某些记录的 data 标签上以保护这些记录,或者添加到 odoo 标签上以保护文件中声明的所有记录。

保护文件中的所有记录:

<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
   <record id="menu_company" model="website.menu">
      <!-- Fields -->
   </record>
   <record id="menu_faq" model="website.menu">
      <!-- Fields -->
   </record>
</odoo>

保护文件中的特定记录:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
   <record id="menu_company" model="website.menu">
      <!-- Fields -->
   </record>

   <data noupdate="1">
      <record id="menu_faq" model="website.menu">
         <!-- Fields -->
      </record>
      <record id="menu_legal" model="website.menu">
         <!-- Fields -->
      </record>
   </data>
</odoo>

用例

模块中创建了多个静态页面。该模块已安装到数据库中,并且最终用户更新了其中一些页面。需要在静态页面上应用一些错误修复,同时避免丢失最终用户的更改。

问题

如果在数据库中更新模块,则模块中声明的每个记录都会覆盖数据库中现有的记录,即使最终用户已经更改了其中一些记录。

解决方案

通过将记录(或文件中声明的所有记录)包装在 <data noupdate="1"></data> 标签中,声明的记录会在模块首次安装时创建,但在模块更新后不会被更新。

系统检测到该记录不存在并将重新创建它。

当然不是。从技术上讲,它可用于每种类型的记录。

页眉覆盖

使页眉背景透明并位于页面内容之上。

<field name="header_overlay" eval="True"/>
页眉覆盖

注解

要创建静态页面的内容,请使用 Odoo 的方式进行操作,以确保可以通过网站构建器进行编辑。请注意,Odoo 利用了 Bootstrap 框架(5.3)。

查找可用的类和组件:

页面模板

创建可以从“新建页面”对话框窗口中使用的预设静态页面模板。

声明

页面模板必须通过 new_page_templates 在模块的 __manifest__.py 中定义,并通过 new_page_template_templates.xml 文件定义:

/website_airproof/__manifest__.py
{
   'name': 'Airproof Theme',
   'description': '...',
   'category': 'Website/Theme',
   'version': '20.0.0.0',
   'author': '...',
   'license': '...',
   'depends': ['website'],
   'data': [
      # ...
      'views/new_page_template_templates.xml'
   ],
   'assets': {
      # ...
   },
   'new_page_templates': {
      'airproof': {
         'faq': ['s_airproof_text_block_h1', 's_title', 's_faq_collapse', 's_call_to_action']
   }
}

模板

然后,您需要根据 __manifest__.py 中的层次结构使用特定的命名约定创建模板。在此情况下,名称为 new_page_template_sections_airproof_faq 。此模板中调用的构建块与标准构建块完全相同,只是第一个块是“即时”调整的。

创建标准 s_text_block 的一个新实例( primary 属性很重要),并进行一些调整:

/website_airproof/views/new_page_template_templates.xml
<template id="s_airproof_text_block_h1" inherit_id="website.s_text_block" primary="True">
   <xpath expr="//div[hasclass('container')]|//div[hasclass('o_container_small')]" position="replace">
      <div class="container s_allow_columns">
            <h1 class="display-1">FAQ - Help</h1>
      </div>
   </xpath>
</template>

为页面模板实例化每个构建块(无论是否修改):

/website_airproof/views/new_page_template_templates.xml
<template id="new_page_template_airproof_faq_s_text_block_h1" inherit_id="website_airproof.s_airproof_text_block_h1" primary="True" />
<template id="new_page_template_airproof_faq_s_title" inherit_id="website.s_title" primary="True" />
<template id="new_page_template_airproof_faq_s_faq_collapse" inherit_id="website.s_faq_collapse" primary="True" />
<template id="new_page_template_airproof_faq_s_call_to_action" inherit_id="website.s_call_to_action" primary="True" />

然后,如上所述,在 #wrap 内使用一些 t-snippet-call 创建您的页面模板:

/website_airproof/views/new_page_template_templates.xml
<template id="new_page_template_sections_airproof_faq" name="Airproof - New Page Template FAQ">
   <div id="wrap">
      <t t-snippet-call="website_airproof.new_page_template_airproof_faq_s_text_block_h1" />
      <t t-snippet-call="website_airproof.new_page_template_airproof_faq_s_title" />
      <t t-snippet-call="website_airproof.new_page_template_airproof_faq_s_faq_collapse" />
      <t t-snippet-call="website_airproof.new_page_template_airproof_faq_s_call_to_action" />
   </div>
</template>

创建页面模板后,创建一个自定义组并将其添加到现有组中。以下是现有组的列表:

/website/views/new_page_template_templates.xml
<template id="new_page_template_groups">
   <div id="about_us">About Us</div>
   <div id="services">Services</div>
   <div id="pricing">Pricing</div>
   <div id="portfolio">Portfolio</div>
   <div id="contact_us">Contact Us</div>
   <div id="utility">Utility</div>
</template>

请随时向列表中添加自定义组:

/website_airproof/views/new_page_template_templates.xml
<template id="new_page_template_groups" inherit_id="website.new_page_template_groups" name="Airproof - New Page Template Groups">
   <xpath expr="//div[@id='custom']" position="after">
      <div id="airproof">Airproof</div>
   </xpath>
</template>
现有静态页面模板列表