欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Lua編寫Web端模板引擎的實(shí)例代碼分享

 更新時(shí)間:2016年06月24日 16:59:18   作者:烏龜殼  
這里我們給出一個(gè)使用Lua編寫Web端模板引擎的實(shí)例代碼分享,力求簡潔(核心代碼約為70行),僅實(shí)現(xiàn)最基本的模板功能:

ltemplate.lua

local insert = table.insert
local remove = table.remove
local concat = table.concat
local format = string.format

local loaded = {}
local partten = "(.-){#([^#].-[^#])#}()"

local content = {}
local cur_content = nil

local function ob_start()
 cur_content = {}
 insert(content, cur_content)
end

local function ob_get_clean()
 local ret = concat(cur_content)
 remove(content)
 cur_content = content[#content]
 return ret
end

local function echo(value)
 insert(cur_content, value)
end

local function include(path, params)
 local bitcode = loaded[path]

 if not bitcode then
 local fp = io.open(path, "rb")
 local template = fp:read('*a')
 fp:close()
 local results = {}
 local last_endpos = 0
 for outside, inside, endpos in template:gmatch(partten) do
  insert(results, format("echo(%q)", outside))
  insert(results, inside)
  last_endpos = endpos
 end
 insert(results, format("echo(%q)", template:sub(last_endpos)))
 results = concat(results, "\n")
 bitcode = assert(loadstring(results))
 loaded[path] = bitcode
 end

 local env = {
 include = include,
 echo = echo,
 ob_start = ob_start,
 ob_get_clean = ob_get_clean
 }
 setmetatable(env, {__index = function(tb, k)
 return params[k] or _G[k]
 end})
 setfenv(bitcode, env)
 bitcode()
end

for i = 1, 100000 do
 ob_start()
 include(arg[1], {
 params = {
  a = '1234',
  b = '4321'
 }
 })
 ob_get_clean()
end

master.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <meta http-equiv="Content-Language" content="zh-CN"/>
 <meta name="robots" content="index, follow" />
 <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico" />
 <title> child&apos;s personal page - 開源中國社區(qū)</title>
   <link rel="stylesheet/less"  type="text/css" media="screen" />
 <link rel="stylesheet" href="/js/2012/poshytip/tip-yellowsimple/tip-yellowsimple.css" type="text/css" />
 <link rel="stylesheet" type="text/css" href="/js/2011/fancybox/jquery.fancybox-1.3.4.css" media="screen" />
 <script type="text/javascript" src="/js/2012/jquery-1.7.1.min.js"></script>
 <script type="text/javascript" src="/js/2012/jquery.form.js"></script>
 <script type="text/javascript" src="/js/2011/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
 <script type="text/javascript" src="/js/2012/poshytip/jquery.poshytip.min.js"></script>
 <script type="text/javascript" src="/js/2011/oschina.js?ver=20121007"></script>
 <script type="text/javascript" src="/js/2012/less-1.3.0.min.js"></script>
 <script type="text/javascript" src="/js/scrolltopcontrol.js"></script>
 <script type='text/javascript' src='/js/jquery/jquery.atwho.js?ver=2013112501'></script>
 <link rel="stylesheet" type="text/css" href="/js/jquery/jquery.atwho.css" />
 <link rel="alternate" type="application/rss+xml" title="lostchild最新博客"  />
 <link rel="EditURI" type="application/rsd+xml" title="RSD"  />
 <link rel="wlwmanifest" type="application/wlwmanifest+xml"  />
 {# echo(header) #}
</head>
<body>
{# echo(content) #}
<body>
</html>

temp.html,繼承master.html

{# ob_start() #}
<script>
 alert("hello World")
</script>
{# local header = ob_get_clean() #}


{# ob_start() #}
<table>
{# for k, v in pairs(params) do #}
<tr>
 <td>{# echo(k) #}</td>
 <td>{# echo(v) #}</td>
</tr>
{# end #}
</table>
{# local content = ob_get_clean() #}

{# include('master.html', {header = header, content = content}) #}

循環(huán)十萬次測試渲染速度(阿里云最便宜一款vps)

[root@AY130801221248587d02Z ~]# time lua ltemplate.lua temp.htmlreal  0m1.867s
user  0m1.862s
sys   0m0.004s

總結(jié)

由此可見渲染的速度還是非??斓?,可以將此原型用于嵌入式設(shè)備中的頁面上(用大量js實(shí)現(xiàn)的嵌入式設(shè)備頁面兼容性不好)。而且嵌入式設(shè)備的界面需要簡單明確,所以也不用太豐富的模版功能。

原理很簡單:

1.用lua版的正則把模版內(nèi){#與#}之間的內(nèi)容挖出來,原樣輸出成lua代碼,其它部分則生成使用echo打印到某個(gè)緩沖區(qū)的lua代碼。

2.將這個(gè)生成出來的代碼使用loadstring編譯。

3.通過setfenv實(shí)現(xiàn)loadstring后的模擬環(huán)境配置(用以提供模版內(nèi)使用的echo,ob_start等函數(shù),以及傳入的參數(shù))

4.執(zhí)行這個(gè)編譯后的函數(shù)即可。

相關(guān)文章

  • Lua的協(xié)程(coroutine)簡介

    Lua的協(xié)程(coroutine)簡介

    這篇文章主要介紹了Lua的協(xié)程(coroutine)簡介,本文講解了coroutine的創(chuàng)建、協(xié)程的三種狀態(tài)和yield函數(shù)的配合使用等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • linux系統(tǒng)安裝Nginx Lua環(huán)境

    linux系統(tǒng)安裝Nginx Lua環(huán)境

    因項(xiàng)目需求,需要在Linux系統(tǒng)下搭建一套nginx+lua的開發(fā)環(huán)境,經(jīng)過一番摸索,現(xiàn)總結(jié)如下,希望大家能夠喜歡。
    2016-12-12
  • Lua中for循環(huán)語句的使用教程

    Lua中for循環(huán)語句的使用教程

    這篇文章主要介紹了Lua中for循環(huán)語句的使用教程,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Lua多行注釋和取消多行注釋的方法

    Lua多行注釋和取消多行注釋的方法

    這篇文章主要介紹了Lua多行注釋和取消多行注釋的方法,本文分別給出代碼示例,請(qǐng)注意細(xì)節(jié)~,需要的朋友可以參考下
    2015-06-06
  • 詳解Lua中的while循環(huán)語句的使用

    詳解Lua中的while循環(huán)語句的使用

    這篇文章主要介紹了詳解Lua中的while循環(huán)語句的使用,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Lua數(shù)據(jù)類型介紹

    Lua數(shù)據(jù)類型介紹

    這篇文章主要介紹了Lua數(shù)據(jù)類型介紹,本文講解了Lua中的nil(空)、boolean(布爾)、number(數(shù)字)、string(字符串)、table(表)、function(函數(shù))、thread(線程)、userdata(自定義類型)等數(shù)據(jù)類型,需要的朋友可以參考下
    2014-12-12
  • Mac平臺(tái)中編譯安裝Lua運(yùn)行環(huán)境及Hello Lua實(shí)例

    Mac平臺(tái)中編譯安裝Lua運(yùn)行環(huán)境及Hello Lua實(shí)例

    這篇文章主要介紹了Mac平臺(tái)中編譯安裝Lua運(yùn)行環(huán)境及Hello Lua實(shí)例,本文給出了兩種Hello Lua示例,一種是控制臺(tái)直接輸出,通過文件編譯輸出,需要的朋友可以參考下
    2014-10-10
  • vs2012 error c4996: This function or variable may be unsafe

    vs2012 error c4996: This function or variable may be unsafe

    這篇文章主要介紹了vs2012 error c4996: This function or variable may be unsafe,需要的朋友可以參考下
    2015-04-04
  • Lua中的閉包學(xué)習(xí)筆記

    Lua中的閉包學(xué)習(xí)筆記

    這篇文章主要介紹了Lua中的閉包學(xué)習(xí)筆記,閉包是可以包含自由(未綁定到特定對(duì)象)變量的代碼塊;這些變量不是在這個(gè)代碼塊內(nèi)或者任何全局上下文中定義的,而是在定義代碼塊的環(huán)境中定義(局部變量),需要的朋友可以參考下
    2014-12-12
  • Lua極簡入門指南(一):基礎(chǔ)知識(shí)篇

    Lua極簡入門指南(一):基礎(chǔ)知識(shí)篇

    這篇文章主要介紹了Lua極簡入門指南(一):基礎(chǔ)知識(shí)篇,本文羅列了Lua的基礎(chǔ)知識(shí),如注釋、數(shù)據(jù)類型、table、循環(huán)控制結(jié)構(gòu)等內(nèi)容,需要的朋友可以參考下
    2014-10-10

最新評(píng)論