【OpenWrt】(Luci) CBI

Overview

Sergey Ponomarev edited this page on 9 Dec 2018 · 6 revisions



Writing LuCI CBI models

CBI models are Lua files describing the structure of an UCI config file and the resulting HTML form to be evaluated by the CBI parser.

All CBI model files must return an object of type luci.cbi.Map.

For a commented example of a CBI model, see the Writing Modules tutorial.

The scope of a CBI model file is automatically extended by the contents of the module luci.cbi and the translate function from luci.i18n.

This Reference covers the basics of the CBI system.

CBI(Configuration Binding Interface) 模型是描述了一个 UCI1 配置文件的结构和被 CBI 解析器计算(evaluated)的 HTML 表单(form)的 Lua2 文件。
所有的 CBI 模型文件 MUSTluci.cbi.Map 类型的实例 return
一份关于 CBI 模型的例子参见 Writing Modules tutorial

CBI 模型文件的作用范围是被 luci.cbi 模块的内容和来自 luci.i18ntranslate 功能自动拓展的。

“Reference” 包括了“the basics” of the CBI system。

扫描二维码关注公众号,回复: 9799481 查看本文章


class Map (config, title, description)

这(Map)是模型的根对象。

  • config: 映射的配置文件,查看 UCI documentation/etc/config 目录下的文件。
  • title: 在 UI 上显示的标题。
  • description: 在 UI 上显示的(本页面)描述

function :section (sectionclass, …)

创建一个新的 section

  • sectionclass: 一个这个 section 的类。
  • additional parameters passed to the constructor of the section class

class NamedSection (name, type, title, description)

一个对象描述了一个通过 name 选中的 UCI section。
通过 Map:section(NamedSection, "name", "type", "title", "description") 实例化。

  • name: UCI section 的 name;
  • type: UCI section 的 type;
  • title: 在 UI 上显示的 title;
  • description: 在 UI 上显示的 description。
function :option(optionclass, …)

创建一个新的 option

  • optionclass: 这个 section 的一个类
  • additional parameters passed to the constructor of the option class
property .addremove = false

允许用户删除和重新创建(recreate)该配置(configuration)单元(section)

property .dynamic = false

Marks this section as dynamic. Dynamic sections can contain an undefinded number of completely userdefined options.

将这个单元标记为 dynamic。动态单元可以包含一个 完全的用户定义(userdefined)选项的 undefinded number。

property .optional = true

Parse optional options

分析可选的选项。


class TypedSection (type, title, description)

An object describing a group of UCI sections selected by their type.

To instantiate use: Map:section(TypedSection, "type", "title", "description")

  • type: UCI section type
  • title: The title shown in the UI
  • description: description shown in the UI
function :option(optionclass, …)

Creates a new option

  • optionclass: a class object of the section
  • additional parameters passed to the constructor of the option class
function :depends(key, value)

Only show this option field if another option key is set to value in the same section.

If you call this function several times the dependencies will be linked with "or"

function .filter(self, section) -abstract-

You can override this function to filter certain sections that will not be parsed.
The filter function will be called for every section that should be parsed and returns nil for sections that should be filtered.
For all other sections it should return the section name as given in the second parameter.

property .addremove = false

Allows the user to remove and recreate the configuration section

property .dynamic = false

Marks this section as dynamic. Dynamic sections can contain an undefinded number of completely userdefined options.

property .optional = true

Parse optional options

property .anonymous = false

Do not show UCI section names


class Value (option, title, description)

An object describing an option in a section of a UCI File. Creates a standard text field in the formular.

To instantiate use: NamedSection:option(Value, "option", "title", "description")

or TypedSection:option(Value, "option", "title", "description")

  • option: UCI option name
  • title: The title shown in the UI
  • description: description shown in the UI
function :depends(key, value)

Only show this option field if another option key is set to value in the same section.

If you call this function several times the dependencies will be linked with "or"

function :value(key, value)

Convert this text field into a combobox if possible and add a selection option.

