浏览代码

templates `for` added.

Ethosa 5 年之前
父节点
当前提交
fa5fa06765
共有 6 个文件被更改,包括 65 次插入6 次删除
  1. 1 1
      README.md
  2. 1 1
      akane.nimble
  3. 28 4
      akane/akane.nim
  4. 20 0
      tests/test6/main.nim
  5. 1 0
      tests/test6/nim.cfg
  6. 14 0
      tests/test6/templates/index.html

+ 1 - 1
README.md

@@ -5,7 +5,7 @@
 [![Nim language-plastic](https://github.com/Ethosa/yukiko/blob/master/nim-lang.svg)](https://github.com/Ethosa/yukiko/blob/master/nim-lang.svg)
 [![License](https://img.shields.io/github/license/Ethosa/akane)](https://github.com/Ethosa/akane/blob/master/LICENSE)
 
-<h4>Latest version - 0.0.6</h4>
+<h4>Latest version - 0.0.7</h4>
 <h4>Stable version - ?</h4>
 </div>
 

+ 1 - 1
akane.nimble

@@ -1,7 +1,7 @@
 [Package]
 name = "akane"
 author = "Ethosa"
-version = "0.0.6"
+version = "0.0.7"
 description = "The Nim asynchronous web framework."
 license = "MIT"
 srcDir = "akane"

+ 28 - 4
akane/akane.nim

@@ -51,6 +51,13 @@ proc newServer*(address: string = "127.0.0.1",
   )
 
 
+proc toStr*(node: JsonNode): string =
+  if node.kind == JString:
+    return node.getStr
+  else:
+    return $node
+
+
 proc loadtemplate*(name: string, json: JsonNode = %*{}): Future[string] {.async, inline.} =
   ## Loads HTML template from `templates` folder.
   ##
@@ -61,6 +68,8 @@ proc loadtemplate*(name: string, json: JsonNode = %*{}): Future[string] {.async,
   ## Replaces:
   ## -  $(key) -> value
   ## -  if $(key) { ... } -> ... (if value is true)
+  ## -  if not $(key) { ... } -> ... (if value is false)
+  ## -  for i in 0..$(key) { ... } -> ........., etc
   var
     file = openAsync(("templates" / name) & ".html")
     readed = await file.readAll()
@@ -68,12 +77,15 @@ proc loadtemplate*(name: string, json: JsonNode = %*{}): Future[string] {.async,
   for key, value in json.pairs:
     # ---- regex patterns ---- #
     let
-      # variable statment, e.g.: $(variable)
+      # variable statement, e.g.: $(variable)
       variable_stmt = re("(\\$\\s*\\(" & key & "\\))")
-      # if statment, e.g.: if $(variable) {......}
+      # if statement, e.g.: if $(variable) {......}
       if_stmt = re("if\\s*(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
-      # if not statment, e.g.: if not $(variable) {......}
+      # if not statement, e.g.: if not $(variable) {......}
       if_notstmt = re("if\\s*not\\s*(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
+      # for statement, e.g.: for i in 0..$(variable) {hello, $variable[i]}
+      forstmt = re(
+        "for\\s*([\\S]+)\\s*in\\s*(\\d+)\\.\\.(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
 
     # ---- converts value to bool ---- #
     var value_bool = false
@@ -109,7 +121,19 @@ proc loadtemplate*(name: string, json: JsonNode = %*{}): Future[string] {.async,
         readed = readed.replacef(if_notstmt, "")
       else:
         readed = readed.replacef(if_notstmt, "$2")
-    readed = readed.replacef(variable_stmt, $value)
+    var
+      matches: array[20, string]
+      now = 0
+    while readed.contains(forstmt):
+      let
+        (start, stop) = readed.findBounds(forstmt, matches, now)
+        elem = re("(\\$" & key & "\\[" & matches[0] & "\\])")
+      var output = ""
+      for i in parseInt(matches[1])..<value.len:
+        output &= matches[3].replacef(elem, value[i].toStr)
+      readed = readed[0..start-1] & output & readed[stop+1..^1]
+      now += stop
+    readed = readed.replacef(variable_stmt, value.toStr)
   return readed
 
 

+ 20 - 0
tests/test6/main.nim

@@ -0,0 +1,20 @@
+# author: Ethosa
+# templates for
+import akane
+
+
+proc main =
+  var
+    server = newServer(debug=true)
+    data = %*{
+      "fruits": %["apple", "banana"]
+    }
+
+  server.pages:
+    "/":
+      let index = await loadtemplate("index", data)
+      await request.answer(index)
+
+  server.start()
+
+main()

+ 1 - 0
tests/test6/nim.cfg

@@ -0,0 +1 @@
+--path:"../../akane"

+ 14 - 0
tests/test6/templates/index.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>For</title>
+</head>
+<body>
+  <ul>
+    for i in 0..$(fruits)
+    {
+      <li>$fruits[i]</li>
+    }
+  </ul>
+</body>
+</html>