Permalink
Browse files

update example page & fix template bug

  • Loading branch information...
1 parent ebdb4b9 commit 9128d15accd921e9400ae6bce0368c65c3a84f71 @chzyer chzyer committed Mar 7, 2012
View
@@ -1,16 +1,27 @@
is_extend = false
function html(data)
-title = has(data, "title") ? data["title"] : """你好, 这里是base的标题"""
-body = has(data, "body") ? data["body"] : """
- """
-"""<html>
-<head>
- <title>$(title)</title>
-</head>
-<body>
- $(body)
-
- $(time())
-</body>
+bar = has(data, "bar") ? data["bar"] : """"""
+title = has(data, "title") ? data["title"] : """hello world"""
+js = has(data, "js") ? data["js"] : """"""
+body = has(data, "body") ? data["body"] : """asdf"""
+"""<!DOCTYPE HTML>\
+<html lang=zh-CN>\
+<head>\
+ <meta charset=\"utf-8\">\
+ <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
+ <link href=\"/css/base.css\" media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" />\
+ <title>$(title)</title> \
+ <script src=\"/js/jquery.min.js\"></script>\
+</head>\
+<body>\
+ <div id=\"topbar\">Welcome to JuliaWebServer</div>\
+ <div id=\"header\"><a href=\"/\">JuliaWebServer</a></div>\
+ <div id=\"bar\"><a href=\"/\">index</a> &rsaquo; $(bar)</div>\
+ <div id=\"body\">$(body)</div>\
+ <div id=\"footbar\">Here is the footer</div>\
+ <script>\
+ $(js)\
+ </script>\
+</body>\
</html>"""
end
View
@@ -1,39 +1,10 @@
is_extend = true
function html()
extend = "base"
-title = """thisisnewtitle"""
-body = """
-<a href=\"javascript\"><img src=\"http://img2.douban.com/pics/fw2douban_s.png\" alt=\"推荐到豆瓣\" /></a>
- """
-for i=1:10
-body = strcat(body, """
- <a href=\"javascript:;\">$(i)d</a>
- """)
-end
-if 1==2
-body = strcat(body, """
- 1 = 2
- """)
-elseif 2==2
-body = strcat(body, """
- """)
-if 1==1
-body = strcat(body, """
- 哈拉拉
- """)
-else
-body = strcat(body, """
- 也是
- """)
-end
-else
-body = strcat(body, """
- 1 != 2<br>
- salflkjsadlfkjasdf
- """)
-end
-body = strcat(body, """
- <img src=\"http://img2.douban.com/pics/fw2douban_s.png\" alt=\"推荐到豆瓣\" />
-""")
+title = """welcome to julia webserver"""
+body = """\
+<div>This example will show you how to use template:</div>\
+<li><a href=\"/source/loop\">loop</a></li>\
+"""
{"extend"=>extend,"title"=>title,"body"=>body,}
end
@@ -0,0 +1,53 @@
+is_extend = true
+function html()
+extend = "base"
+title = """welcome to julia webserver"""
+bar = """source loop"""
+body = """\
+<h2>loop</h2>\
+<h3>loop source:</h3>\
+<code>\
+{\% for i=1:10 %}\
+<li><a href=\"javascript:;\">item{\{ i }}</a></li>\
+{\% end %}\
+</code>\
+<h3>real source:</h3>\
+<code>\
+ """
+for i=1:10
+body = strcat(body, """\
+ <li><a href=\"javascript:;\">item$(i)</a></li>\
+ """)
+end
+body = strcat(body, """\
+</code>\
+<h3>html view:</h3>\
+<div class=\"viewcode\">\
+ """)
+for ii=1:10
+body = strcat(body, """\
+ <li><a href=\"javascript:;\">item$(ii+11)</a></li>\
+ """)
+end
+body = strcat(body, """\
+</div>\
+""")
+js = """\
+function replace(str, reg, news){\
+ return str.replace(new RegExp(reg, \"gm\"), news)\
+}\
+\$(\"code\").each(function(){\
+ html = \$(this).html();\
+ new_html = html;\
+ new_html = replace(new_html, \"<\", \"&lt;\")\
+ new_html = replace(new_html, \">\", \"&gt;\")\
+ if (new_html[0] == \"\\n\"){\
+ new_html = new_html.substring(1)\
+ }\
+ new_html = replace(new_html, \"\\n\", \"<br>\")\
+ \$(this).html(new_html).wrap(\"<div class='code'></div>\")\
+\
+})\
+"""
+ {"extend"=>extend,"title"=>title,"bar"=>bar,"body"=>body,"js"=>js,}
+end
View
@@ -0,0 +1,52 @@
+html{font-size:14px;background:#ccc;}
+body{
+ width:900px;
+ background: #999;
+ margin: 0 auto;
+ font-family:"Microsoft Yahei";
+}
+a{
+ text-decoration:none;
+}
+li {list-style:none;}
+body > #topbar{
+ background:#555;
+ padding:2px;
+ color:#fff;
+ padding-left:5px;
+}
+body > #footbar{
+ background:#888;
+ text-align:right;
+ padding:2px;
+ color:#000;
+ padding-right:5px;
+}
+body > #header{
+ font-size:32px;
+ background: #aaa;
+ padding:20px;
+}
+body > #header a{
+ color:#88c701;
+}
+body > #body{
+ padding-left:5px;
+ padding-right:5px;
+}
+
+body > #body div.code,div.viewcode{
+ border:1px dashed #555;
+ padding:5px;
+ margin:10px;
+ background:#ddd;
+}
+
+body > #bar {
+ background:#444;
+ padding-left:10px;
+ color:#999;
+}
+body > #bar a{
+ color:#aaa;
+}
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -3,10 +3,6 @@ load("sub.j")
mainHandler = Handler(
function (f)
-
- f.write(f.get_cookie("user", nothing))
- f.write(f.get_cookie("_xsrf", nothing))
- f.write("欢迎通过get方式访问main")
f.render("index")
end
,
@@ -17,10 +13,17 @@ mainHandler = Handler(
end
)
+SourceLoopHandler = Handler(
+ function (f)
+ f.render("source/loop")
+ end,
+ nothing
+)
__handlers = [
(r"/", mainHandler),
- (r"/[^/]+", subHandler)
+ (r"/source/loop", SourceLoopHandler),
+ (r"/[^/]+", subHandler),
]
_setting = {
debug = true
View
@@ -1,11 +1,20 @@
-<html>
+<!DOCTYPE HTML>
+<html lang=zh-CN>
<head>
- <title>{% block title %}你好, 这里是base的标题{% end %}</title>
+ <meta charset="utf-8">
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <link href="/css/base.css" media="screen, projection" rel="stylesheet" type="text/css" />
+ <title>{% block title %}hello world{% end %}</title>
+ <script src="/js/jquery.min.js"></script>
</head>
<body>
- {% block body %}
- {% end %}
-
- {{ time() }}
+ <div id="topbar">Welcome to JuliaWebServer</div>
+ <div id="header"><a href="/">JuliaWebServer</a></div>
+ <div id="bar"><a href="/">index</a> &rsaquo; {% block bar %}{% end %}</div>
+ <div id="body">{% block body %}asdf{% end %}</div>
+ <div id="footbar">Here is the footer</div>
+ <script>
+ {% block js %}{% end %}
+ </script>
</body>
</html>
View
@@ -1,24 +1,7 @@
{% extend base %}
-{% block title %}thisisnewtitle{% end %}
+{% block title %}welcome to julia webserver{% end %}
{% block body %}
-<a href="javascript"><img src="http://img2.douban.com/pics/fw2douban_s.png" alt="推荐到豆瓣" /></a>
- {% for i=1:10 %}
- <a href="javascript:;">{{ i }}d</a>
- {% end %}
-thisisbody
- {% if 1==2 %}
- 1 = 2
- {% elseif 2==2 %}
- {% if 1==1 %}
- 哈拉拉
- {% else %}
- 也是
- {% end %}
- {% else %}
- 1 != 2<br>
- salflkjsadlfkjasdf
- {% end %}
- <img src="http://img2.douban.com/pics/fw2douban_s.png" alt="推荐到豆瓣" />
-{% end %}
-
+<div>This example will show you how to use template:</div>
+<li><a href="/source/loop">loop</a></li>
+{% end %}
@@ -0,0 +1,42 @@
+{% extend base %}
+{% block title %}welcome to julia webserver{% end %}
+{% block bar %}source loop{% end %}
+{% block body %}
+<h2>loop</h2>
+<h3>loop source:</h3>
+<code>
+{\% for i=1:10 %}
+<li><a href="javascript:;">item{\{ i }}</a></li>
+{\% end %}
+</code>
+<h3>real source:</h3>
+<code>
+ {% for i=1:10 %}
+ <li><a href="javascript:;">item{{ i }}</a></li>
+ {% end %}
+</code>
+<h3>html view:</h3>
+<div class="viewcode">
+ {% for ii=1:10 %}
+ <li><a href="javascript:;">item{{ ii+11 }}</a></li>
+ {% end %}
+</div>
+{% end %}
+
+{% block js %}
+function replace(str, reg, news){
+ return str.replace(new RegExp(reg, "gm"), news)
+}
+$("code").each(function(){
+ html = $(this).html();
+ new_html = html;
+ new_html = replace(new_html, "<", "&lt;")
+ new_html = replace(new_html, ">", "&gt;")
+ if (new_html[0] == "\n"){
+ new_html = new_html.substring(1)
+ }
+ new_html = replace(new_html, "\n", "<br>")
+ $(this).html(new_html).wrap("<div class='code'></div>")
+
+})
+{% end %}
View
@@ -111,15 +111,18 @@ function __eval_exprs(__parsed_exprs)
break
end
end
-
- if match_route
+
+ if f != nothing && match_route
html = f.data["html"]
status = f.data["status"]
+ cookie = f.data["cookie"]
else
status = __htmlfunc_senderror(404)
html = status
+ cookie = nothing
end
- __write_back(html, status, f.data["cookie"])
+
+ __write_back(html, status, cookie)
__connect()
end
@@ -16,7 +16,7 @@ function __write_back(html, status, cookie)
write(__io, "$DEFAULT_PROTOCOL $status\n")
write(__io, "Server: Microsoft-IIS/5.0\n")
write(__io, "Content-Length: $(length(html))\n")
- if length(cookie) > 0
+ if cookie != nothing && length(cookie) > 0
for i = cookie
write(__io, "Set-Cookie: $i\n")
end
Oops, something went wrong.

0 comments on commit 9128d15

Please sign in to comment.