From 23c80f907c2fe30678e73e9bb5053696acca6635 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 5 Feb 2016 18:43:30 -0800 Subject: [PATCH] show individual quotes --- src/quotesite.nim | 41 ++++++++++++++++++++++++++++++------ src/templates/index.mustache | 2 +- src/templates/quote.mustache | 12 +++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 src/templates/quote.mustache diff --git a/src/quotesite.nim b/src/quotesite.nim index ca85410..8ac54ce 100644 --- a/src/quotesite.nim +++ b/src/quotesite.nim @@ -1,10 +1,11 @@ import asyncdispatch, db_sqlite, jester, moustachu, os, - shorturl, strutils, tables, times + shorturl, strutils, tables, times, typetraits const baseTemplate*: string = staticRead "./templates/layout.mustache" indexTemplate*: string = staticRead "./templates/index.mustache" errorTemplate*: string = staticRead "./templates/error.mustache" + quoteTemplate*: string = staticRead "./templates/quote.mustache" testTemplate*: string = """

hi!

""" @@ -37,7 +38,14 @@ template renderMustache*(title: string, templ: string, ctx: Context): expr = template fail*(): expr = var ctx = newContext() ctx["exception"] = getCurrentExceptionMsg() - renderMustache("error", errorTemplate, ctx) + + var + layoutCtx = moustachu.newContext() + + layoutCtx["title"] = title + layoutCtx["body"] = render(templ, ctx) + + halt render(baseTemplate, layoutCtx) settings: port = 5000.Port @@ -57,6 +65,7 @@ routes: for quote in items(quotes): var c = newContext() + c["listid"] = quote[0] c["id"] = quote[0].parseInt().encodeURLSimple() c["channel"] = quote[1] c["channelsafe"] = quote[1].replace("#", "hashtag-") @@ -72,16 +81,34 @@ routes: renderMustache("most recent quotes", indexTemplate, ctx) get "/quotes/@id": - var qid: int = -1 + var qid: int = 0 + try: - qid = (@"id").decodeURLSimple() + qid = (@"id").parseInt except: - fail() + qid = (@"id").decodeURLSimple() + redirect "/quotes/" & $qid let quote = db.getRow(sql"SELECT * FROM quotes WHERE id = ?", qid) - echo quote + if quote[1] == "": + halt Http404, "no such quote" - resp $quote + var + ctx = newContext() + + ctx["listid"] = qid + ctx["id"] = quote[0].parseInt().encodeURLSimple() + ctx["channel"] = quote[1] + ctx["channelsafe"] = quote[1].replace("#", "hashtag-") + ctx["nick"] = quote[2] + ctx["adder"] = quote[3] + ctx["message"] = quote[4] + ctx["time"] = parseInt(split(quote[5], '.')[0]) + + ctx["last"] = qid - 1 + ctx["next"] = qid + 1 + + renderMustache("quote #" & $qid & " by " & quote[2], quoteTemplate, ctx) runForever() diff --git a/src/templates/index.mustache b/src/templates/index.mustache index fe959ec..39e6f46 100644 --- a/src/templates/index.mustache +++ b/src/templates/index.mustache @@ -1,6 +1,6 @@ {{# quotes }}
-

{{channel}} - {{time}}

+

{{channel}} - #{{listid}} - {{time}}

({{nick}}) {{message}}

Added by {{adder}}. Permalink.
diff --git a/src/templates/quote.mustache b/src/templates/quote.mustache new file mode 100644 index 0000000..f008d66 --- /dev/null +++ b/src/templates/quote.mustache @@ -0,0 +1,12 @@ +
+

{{channel}} - #{{ listid }} - {{time}}

+

({{nick}}) {{message}}

+ Added by {{adder}}. Permalink. +
+
+ +
+ +
+

prev this next

+