Skip to content

How render a list of Files with renderTemplate #203

Answered by Ethosa
jerrygzy asked this question in Q&A
Discussion options

You must be logged in to vote

You can use this example to fetch files from your templates directory:

import
  os,
  macros,
  happyx


macro getFilesInDir*(directory: static[string]): untyped =
  # result is [file1, file2, file3, etc.]
  result = newNimNode(nnkBracket)
  for i in directory.walkDirRec(followFilter = {}, relative = true):
    result.add(newLit(i))


const files = getFilesInDir("templates")
echo files

And example below shows how you can replace case statement to macro but keep logic in runtime still:

macro renderTemplates*(arr: static[openarray[string]]): untyped =
  result = newNimNode(nnkCaseStmt).add(
    newCall("find", ident"files", ident"filename")
  )
  for i in arr.low..arr.high:
    result.add(n…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
3 replies
@jerrygzy
Comment options

@Ethosa
Comment options

@jerrygzy
Comment options

Comment options

You must be logged in to vote
3 replies
@jerrygzy
Comment options

@Ethosa
Comment options

Answer selected by jerrygzy
@jerrygzy
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants