From 4b21a120276fc433cf03a3e956c3879373560815 Mon Sep 17 00:00:00 2001 From: "David Krause (enthus1ast)" Date: Fri, 9 Aug 2024 16:39:26 +0200 Subject: [PATCH 1/5] allow nimja to import other nimja code fixes #15 fixes #89 --- nimja.nimble | 2 +- readme.md | 13 +++++- src/nimja/parser.nim | 45 +++++++++++-------- src/nimja/sharedhelper.nim | 3 +- tests/basic/test_block.nim | 6 +-- tests/basic/test_blockInBlock.nim | 6 +-- .../basic/test_blockRenderMasterDirectly.nim | 2 +- tests/basic/test_case.nim | 14 +++--- tests/basic/test_double_extend.nim | 30 +++++++++++-- tests/basic/test_module_import.nim | 28 ++++++++++++ tests/basic/test_nimjautils.nim | 2 +- tests/basic/test_proc_import.nim | 4 +- tests/basic/test_scope.nim | 12 ++--- tests/basic/test_self.nim | 2 +- tests/basic/test_tmplf_with_context.nim | 6 +-- tests/basic/test_tmpls_with_context.nim | 4 +- tests/basic/theModule/theFile.nimja | 1 + tests/basic/theModule/theModule.nim | 9 ++++ 18 files changed, 135 insertions(+), 54 deletions(-) create mode 100644 tests/basic/test_module_import.nim create mode 100644 tests/basic/theModule/theFile.nimja create mode 100644 tests/basic/theModule/theModule.nim diff --git a/nimja.nimble b/nimja.nimble index a1d428e..df1e349 100644 --- a/nimja.nimble +++ b/nimja.nimble @@ -1,6 +1,6 @@ # Package -version = "0.8.7" +version = "0.9.0" author = "David Krause" description = "typed and compiled template engine inspired by jinja2, twig and onionhammer/nim-templates for Nim." diff --git a/readme.md b/readme.md index 1a68ea2..b06f70a 100644 --- a/readme.md +++ b/readme.md @@ -1107,9 +1107,20 @@ Changelog ========= ## TODO -- 0.8.? +- 0.?.? - Added context to `importnimja` ## DONE +- 0.9.0 + - BREAKING CHANGE! + - in order to fix #15 & #89 and to enable nimja components imported from other modules, + all proc (`tmpls`, `tmplf`, `compileTemplateString` and `compileTemplateFile`) got an `baseDir` param: + ``` + compileTemplateStr("""{{importnimja "some/template.nimja"}}""", baseDir = getScriptDir()) + tmpls("""{{importnimja "some/template.nimja"}}""", baseDir = getScriptDir()) + compileTemplateFile("some/template.nimja", baseDir = getScriptDir()) + tmpls("some/template.nimja", baseDir = getScriptDir()) + ``` + The use of tmplf(getScriptDir() / "foo.nimja") is discourage. It could still work in some cirumstances though. - 0.8.7 - Removed unused `NImport`. - Error on uneven `when` blocks. diff --git a/src/nimja/parser.nim b/src/nimja/parser.nim index 5eb285f..cd59015 100644 --- a/src/nimja/parser.nim +++ b/src/nimja/parser.nim @@ -75,6 +75,7 @@ var cacheNwtNode {.compileTime.}: Table[string, seq[NwtNode]] ## a cache for ren var cacheNwtNodeFile {.compileTime.}: Table[Path, string] ## a cache for content of a path var nwtIter {.compileTime.} = false var nwtVarname {.compileTime.}: string +var nwtBaseDir {.compileTime.}: string var blocks {.compileTime.} : Table[string, seq[NwtNode]] var guessedStringLen {.compileTime.} = 0 @@ -292,8 +293,7 @@ converter singleNwtNodeToSeq(nwtNode: NwtNode): seq[NwtNode] = return @[nwtNode] proc importNimja(nodes: var seq[NwtNode], path: string) = - const basePath = getScriptDir() - var str = read(basePath / path) + var str = read(nwtBaseDir / path) nodes = compile(str) proc parseSecondStepOne(fsTokens: seq[FSNode], pos: var int): seq[NwtNode] = @@ -665,20 +665,20 @@ proc loadCacheFile(path: Path): string = ## The second time the same file should be read ## it is returned from the cache when defined(nwtCacheOff): - return read(path) + return read(nwtBaseDir / path) else: - if cacheNwtNodeFile.contains(path): - return cacheNwtNodeFile[path] + if cacheNwtNodeFile.contains(nwtBaseDir / path): + return cacheNwtNodeFile[nwtBaseDir / path] else: - cacheNwtNodeFile[path] = read(path) - return cacheNwtNodeFile[path] + cacheNwtNodeFile[nwtBaseDir / path] = read(nwtBaseDir / path) + return cacheNwtNodeFile[nwtBaseDir / path] proc extend(str: string, templateCache: var Deque[seq[NwtNode]]) = var secondsStepTokens = loadCache(str) let foundExtendAt = validExtend(secondsStepTokens) if foundExtendAt > -1: templateCache.addFirst secondsStepTokens - let ext = loadCacheFile(getScriptDir() / secondsStepTokens[foundExtendAt].extendsPath) + let ext = loadCacheFile(secondsStepTokens[foundExtendAt].extendsPath) extend(ext, templateCache) else: templateCache.addFirst secondsStepTokens @@ -784,7 +784,7 @@ template tmplsMacroImpl() = alias.add body result.add alias -macro compileTemplateStr*(str: typed, iter: static bool = false, +macro compileTemplateStr*(str: typed, baseDir: static string = "", iter: static bool = false, varname: static string = "result", context: untyped = nil): untyped = ## Compiles a Nimja template from a string. ## @@ -825,11 +825,13 @@ macro compileTemplateStr*(str: typed, iter: static bool = false, # Please note, currently the context **cannot be** procs/funcs etc. nwtVarname = varname nwtIter = iter + nwtBaseDir = baseDir + echo "inCompileTemplateStr: ", nwtBaseDir tmplsMacroImpl() doCompile(str.strVal, result) -macro compileTemplateFile*(path: static string, iter: static bool = false, - varname: static string = "result", context: untyped = nil): untyped = +macro compileTemplateFile*(path: static string, baseDir: static string = "", iter: static bool = false, + varname: static string = "result", context: untyped = nil): untyped = ## Compiles a Nimja template from a file. ## ## .. code-block:: nim @@ -866,16 +868,21 @@ macro compileTemplateFile*(path: static string, iter: static bool = false, ## nwtVarname = varname nwtIter = iter + nwtBaseDir = baseDir + echo "*****************************" + echo "nwtBaseDir: ", nwtBaseDir + # echo currentSourcePath() + echo "*****************************" let str = loadCacheFile(path) tmplsMacroImpl() doCompile(str, result) -template tmplsImpl(str: static string): string = +template tmplsImpl(str: static string, baseDir: static string): string = var nimjaTmplsVar: string - compileTemplateStr(str, varname = astToStr nimjaTmplsVar) + compileTemplateStr(str, baseDir, varname = astToStr nimjaTmplsVar) nimjaTmplsVar -macro tmpls*(str: static string, context: untyped = nil): string = +macro tmpls*(str: static string, baseDir: static string = "", context: untyped = nil): string = ## Compiles a Nimja template string and returns directly. ## Can be used inline, without a wrapper proc. ## @@ -895,14 +902,14 @@ macro tmpls*(str: static string, context: untyped = nil): string = ## tmplsMacroImpl() result.add quote do: - tmplsImpl(`str`) + tmplsImpl(`str`, `baseDir`) -template tmplfImpl(path: static string): string = +template tmplfImpl(path: static string, baseDir: static string = "",): string = var nimjaTmplfVar: string - compileTemplateFile(path, varname = astToStr nimjaTmplfVar) + compileTemplateFile(path, baseDir, varname = astToStr nimjaTmplfVar) nimjaTmplfVar -macro tmplf*(str: static string, context: untyped = nil): string = +macro tmplf*(str: static string, baseDir: static string = "", context: untyped = nil): string = ## Compiles a Nimja template file and returns directly. ## Can be used inline, without a wrapper proc. ## @@ -921,4 +928,4 @@ macro tmplf*(str: static string, context: untyped = nil): string = ## tmplsMacroImpl() result.add quote do: - tmplfImpl(`str`) + tmplfImpl(`str`, `baseDir`) diff --git a/src/nimja/sharedhelper.nim b/src/nimja/sharedhelper.nim index cbf6cbe..2233e16 100644 --- a/src/nimja/sharedhelper.nim +++ b/src/nimja/sharedhelper.nim @@ -9,9 +9,10 @@ template read*(path: untyped): untyped = staticRead(path) else: readFile(path) + template getScriptDir*(): string = ## Helper for staticRead. ## ## returns the absolute path to your project, on compile time. - getProjectPath() \ No newline at end of file + instantiationInfo(-1, true).filename.parentDir() \ No newline at end of file diff --git a/tests/basic/test_block.nim b/tests/basic/test_block.nim index 7fb8314..bde911d 100644 --- a/tests/basic/test_block.nim +++ b/tests/basic/test_block.nim @@ -7,10 +7,10 @@ import unittest block: ## Test compileTemplateFile proc index(title: auto, body: auto): string = - compileTemplateFile("../templates/blockIndex.nimja") + compileTemplateFile("../templates/blockIndex.nimja", baseDir = getScriptDir()) proc master(): string = - compileTemplateFile("../templates/blockMaster.nimja") + compileTemplateFile("../templates/blockMaster.nimja", baseDir = getScriptDir()) check index("title", "FOO") == "titleFOO" check index("", "") == "" @@ -23,4 +23,4 @@ block: compileTemplateStr("""{%extends "../templates/blockMaster.nimja"%}{%block mytitle%}{{title}}{%endblock%}{%block mybody%}{{body}}{%endblock%}""") check index("title", "FOO") == "titleFOO" check index("", "") == "" - check index(1, 2) == "12" \ No newline at end of file + check index(1, 2) == "12" diff --git a/tests/basic/test_blockInBlock.nim b/tests/basic/test_blockInBlock.nim index b45d6bc..ee61fe8 100644 --- a/tests/basic/test_blockInBlock.nim +++ b/tests/basic/test_blockInBlock.nim @@ -7,11 +7,11 @@ import unittest test "one block": block: proc child1(): string = - compileTemplateStr("{% extends test_block_in_block/master.nimja %}{%block inner%}newinner{%endblock%}") + compileTemplateStr("{% extends test_block_in_block/master.nimja %}{%block inner%}newinner{%endblock%}", baseDir = getScriptDir()) check child1() == "outer1newinnerouter2" test "two blocks": block: proc child2(): string = - compileTemplateStr("{% extends test_block_in_block/master.nimja %}{%block outer%}newouter{%endblock%}") - check child2() == "newouter" \ No newline at end of file + compileTemplateStr("{% extends test_block_in_block/master.nimja %}{%block outer%}newouter{%endblock%}", baseDir = getScriptDir()) + check child2() == "newouter" diff --git a/tests/basic/test_blockRenderMasterDirectly.nim b/tests/basic/test_blockRenderMasterDirectly.nim index e84d48c..74a3b2a 100644 --- a/tests/basic/test_blockRenderMasterDirectly.nim +++ b/tests/basic/test_blockRenderMasterDirectly.nim @@ -5,6 +5,6 @@ import ../../src/nimja block: ## Render Master directly proc master(): string = - compileTemplateFile("../templates/blockMasterWithContent.nimja") + compileTemplateFile("../templates/blockMasterWithContent.nimja", baseDir = getScriptDir()) echo master() assert master() == """TITLEBODY""" diff --git a/tests/basic/test_case.nim b/tests/basic/test_case.nim index 26386d0..16c4f1c 100644 --- a/tests/basic/test_case.nim +++ b/tests/basic/test_case.nim @@ -8,10 +8,10 @@ import os suite "case": test "basic test": var str = "foo" - check "foo" == tmplf(getScriptDir() / "case" / "case.nimja") + check "foo" == tmplf("case" / "case.nimja", baseDir = getScriptDir()) str = "baa" - check "baa" == tmplf(getScriptDir() / "case" / "case.nimja") + check "baa" == tmplf(getScriptDir() / "case" / "case.nimja") # getScriptDir works in THAT case! str = "baz" check "baz" == tmplf(getScriptDir() / "case" / "case.nimja") @@ -27,14 +27,14 @@ suite "case": aaa, bbb, ccc, ddd var foo: Foo = aaa var isNothing: bool - check "AAA" == tmplf(getScriptDir() / "case" / "case2.nimja", {ee: foo}) - check "BBB" == tmplf(getScriptDir() / "case" / "case2.nimja", {ee: Foo.bbb}) - check "CCC" == tmplf(getScriptDir() / "case" / "case2.nimja", {ee: ccc}) + check "AAA" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: foo}) + check "BBB" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: Foo.bbb}) + check "CCC" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: ccc}) isNothing = true - check "nothing" == tmplf(getScriptDir() / "case" / "case2.nimja", {ee: ddd}) + check "nothing" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: ddd}) isNothing = false - check "something" == tmplf(getScriptDir() / "case" / "case2.nimja", {ee: ddd}) + check "something" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: ddd}) diff --git a/tests/basic/test_double_extend.nim b/tests/basic/test_double_extend.nim index 6a56d6d..188d3a8 100644 --- a/tests/basic/test_double_extend.nim +++ b/tests/basic/test_double_extend.nim @@ -6,12 +6,36 @@ discard """ include ../../src/nimja/parser # import unittest +# echo "#############################" +# echo "getScriptDir: :", getScriptDir() +# echo "getScriptDirProc: :", getScriptDirProc() +# echo "getScriptDirMacro: :", getScriptDirMacro() +# echo "getScriptDirII: :", getScriptDirII() +# echo "getScriptDirIItemplate :", getScriptDirIItemplate() +# echo "getScriptDirIItemplate2 :", getScriptDirIItemplate2() +# echo "currentSourcePath().parentDir() user:", currentSourcePath().parentDir() +# template ii(): string = +# instantiationInfo(-1, true).filename.parentDir() +# echo "instantiationInfo(-1, true).filename:", ii +# echo "#############################" + # proc inner(): string = -# compileTemplateFile(getScriptDir() / "doubleExtends" / "inner.nimja") +# # compileTemplateFile("doubleExtends" / "inner.nimja", baseDir = getScriptDir()) +# # compileTemplateFile("doubleExtends" / "inner.nimja", baseDir = "") #, baseDir = currentSourcePath().parentDir()) +# compileTemplateFile(getScriptDir() / "doubleExtends" / "inner.nimja", baseDir = "") #, baseDir = currentSourcePath().parentDir()) # echo inner() + proc outer(): string = - compileTemplateFile(getScriptDir() / "doubleExtends" / "outer.nimja") + echo "#############################" + echo currentSourcePath() + echo currentSourcePath().parentDir() + echo "#############################" + const baseDir = currentSourcePath().parentDir() + # compileTemplateFile("doubleExtends" / "outer.nimja", baseDir = baseDir) + # echo "GSCD: ", getScriptDir() + compileTemplateFile("doubleExtends" / "outer.nimja", baseDir = getScriptDir()) + # compileTemplateFile("doubleExtends" / "outer.nimja") assert outer() == "baseouterouterbase" # proc base(): string = @@ -23,4 +47,4 @@ assert outer() == "baseouterouterbase" # check "baseouterinnerouterbase" == inner() # check "baseouterouterbase" == outer() -# check "basebase" == base() \ No newline at end of file +# check "basebase" == base() diff --git a/tests/basic/test_module_import.nim b/tests/basic/test_module_import.nim new file mode 100644 index 0000000..66eb451 --- /dev/null +++ b/tests/basic/test_module_import.nim @@ -0,0 +1,28 @@ +discard """ + joinable: false +""" +import ../../src/nimja +import unittest +import strutils + +import theModule/theModule + +proc baseRender(): string = + return moduleRender() + +proc baseRender2(): string = + compileTemplateStr("{{moduleRender()}}") + +proc baseRender3(): string = + # compileTemplateFile() + tmpls("{{moduleRender()}}") + +suite "import module": + test "compileTemplateFile (from module)": + check "FROM THE FILE" == baseRender().strip() # why need strip? + + test "compileTemplateFile (from ctf -> module)": + check "FROM THE FILE" == baseRender2().strip() # why need strip? + + test "tmpls (from ctf -> module)": + check "FROM THE FILE" == baseRender3().strip() # why need strip? diff --git a/tests/basic/test_nimjautils.nim b/tests/basic/test_nimjautils.nim index 6e7b2b1..6360fa9 100644 --- a/tests/basic/test_nimjautils.nim +++ b/tests/basic/test_nimjautils.nim @@ -34,7 +34,7 @@ suite "nimjautils": test "includeRaw": proc test(): string = - let path = (getScriptDir() / "basic" / "includeRawT.txt") + let path = (getScriptDir() / "includeRawT.txt") compileTemplateStr("""pre{{ includeRaw(path) }}suf""") check test() == "pre123suf" diff --git a/tests/basic/test_proc_import.nim b/tests/basic/test_proc_import.nim index bd43cd8..2076f3c 100644 --- a/tests/basic/test_proc_import.nim +++ b/tests/basic/test_proc_import.nim @@ -8,7 +8,7 @@ suite "proc_import": test "basic": proc test(): string = - compileTemplateStr("""{% importnimja "procs.nimja" %}{{foo()}}""") + compileTemplateStr("""{% importnimja "procs.nimja" %}{{foo()}}""", baseDir = getScriptDir()) check test() == "foo" test "import on child (in block)": @@ -19,5 +19,5 @@ suite "proc_import": {%- importnimja "procs.nimja" -%} {{- foo() -}} {%- endblock -%} - """) + """, baseDir = getScriptDir()) check test() == "foo" diff --git a/tests/basic/test_scope.nim b/tests/basic/test_scope.nim index 64efe99..c76d3c8 100644 --- a/tests/basic/test_scope.nim +++ b/tests/basic/test_scope.nim @@ -10,14 +10,14 @@ import strutils suite "scope": test "1": - check "POSTFOO" == tmpls """ + check "POSTFOO" == tmpls(""" {%- scope -%} {% let httpMethod = "POST" %} {{- httpMethod -}} {%- endscope -%} {%- let httpMethod = "FOO" -%} {{- httpMethod -}} - """ + """) test "2 named": # we break out of a scope prematurely check "foo" == tmpls """ @@ -39,10 +39,10 @@ suite "scope": {%- endscope -%} """ test "4 scope with import": - check "foo" == tmpls """ + check "foo" == tmpls(""" {%- scope foo -%} {%- importnimja "foo.nimja" -%} {%- endscope -%} - """ - - \ No newline at end of file + """, getScriptDir()) # <---- this is required here!! + + diff --git a/tests/basic/test_self.nim b/tests/basic/test_self.nim index c0e1f08..f34d733 100644 --- a/tests/basic/test_self.nim +++ b/tests/basic/test_self.nim @@ -11,6 +11,6 @@ doAssert simple() == "titletitle" func ex(): string = - compileTemplateFile(getScriptDir() / "self_multiblock.nimja") + compileTemplateFile("self_multiblock.nimja", baseDir = getScriptDir()) doAssert ex() == "titletitle\13\10" # TODO why \13\10 ? diff --git a/tests/basic/test_tmplf_with_context.nim b/tests/basic/test_tmplf_with_context.nim index 84e6c12..f172a66 100644 --- a/tests/basic/test_tmplf_with_context.nim +++ b/tests/basic/test_tmplf_with_context.nim @@ -6,7 +6,7 @@ import os block: # test tmplf without context - doAssert "foo" == tmplf(getScriptDir() / "foo.nimja") + doAssert "foo" == tmplf("foo.nimja", baseDir = getScriptDir()) block: type @@ -17,7 +17,7 @@ block: var ii = 123 doAssert "idx: 123, aa: aaaa, nodes: aaaa, 13.37" == tmplf( - getScriptDir() / "tmplf_with_context.nimja", + "tmplf_with_context.nimja", getScriptDir(), { idx: ii, aa: rax.aa, @@ -32,4 +32,4 @@ block: bb: float var rax = Rax(aa: "aaaa", bb: 13.37) var foo = 123 - doAssert "13.37123" == tmpls("""{% if node.aa == "aaaa" %}{{node.bb}}{% endif %}{{baa}}""", {node: rax, baa: foo}) \ No newline at end of file + doAssert "13.37123" == tmpls("""{% if node.aa == "aaaa" %}{{node.bb}}{% endif %}{{baa}}""", getScriptDir(), {node: rax, baa: foo}) diff --git a/tests/basic/test_tmpls_with_context.nim b/tests/basic/test_tmpls_with_context.nim index 593b58f..eab2fc1 100644 --- a/tests/basic/test_tmpls_with_context.nim +++ b/tests/basic/test_tmpls_with_context.nim @@ -53,7 +53,7 @@ block: proc render(): string = compileTemplateStr("asdf {{node.bb}} {{ss}}", context = {node: rax, ss: rax.aa}) doAssert "asdf 13.37 aaaa" == render() - doAssert "42.0" == tmpls("""{% if node.aa == "aaaa" %}{%node.bb = 42.0%}{% endif %}{{node.bb}}""", {node: rax, baa: foo}) + doAssert "42.0" == tmpls("""{% if node.aa == "aaaa" %}{%node.bb = 42.0%}{% endif %}{{node.bb}}""", context = {node: rax, baa: foo}) doAssert 42.0 == rax.bb block: @@ -94,4 +94,4 @@ block: # # block: # # test if context can contain procs/funcs # # proc foo(ii: int): string = return "foo" & $ii -# # doAssert "foo321" == tmpls("{{baa(321)}}", baa = foo(123)) \ No newline at end of file +# # doAssert "foo321" == tmpls("{{baa(321)}}", baa = foo(123)) diff --git a/tests/basic/theModule/theFile.nimja b/tests/basic/theModule/theFile.nimja new file mode 100644 index 0000000..2ff6a6f --- /dev/null +++ b/tests/basic/theModule/theFile.nimja @@ -0,0 +1 @@ +FROM THE FILE diff --git a/tests/basic/theModule/theModule.nim b/tests/basic/theModule/theModule.nim new file mode 100644 index 0000000..1f1e7b5 --- /dev/null +++ b/tests/basic/theModule/theModule.nim @@ -0,0 +1,9 @@ +import ../../../src/nimja + + +proc moduleRender*(): string = + compileTemplateFile("theFile.nimja", getScriptDir()) + + +when isMainModule: + echo moduleRender() From c74a6e64c6fc6953a675172af228cf1e84966001 Mon Sep 17 00:00:00 2001 From: "David Krause (enthus1ast)" Date: Fri, 9 Aug 2024 16:52:30 +0200 Subject: [PATCH 2/5] make last tests green --- examples/prologue/serverJester.nim | 6 +++--- examples/prologue/serverPrologue.nim | 6 +++--- tests/basic/test_case_missing.nim | 2 +- tests/bugs/test_nwt_bug4_debug.nim | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/prologue/serverJester.nim b/examples/prologue/serverJester.nim index 1b5ce82..9193fc4 100644 --- a/examples/prologue/serverJester.nim +++ b/examples/prologue/serverJester.nim @@ -17,15 +17,15 @@ const users = @[ ] proc renderIndex(title: string, users: seq[User]): string = - compileTemplateFile(getScriptDir() / "index.nimja") + compileTemplateFile("index.nimja", baseDir = getScriptDir()) proc renderUser(title: string, idx: int, users: seq[User]): string = let user = users[idx] - compileTemplateFile(getScriptDir() / "user.nimja") + compileTemplateFile("user.nimja", baseDir = getScriptDir()) proc renderError(title: auto, code: HttpCode, users: seq[User]): string = ## title is `auto` here; nim generics work as well! - compileTemplateFile(getScriptDir() / "error.nimja") + compileTemplateFile("error.nimja", baseDir = getScriptDir()) routes: get "/": diff --git a/examples/prologue/serverPrologue.nim b/examples/prologue/serverPrologue.nim index 488dbe8..2682e0b 100644 --- a/examples/prologue/serverPrologue.nim +++ b/examples/prologue/serverPrologue.nim @@ -23,15 +23,15 @@ method extend(ctx: Context) = ] proc renderIndex(title: string, users: seq[User]): string = - compileTemplateFile(getScriptDir() / "index.nimja") + compileTemplateFile("index.nimja", baseDir = getScriptDir()) proc renderUser(title: string, idx: int, users: seq[User]): string = let user = users[idx] - compileTemplateFile(getScriptDir() / "user.nimja") + compileTemplateFile("user.nimja", baseDir = getScriptDir()) proc renderError(title: auto, code: HttpCode, users: seq[User]): string = ## title is `auto` here; nim generics work as well! - compileTemplateFile(getScriptDir() / "error.nimja") + compileTemplateFile("error.nimja", baseDir = getScriptDir()) proc hello*(ctx: Context) {.async.} = resp renderIndex("someTitle", UserContext(ctx).users) diff --git a/tests/basic/test_case_missing.nim b/tests/basic/test_case_missing.nim index 1cdce20..c335386 100644 --- a/tests/basic/test_case_missing.nim +++ b/tests/basic/test_case_missing.nim @@ -8,4 +8,4 @@ include ../../src/nimja/parser type Foo = enum aaa, bbb, ccc, ddd var foo: Foo = aaa -discard tmplf(getScriptDir() / "case" / "case3.nimja", {ee: ddd}) +discard tmplf("case" / "case3.nimja", baseDir = getScriptDir(), {ee: ddd}) diff --git a/tests/bugs/test_nwt_bug4_debug.nim b/tests/bugs/test_nwt_bug4_debug.nim index b98685c..16c45cf 100644 --- a/tests/bugs/test_nwt_bug4_debug.nim +++ b/tests/bugs/test_nwt_bug4_debug.nim @@ -5,7 +5,7 @@ import ../../src/nimja import base64, os proc foo(key1 = "one", key2 = "two"): string = - compileTemplateFile(getScriptDir() / "test_nwt_bug4_child.nimja") + compileTemplateFile("test_nwt_bug4_child.nimja", baseDir = getScriptDir()) const good = "PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ImVuIj4KPGhlYWQ+CiAgICA8bWV0YSBjaGFyc2V0PSJVVEYtOCI+CiAgICA8dGl0bGU+VGl0bGU8L3RpdGxlPgogICAgCmhlYWQhCm9uZSB0d28KCjwvaGVhZD4KPGJvZHk+Cgpjb250ZW50IQpvbmUgdHdvCgo8L2JvZHk+CjwvaHRtbD4=" assert foo() == good.decode() From 657ea668cdc2174b65e1ed250f8733c5d7531777 Mon Sep 17 00:00:00 2001 From: "David Krause (enthus1ast)" Date: Fri, 9 Aug 2024 16:57:20 +0200 Subject: [PATCH 3/5] make last tests green :) --- .gitignore | 4 +++- examples/fromReadme/server.nim | 2 +- readme.md | 2 +- src/nimja/parser.nim | 4 ---- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 95be004..476f601 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,6 @@ tests/basic/test_if tests/basic/test_if_complex tests/basic/test_insert tests/basic/test_simple -tests/basic/test_while \ No newline at end of file +tests/basic/test_while +src/nimja/parser +src/nimja/sharedhelper diff --git a/examples/fromReadme/server.nim b/examples/fromReadme/server.nim index 6e38cc7..a09798d 100644 --- a/examples/fromReadme/server.nim +++ b/examples/fromReadme/server.nim @@ -19,7 +19,7 @@ proc renderIndex(title: string, users: seq[User]): string = ## so it can access all variables like `title` and `users` ## the return variable could be `string` or `Rope` or ## anything which has a `&=`(obj: YourObj, str: string) proc. - compileTemplateFile(getScriptDir() / "index.nimja") + compileTemplateFile("index.nimja", baseDir = getScriptDir()) proc main {.async.} = var server = newAsyncHttpServer() diff --git a/readme.md b/readme.md index b06f70a..b3dee72 100644 --- a/readme.md +++ b/readme.md @@ -1120,7 +1120,7 @@ Changelog compileTemplateFile("some/template.nimja", baseDir = getScriptDir()) tmpls("some/template.nimja", baseDir = getScriptDir()) ``` - The use of tmplf(getScriptDir() / "foo.nimja") is discourage. It could still work in some cirumstances though. + The use of `tmplf(getScriptDir() / "foo.nimja")` is discourage. It could still work in some cirumstances though. - 0.8.7 - Removed unused `NImport`. - Error on uneven `when` blocks. diff --git a/src/nimja/parser.nim b/src/nimja/parser.nim index cd59015..d33a680 100644 --- a/src/nimja/parser.nim +++ b/src/nimja/parser.nim @@ -869,10 +869,6 @@ macro compileTemplateFile*(path: static string, baseDir: static string = "", ite nwtVarname = varname nwtIter = iter nwtBaseDir = baseDir - echo "*****************************" - echo "nwtBaseDir: ", nwtBaseDir - # echo currentSourcePath() - echo "*****************************" let str = loadCacheFile(path) tmplsMacroImpl() doCompile(str, result) From 5241a697bc19f6708c28cf1053a0d1f0340a7b83 Mon Sep 17 00:00:00 2001 From: "David Krause (enthus1ast)" Date: Fri, 9 Aug 2024 17:02:02 +0200 Subject: [PATCH 4/5] fix readme --- readme.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index b3dee72..5f143de 100644 --- a/readme.md +++ b/readme.md @@ -68,7 +68,7 @@ proc renderIndex(title: string, users: seq[User]): string = ## so it can access all variables like `title` and `users` ## the return variable could be `string` or `Rope` or ## anything which has a `&=`(obj: YourObj, str: string) proc. - compileTemplateFile(getScriptDir() / "index.nimja") + compileTemplateFile("index.nimja", baseDir = getScriptDir()) proc main {.async.} = var server = newAsyncHttpServer() @@ -258,7 +258,7 @@ you should use it like so: ```nim import os # for `/` proc myRenderProc(someParam: string): string = - compileTemplateFile(getScriptDir() / "myFile.html") + compileTemplateFile("myFile.html", baseDir = getScriptDir()) echo myRenderProc("test123") ``` @@ -379,7 +379,7 @@ situations where you want to inline a render call. ```nim let leet = 1337 echo tmpls("foo {{leet}}") -echo tmplf(getScriptDir() / "templates" / "myfile.nimja") +echo tmplf("templates" / "myfile.nimja", baseDir = getScriptDir()) ``` A context can be supplied to the template, to override variable names: @@ -525,7 +525,7 @@ if the child.nimja is compiled then rendered like so: ```nim proc renderChild(): string = - compileTemplateFile(getScriptDir() / "child.nimja") + compileTemplateFile("child.nimja", baseDir = getScriptDir()) echo renderChild() ``` @@ -780,7 +780,7 @@ Good for documentation etc.. ```nim proc test(): string = - let path = (getScriptDir() / "tests/basic" / "includeRawT.txt") + let path = ("tests/basic" / "includeRawT.txt", baseDir = getScriptDir()) compileTemplateStr("""pre{{ includeRaw(path) }}suf""") ``` @@ -1012,10 +1012,10 @@ import os # for `/` proc index*(): string {.exportc, dynlib.} = var foos = 1351 # change me i'm dynamic :) - compileTemplateFile(getScriptDir() / "templates/index.nimja") + compileTemplateFile("templates/index.nimja", baseDir = getScriptDir()) proc detail*(id: string): string {.exportc, dynlib.} = - compileTemplateFile(getScriptDir() / "templates/detail.nimja") + compileTemplateFile("templates/detail.nimja", baseDir = getScriptDir()) ``` From 62ad3f003f1044cb350d87e5524f235ce77b1725 Mon Sep 17 00:00:00 2001 From: "David Krause (enthus1ast)" Date: Fri, 9 Aug 2024 23:10:14 +0200 Subject: [PATCH 5/5] readme baseDir --- .gitignore | 31 +++++++++++++++++++++++++++++++ readme.md | 11 +++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 476f601..e25f129 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,34 @@ tests/basic/test_simple tests/basic/test_while src/nimja/parser src/nimja/sharedhelper +tests/basic/NOT_READY_test_raw +tests/basic/test_endless_iterator +tests/basic/test_file_foo_html +tests/basic/test_iterator +tests/basic/test_lexer +tests/basic/test_module_import +tests/basic/test_nimjautils +tests/basic/test_proc +tests/basic/test_proc_import +tests/basic/test_scope +tests/basic/test_self +tests/basic/test_shorhands +tests/basic/test_strparse +tests/basic/test_tmplf_with_context +tests/basic/test_tmpls_with_context +tests/basic/test_tokens_before_extend +tests/basic/test_triple +tests/basic/test_unicode +tests/basic/test_when +tests/basic/test_whitespacecontrol +tests/basic/test_blockInBlock +tests/basic/test_blockNotExtendedMaster +tests/basic/test_blockRenderMasterDirectly +tests/basic/test_case +tests/basic/test_condense_strings +tests/basic/test_double_extend +tests/basic/test_doublicated_blocks +tests/basic/theModule/theModule +tests/bugs/test_nwt_bug4_debug +examples/prologue/serverPrologue +examples/prologue/serverJester diff --git a/readme.md b/readme.md index 5f143de..ad9a7d2 100644 --- a/readme.md +++ b/readme.md @@ -474,6 +474,9 @@ template and therefore can be included. This way you create reusable template blocks to use all over your webpage. +(Since Nimja 0.9.0) If you import other templates, make sure to use the `baseDir` param with +`tmpls`, `tmplf`, `compileTemplateString` and `compileTemplateFile`. + partials/_user.nimja: ```twig
@@ -501,6 +504,8 @@ a child template can extend a master template. So that placeholder blocks in the master are filled with content from the child. +(Since Nimja 0.9.0) If you extend other templates, make sure to use the `baseDir` param with +`tmpls`, `tmplf`, `compileTemplateString` and `compileTemplateFile`. partials/_master.nimja ```twig @@ -1113,14 +1118,16 @@ Changelog - 0.9.0 - BREAKING CHANGE! - in order to fix #15 & #89 and to enable nimja components imported from other modules, - all proc (`tmpls`, `tmplf`, `compileTemplateString` and `compileTemplateFile`) got an `baseDir` param: + all proc (`tmpls`, `tmplf`, `compileTemplateString` and `compileTemplateFile`) got a `baseDir` param: ``` compileTemplateStr("""{{importnimja "some/template.nimja"}}""", baseDir = getScriptDir()) tmpls("""{{importnimja "some/template.nimja"}}""", baseDir = getScriptDir()) compileTemplateFile("some/template.nimja", baseDir = getScriptDir()) tmpls("some/template.nimja", baseDir = getScriptDir()) ``` - The use of `tmplf(getScriptDir() / "foo.nimja")` is discourage. It could still work in some cirumstances though. + The use of `tmplf(getScriptDir() / "foo.nimja")` is discourage, use `tmplf("foo.nimja", baseDir = getScriptDir())` instead. + The old way could still work in some cirumstances though. + But its neccesary if you plan to import your nimja template code into other code. - 0.8.7 - Removed unused `NImport`. - Error on uneven `when` blocks.