property .default = nil

The default value

property .maxlength = nil

The maximum input length (of chars) of the value

property .optional = false

Marks this option as optional, implies .rmempty = true

property .rmempty = true

Removes this option from the configuration file when the user enters an empty value

property .size = nil

The maximum number of chars displayed by form field


class ListValue (option, title, description)

An object describing an option in a section of a UCI File.

Creates a list box or list of radio (for selecting one of many choices) in the formular.

To instantiate use: NamedSection:option(ListValue, "option", "title", "description")

or TypedSection:option(ListValue, "option", "title", "description")

  • option: UCI option name
  • title: The title shown in the UI
  • description: description shown in the UI
function :depends(key, value)

Only show this option field if another option key is set to value in the same section.

If you call this function several times the dependencies will be linked with "or"

function :value(key, value)

Adds an entry to the selection list

property .widget = “select”

select shows a selection list, radio shows a list of radio buttons inside form

property .default = nil

The default value

property .optional = false

Marks this option as optional, implies .rmempty = true

property .rmempty = true

Removes this option from the configuration file when the user enters an empty value

property .size = nil

The size of the form field


class Flag (option, title, description)

An object describing an option with two possible values in a section of a UCI File.

Creates a checkbox field in the formular.

To instantiate use: NamedSection:option(Flag, "option", "title", "description")

or TypedSection:option(Flag, "option", "title", "description")

  • option: UCI option name
  • title: The title shown in the UI
  • description: description shown in the UI
function :depends (key, value)

Only show this option field if another option key is set to value in the same section.

If you call this function several times the dependencies will be linked with "or"

property .default = nil

The default value

property .disabled = 0

the value that should be set if the checkbox is unchecked

property .enabled = 1

the value that should be set if the checkbox is checked

property .optional = false

Marks this option as optional, implies .rmempty = true

property .rmempty = true

Removes this option from the configuration file when the user enters an empty value


class MultiValue (option, title, description)

An object describing an option in a section of a UCI File.

Creates a list of checkboxed or a multiselectable list as form fields.

To instantiate use: NamedSection:option(MultiValue, "option", "title", "description")

or TypedSection:option(MultiValue, "option", "title", "description")

  • option: UCI option name
  • title: The title shown in the UI
  • description: description shown in the UI
function :depends (key, value)

Only show this option field if another option key is set to value in the same section.

If you call this function several times the dependencies will be linked with "or"

function :value(key, value)

Adds an entry to the list

property .widget = “checkbox”

select shows a selection list, checkbox shows a list of checkboxes inside form

property .delimiter = " "

The string which will be used to delimit the values inside stored option

property .default = nil

The default value

property .optional = false

Marks this option as optional, implies .rmempty = true

property .rmempty = true

Removes this option from the configuration file when the user enters an empty value

property .size = nil

The size of the form field (only used if property .widget = "select")


class StaticList (option, title, description)

Similar to the MultiValue, but stores selected Values into a UCI list instead of a character-separated option.


class DynamicList (option, title, description)

A extensible list of user-defined values. Stores Values into a UCI list


class DummyValue (option, title, description)

Creates a readonly text in the form. !It writes no data to UCI!

To instantiate use: NamedSection:option(DummyValue, "option", "title", "description")

or TypedSection:option(DummyValue, "option", "title", "description")

  • option: UCI option name
  • title: The title shown in the UI
  • description: description shown in the UI
function :depends (key, value)

Only show this option field if another option key is set to value in the same section.

If you call this function several times the dependencies will be linked with "or"


class TextValue (option, title, description)

An object describing a multi-line textbox in a section in a non-UCI form.


class Button (option, title, description)

An object describing a Button in a section in a non-UCI form.

Reference




  1. UCI: Unified Configuration Interface – n/a ↩︎

  2. Lua: 一种编程语言(解释型语言) ↩︎

发布了164 篇原创文章 · 获赞 76 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_29757283/article/details/100309490
今日推荐