-
# Declare template folder
templateFolder("templates")
proc render(title: string): string =
## Renders template and returns HTML string
##
## `title` is template argument
renderTemplate("index.html")
# Below is code I am trying to do, but don't know how...
const file_list = @["index.html", "index2.html", "index3.html"]
const target_dir = "public"
# a function call to render all files to target dir at compile time
renderFiles(file_list, target_dir)
I have tried many way, including using templates, but failed. please help |
Beta Was this translation helpful? Give feedback.
Answered by
Ethosa
Nov 30, 2023
Replies: 2 comments 6 replies
-
proc renderFiles(fileList: seq[string], directory: string): string =
## Renders all files
renderTemplate("filelist.html")
<!DOCTYPE html>
<html>
<head>...</head>
<body>
{% for file in fileList %}
{{ directory }}/{{ file }}
{% endfor %}
</body>
</html> read more about nimja template engine: https://github.com/enthus1ast/nimja |
Beta Was this translation helpful? Give feedback.
3 replies
-
I found a solution using templates, I know it's not optimal solution, but post it in case someone need it, btw it's good to learn how nim compile time computation works const listFiles = @["page_1.html", "page_2.html", "page_3.html"]
template renderMyTemplate(fileName) =
case listFiles.find(fileName) :
of 0:
renderTemplate(listFiles[0])
of 1:
renderTemplate(listFiles[1])
of 2:
renderTemplate(listFiles[2])
else:
echo "file not found"
proc render(fileName: string, element: string, class: string): string =
renderMyTemplate(fileName)
how to use for f in listFiles:
let e = "elementName"
let c = "className"
writeFile(target_dir, render(f,e,c)) please note if you add new file to listFiles, you need to manually add |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use this example to fetch files from your templates directory:
And example below shows how you can replace
case statement
to macro but keep logic in runtime still: