天天看點

OpenWRT(十一)LuCi開發(二)

一、文法介紹

上一節我們添加了下面這條語句:

entry({“admin”,“system”,“helloworld”}, template(“admin_system/helloworld”), _(“Helloworld”), 99);

​entry(path, target, title=nil, order=nil)這個函數用于注冊一個節點​

​參數介紹:​

path: 在排程樹的位置,例如:{“foo”, “bar”, “baz”}會插入foo.bar.baz節點(檔案樹形結構)

target: 使用者請求(點選)節點時調用的動作(可以了解為監聽器),有三種類型:

call, template, cbi

title: menu上面顯示的字元串(可選)

order: 在相同層次的排列順序(可選)

​template(“admin_system/helloworld”)​

template :這個方式對應于web頁面。

admin_system/helloworld: 對應/view/admin_system目錄下的helloworld.htm檔案

​_(“Helloworld”)​

在web頁面上顯示的标題

二、template方式的htm檔案基本文法

1、 包含Lua代碼:

<% code %>

2、 輸出變量和函數值:

  1. <% write(value) %>
  2. <%=value%>

3、 包含模闆:

  1. <% include(templatesName) %>
  2. <%+templatesName%>

4、 轉換:

  1. <%= translate(“Text to translate”) %>
  2. <%:Text to translate%>

5、 注釋:

<%# comment %>

其他文法跟html和JavaScript一樣。

<%+header%>
<h1><%: HelloWorld %></h1>
<%+footer%>      

現在看上面的内容應該就容易很多了

​<%+header%>​

​就是包含一個header.htm的檔案,它在源碼中有提供,我們不需要自己寫。

​<h1></h1>​

​就是html标簽

​<%: HelloWorld %>​

​輸出“HelloWorld”字元串

​<%+footer%>​

​就是包含一個footer.htm的檔案,它在源碼中有提供,我們不需要自己寫。

三、在源碼中添加LuCI頁面

上一節我們是在系統上進行添加,這次我們直接在源碼上進行添加,然後再進行編譯燒錄。

進入openwrt/feeds/luci/application,添加如下目錄結構

OpenWRT(十一)LuCi開發(二)

在luci-myapplication目錄下建立一個Makefile,内容如下:

include $(TOPDIR)/rules.mk

LUCI_TITLE:=LuCI Support for Test
LUCI_DEPENDS:=

include ../../luci.mk

# call BuildPackage - OpenWrt buildroot signature      

3、在myapp目錄下建立new_tab.lua,内容如下:

module("luci.controller.myapp.new_tab", package.seeall)  --new_tab要與檔案名一緻
 function index()
     entry({"admin", "new_tab"}, firstchild(), "New tab", 60).dependent=false  --添加一個頂層導航
     entry({"admin", "new_tab", "tab_from_cbi"}, cbi("myapp-mymodule/cbi_tab"), "CBI Tab", 1)  --在New tab下添加一個子選項CBI Tab
     entry({"admin", "new_tab", "tab_from_view"}, template("myapp-mymodule/view_tab"), "View Tab", 2)  --在New tab下添加一個子選項View Tab
end      

4、在cbi/myapp-mymodule目錄下建立cbi_tab.lua,内容如下:

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      

5、在view/myapp-mymodule目錄下建立view_tab.htm,内容如下:

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

6、執行更新:

./scripts/feeds update luci

./scripts/feeds install -a -p luci

make menuconfig

OpenWRT(十一)LuCi開發(二)
OpenWRT(十一)LuCi開發(二)
OpenWRT(十一)LuCi開發(二)

7、編譯燒錄

8、在開發闆/etc/config目錄下建立cbi_file檔案,内容如下:

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

9、在浏覽器輸入192.168.1.1進入web界面,效果如下:

OpenWRT(十一)LuCi開發(二)
OpenWRT(十一)LuCi開發(二)
OpenWRT(十一)LuCi開發(二)