在Luci中增加一个elements

这是一个展示如何向LuCI接口添加新元素的示例。

增加一个顶级页签

首先,我们将在顶部导航中添加一个新的标签。通常人们可以看到:Freifunk | Status | System | Services | Network等。

您可以通过在<luci-path>/applications/luci-myapplication/luasrc/controller/myapp中添加一个文件到控制器目录来实现这一点。

<luci-path>: LuCI源文件路径;

luci-myapplication: 为此例子创建的一个新目录. 这个目录的名字必须要以luci-开头,这样才能被make menuconfig识别为一个模块,并作为一个模块被编译;

controller:UI控制的默认目录;

myapp:为此例子增加的目录; 

我们假设新增文件名字为: new_tab.lua,该文件内容如下:

module("luci.controller.myapp.new_tab", package.seeall)  --notice that new_tab is the name of the file new_tab.lua,表示创建一个模块
function index()
entry({"admin", "new_tab"}, firstchild(), "New tab", 60).dependent=false  --this adds the top level tab and defaults to the first sub-tab (tab_from_cbi), also it is set to position 30
entry({"admin", "new_tab", "tab_from_cbi"}, cbi("myapp-mymodule/cbi_tab"), "CBI Tab", 1)  --this adds the first sub-tab that is located in <luci-path>/luci-myapplication/model/cbi/myapp-mymodule and the file is called cbi_tab.lua, also set to first position
entry({"admin", "new_tab", "tab_from_view"}, template("myapp-mymodule/view_tab"), "View Tab", 2)  --this adds the second sub-tab that is located in <luci-path>/luci-myapplication/view/myapp-mymodule and the file is called view_tab.htm, also set to the second position
end

添加cbi_tab代码

根据上面的代码,我们需要创建一个cbi_tab.lua文件,路径: <luci-path>/luci-myapplication/model/cbi/myapp-mymodule

该文件中的代码如下,该文件定义了一个配置文件、section和option

m = Map("cbi_file", translate("First Tab Form"), translate("Please fill out the form below")) -- cbi_file is the config file in /etc/config
d = m:section(TypedSection, "info", "Part A of the form")  -- info is the section called info in cbi_file
a = d:option(Value, "name", "Name"); a.optional=false; a.rmempty = false;  -- name is the option in the cbi_file
return m

添加cbi_file文件

从上面的代码中,我们知道我们需要一个具有适当的section和option的配置文件。在我们的例子中,我们将在/etc/配置中创建文件cbi_file,如下所示:

config 'info' 'A'
	option 'name' 'OpenWRT'

添加view_tab代码

文件view_tab.htm 应该放在<luci-path>/applications/luci-myapplication/luasrc/view/myapp-mymodule/路径下,具体内容如下:

<%+header%>                                                                    
<h1><%:Hello World%></h1>                                                      
<%+footer%>

module函数和entry函数

module函数

将该文件生成一个模块,可以通过requeire(“model_name”)来载入该模块,文件的后缀名是.lua。

entry函数

entry 功能函数的原型为
entry(path, target, title=nil, order=nil) 前面两个参数是必须的,后面两个是可选的。
Path 形如(admin,system,reboot),entry 根据这些属性创建节点,比如上面内容代表在 system 标签中的 reboot 页面,target 是这里的重点,它描述了当用户调用这个界面所执行的行为。这里主要由三种行为,call、template、cbi,

  • call 为调用文件中的功能函数,
  • template 为调用 view 中的 html 界面,
  • cbi 为调用 mode 中的 lua 文件。

Title 是在页面中显示的内容,order 是显示的顺序,顺序越小显示内容越靠前。下面在 system.lua 中增加一条:

entry({"admin", "system", "helloworld"}, template("admin_system/helloworld"), _("helloworld "), 99)

  • 第一个参数代表一个节点,
  • 第二个参数,调用 view/admin_system/helloworld.htm 文件,
  • 第三个参数为显示的名字,
  • 第四个参数为显示顺序,这里在最后显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李小白20200202

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值