生成器

如果表(DocType)中的每条记录都对应一个网页,您可以设置生成器。

要设置生成器,您必须:

  1. 添加一个字段 route 来指定页面的路由
  2. 添加一个条件字段,用于指示页面是否可见。
  3. 在您应用的 hooks.py 的 website_generators 中添加该 DocType 名称。
  4. 从 frappe.website.website_generator.WebsiteGenerator 继承控制器(Controller)
  5. 为您的页面创建一个模板
  6. 为模板添加自定义属性(上下文)
  7. 自定义路由和列表视图

让我们通过一个示例来说明:

示例

1. 添加字段

我们在 DocType 中添加了 published、route 字段

注意: 字段 route 是必填项

2. 将网站生成器添加到 Hooks

由于 Job Opening 位于 erpnext 中,我们已将其添加到现有的生成器 hooks 列表中:

website_generators = ["Item Group", "Item", "Sales Partner", "Job Opening"]

如果您的 hooks.py 中不存在 website_generators 属性,请添加它!

3. 控制器

我们在 job_opening.py 的 JobOpening 类中添加了 website 属性

在 get_context 中,parents 属性将用于指示面包屑导航

from frappe.website.website_generator import WebsiteGenerator
from frappe import _

# subclass from WebsiteGenerator, not Document
class JobOpening(WebsiteGenerator):
    website = frappe._dict(
        template = "templates/generators/job_opening.html",
        condition_field = "published",
        page_title_field = "job_title",
    )

    def get_context(self, context):
        # show breadcrumbs
        context.parents = [{'name': 'jobs', 'title': _('All Jobs') }]

注意: 完成此操作后,您应该会在文档表单上看到“在网站上查看”链接。

4. 添加模板

在 erpnext/templates/generators/job_opening.html 中添加模板

{% raw %}{% extends "templates/web.html" %}

{% block breadcrumbs %}
    {% include "templates/includes/breadcrumbs.html" %}
{% endblock %}

{% block header %}

<h1>{{ job_title }}</h1>
{% endblock %}

{% block page_content %}

<div>{{ description }}</div>

<a class="btn btn-primary" href="/job_application?job_title=%7B%7B%20doc.job_title%20%7D%7D">
    {{ _("Apply Now") }}</a>

{% endblock %}
{% endraw %}

5. 自定义列表视图

如果您在控制器文件(job_opening.py)中添加一个 get_list_view 方法,您可以为列表视图设置属性

def get_list_context(context):
    context.title = _("Jobs")
    context.introduction = _('Current Job Openings')