nimWebTemplates https://github.com/enthus1ast/nimWebTemplates
nimWebTemplates这个东西, 是一个简单的类似于django 的template , 类似于jinja2的模板, 有 block, end, extends, for, if之类的标签.
大部分的东西, 都是2年前的, 最近的修改是5个月前的. nim的项目作者都是这样, 要不就是5年前的作品放在那里等人挖坟, 要不就是作者诈尸弹起来突然更新一下.
创建如下目录结构(注: 作者目前只支持单层目录, 2层目录的不支持, 我扩展了, 很简单, 现在还没发出来.)
./example.nim
./templates/base.html
./templates/index.html
./templates/about.html
./templates/stats.html
{%block menu%}index || about || stats{%endblock%}
{%import menu.html%}
{{title}}
{{self.menu}}
{{title}}
{%block "content"%}
{# i will be replaced by my children #}
{%endblock%}
Made with nim
{{self.footer}}
example.nim
import asynchttpserver, asyncdispatch
import json
import nwt
var templates = newNwt("templates/*.html") # we have all the templates in a folder called "templates"
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
let res = req.url.path
case res
of "/", "/index.html":
await req.respond(Http200, templates.renderTemplate("index.html"))
of "/about.html":
await req.respond(Http200, templates.renderTemplate("about.html"))
of "/stats.html":
var context = %* {"title": "some variables from nim", "foo": "Foo!", "baa": "Baa!"}
await req.respond(Http200, templates.renderTemplate("stats.html", context))
else:
await req.respond(Http404, "not found")
waitFor server.serve(Port(8080), cb)
基本上, 基于example.nim, 就可以开发一个同步的web 框架了. 我就是在这个的基础上, 加了很多东西, 写了以简单的nim语言的web框架