Skip to content

Commit f94362e

Browse files
author
Olivier Bonnaure
committed
feat: allow variants depending on user agent
1 parent 5602958 commit f94362e

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

.init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ end
6060

6161
-- OnHttpRequest hook
6262
function OnHttpRequest()
63+
SetDevice() -- comment if you do not need this feature
64+
6365
Params = GetParams()
6466
PrepareMultiPartParams() -- if you handle file uploads
6567

@@ -77,3 +79,12 @@ function OnHttpRequest()
7779

7880
HandleRequest()
7981
end
82+
83+
function SetDevice()
84+
local user_agent = GetHeader("User-Agent")
85+
Variant = ""
86+
local preg = assert(re.compile("iPhone"))
87+
if preg:search(user_agent) then
88+
Variant = "iphone"
89+
end
90+
end

.lua/luaonbeans.lua

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,44 @@ function LoadViewsRecursively(path)
3030
end
3131

3232
function Page(view, layout, bindVarsView, bindVarsLayout)
33-
if (BeansEnv == "development") then
34-
Views["app/views/layouts/" .. layout .. "/index.html.etlua"] = Etlua.compile(LoadAsset("app/views/layouts/" ..
35-
layout .. "/index.html.etlua"))
36-
end
33+
Variant = Variant or ""
3734

3835
if (BeansEnv == "development") then
39-
Views["app/views/" .. view .. ".etlua"] = Etlua.compile(LoadAsset("app/views/" .. view .. ".etlua"))
36+
local asset = nil
37+
if Variant == "" then
38+
asset = LoadAsset("app/views/layouts/" .. layout .. "/index.html.etlua")
39+
Views["app/views/layouts/" .. layout .. "/index.html.etlua"] = Etlua.compile(asset)
40+
else
41+
asset = LoadAsset("app/views/layouts/" .. layout .. "/index.html+" .. Variant .. ".etlua")
42+
if asset == nil then
43+
asset = LoadAsset("app/views/layouts/" .. layout .. "/index.html.etlua")
44+
end
45+
Views["app/views/layouts/" .. layout .. "/index.html+" .. Variant .. ".etlua"] = Etlua.compile(asset)
46+
end
47+
48+
if Variant == "" then
49+
asset = LoadAsset("app/views/" .. view .. ".etlua")
50+
Views["app/views/" .. view .. ".etlua"] = Etlua.compile(asset)
51+
else
52+
asset = LoadAsset("app/views/" .. view .. "+" .. Variant .. ".etlua")
53+
if asset == nil then
54+
asset = LoadAsset("app/views/" .. view .. ".etlua")
55+
end
56+
Views["app/views/" .. view .. "+" .. Variant .. ".etlua"] = Etlua.compile(asset)
57+
end
4058
end
4159

42-
layout = Views["app/views/layouts/" .. layout .. "/index.html.etlua"](bindVarsLayout or {})
60+
compiled_layout = Views["app/views/layouts/" .. layout .. "/index.html.etlua"]
61+
if Variant ~= "" then
62+
compiled_layout = Views["app/views/layouts/" .. layout .. "/index.html+" .. Variant .. ".etlua"] or compiled_layout
63+
end
64+
layout = compiled_layout(bindVarsLayout or {})
4365

44-
if Views["app/views/" .. view .. ".etlua"] then
45-
view = Views["app/views/" .. view .. ".etlua"](bindVarsView or {})
66+
compiled_view = Views["app/views/" .. view .. ".etlua"]
67+
if Variant ~= "" then
68+
compiled_view = Views["app/views/" .. view .. "+" .. Variant .. ".etlua"] or compiled_view
4669
end
70+
view = compiled_view(bindVarsView or {})
4771

4872
local content
4973
if view:find("%%") then
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@yield
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello iphone

0 commit comments

Comments
 (0)