Skip to content

Commit 708dbc6

Browse files
author
David Krause (enthus1ast)
committed
Implement Template Fragment #82
Signed-off-by: David Krause (enthus1ast) <krause@biochem2.uni-frankfurt.de>
1 parent 64b91e8 commit 708dbc6

7 files changed

+52
-55
lines changed

nimja.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.9.1"
3+
version = "0.10.0"
44

55
author = "David Krause"
66
description = "typed and compiled template engine inspired by jinja2, twig and onionhammer/nim-templates for Nim."

readme.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ So a "users detail page" would render the whole thing.
806806
```
807807
tmplf("user.nimja", baseDir = getScriptDir())
808808
```
809-
809+
810810
But if you want to display a user somewhere else, you can just render the `user` block:
811811

812812
userlist.nimja
@@ -1216,10 +1216,9 @@ Changelog
12161216
- 0.?.?
12171217
- Added context to `importnimja`
12181218
## DONE
1219-
- 0.9.1
1220-
- Template fragments (good for htmx); Render specific blocks from a template
1221-
all procs (`tmpls`, `tmplf`, `compileTemplateString` and `compileTemplateFile`)
1222-
got a "blockToRender" parameter, when set, only the given block is rendered.
1219+
- 0.10.0
1220+
- Possible Breaking Change.
1221+
- Template fragments (good for htmx); Render specific blocks from a template all procs (`tmpls`, `tmplf`, `compileTemplateString` and `compileTemplateFile`) got a "blockToRender" parameter, when set, only the given block is rendered.
12231222
- 0.9.0
12241223
- BREAKING CHANGE!
12251224
- in order to fix #15 & #89 and to enable nimja components imported from other modules,

src/nimja/parser.nim

+17-17
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ proc compile(str: string, blockToRender: string = ""): seq[NwtNode] =
727727
blocks[nwtn.blockName] = nwtn.blockBody
728728
if blockToRender == "":
729729
var base = templateCache[0]
730-
# return fillBlocks(base).condenseStrings() # ast condense after blocks are filled # TODO remove
731730
result = fillBlocks(base).condenseStrings() # ast condense after blocks are filled
732731
else:
733732
if blocks.contains(blockToRender):
@@ -738,7 +737,6 @@ proc compile(str: string, blockToRender: string = ""): seq[NwtNode] =
738737
quit()
739738
# echo result
740739

741-
742740
proc generatePreallocatedStringDef(len: int): NimNode =
743741
# dumpAstGen:
744742
# when result is string:
@@ -810,13 +808,14 @@ template tmplsMacroImpl() =
810808
alias.add body
811809
result.add alias
812810

813-
macro compileTemplateStr*(str: typed, baseDir: static string = "", iter: static bool = false,
814-
varname: static string = "result", blockToRender: static string = "", context: untyped = nil): untyped =
811+
macro compileTemplateStr*(str: typed, baseDir: static string = "",
812+
blockToRender: static string = "", iter: static bool = false,
813+
varname: static string = "result", context: untyped = nil): untyped =
815814
## Compiles a Nimja template from a string.
816815
##
817816
## .. code-block:: Nim
818817
## proc yourFunc(yourParams: bool): string =
819-
## compileTemplateString("{%if yourParams%}TRUE{%endif%}")
818+
## compileTemplateStr("{%if yourParams%}TRUE{%endif%}")
820819
##
821820
## echo yourFunc(true)
822821
##
@@ -826,15 +825,15 @@ macro compileTemplateStr*(str: typed, baseDir: static string = "", iter: static
826825
##
827826
## .. code-block:: nim
828827
## iterator yourIter(yourParams: bool): string =
829-
## compileTemplateString("{%for idx in 0 .. 100%}{{idx}}{%endfor%}", iter = true)
828+
## compileTemplateStr("{%for idx in 0 .. 100%}{{idx}}{%endfor%}", iter = true)
830829
##
831830
## for elem in yourIter(true):
832831
## echo elem
833832
##
834833
## `varname` specifies the variable that is appended to.
835834
##
836835
##
837-
## A context can be supplied to the `compileTemplateString` (also `compileTemplateFile`), to override variable names:
836+
## A context can be supplied to the `compileTemplateStr` (also `compileTemplateFile`), to override variable names:
838837
##
839838
## .. code-block:: nim
840839
## block:
@@ -845,7 +844,7 @@ macro compileTemplateStr*(str: typed, baseDir: static string = "", iter: static
845844
## var rax = Rax(aa: "aaaa", bb: 13.37)
846845
## var foo = 123
847846
## proc render(): string =
848-
## compileTemplateString("{{node.bb}}{{baa}}", {node: rax, baa: foo})
847+
## compileTemplateStr("{{node.bb}}{{baa}}", {node: rax, baa: foo})
849848
##
850849

851850
# Please note, currently the context **cannot be** procs/funcs etc.
@@ -855,8 +854,9 @@ macro compileTemplateStr*(str: typed, baseDir: static string = "", iter: static
855854
tmplsMacroImpl()
856855
doCompile(str.strVal, blockToRender, result)
857856

858-
macro compileTemplateFile*(path: static string, baseDir: static string = "", iter: static bool = false,
859-
varname: static string = "result", blockToRender: static string = "", context: untyped = nil): untyped =
857+
macro compileTemplateFile*(path: static string, baseDir: static string = "",
858+
blockToRender: static string = "", iter: static bool = false,
859+
varname: static string = "result", context: untyped = nil): untyped =
860860
## Compiles a Nimja template from a file.
861861
##
862862
## .. code-block:: nim
@@ -878,7 +878,7 @@ macro compileTemplateFile*(path: static string, baseDir: static string = "", ite
878878
##
879879
## `varname` specifies the variable that is appended to.
880880
##
881-
## A context can be supplied to the `compileTemplateFile` (also `compileTemplateString`), to override variable names:
881+
## A context can be supplied to the `compileTemplateFile` (also `compileTemplateStr`), to override variable names:
882882
##
883883
## .. code-block:: nim
884884
## block:
@@ -898,9 +898,9 @@ macro compileTemplateFile*(path: static string, baseDir: static string = "", ite
898898
tmplsMacroImpl()
899899
doCompile(str, blockToRender, result)
900900

901-
template tmplsImpl(str: static string, baseDir: static string, blockToRender: static string = ""): string =
901+
template tmplsImpl(str: static string, baseDir: static string, blockToRender: static string): string =
902902
var nimjaTmplsVar: string
903-
compileTemplateStr(str, baseDir, varname = astToStr nimjaTmplsVar, blockToRender = blockToRender)
903+
compileTemplateStr(str, baseDir, blockToRender, varname = astToStr nimjaTmplsVar)
904904
nimjaTmplsVar
905905

906906
macro tmpls*(str: static string, baseDir: static string = "",
@@ -926,12 +926,12 @@ macro tmpls*(str: static string, baseDir: static string = "",
926926
result.add quote do:
927927
tmplsImpl(`str`, `baseDir`, `blockToRender`)
928928

929-
template tmplfImpl(path: static string, baseDir: static string = "",): string =
929+
template tmplfImpl(path: static string, baseDir: static string = "", blockToRender: static string = ""): string =
930930
var nimjaTmplfVar: string
931-
compileTemplateFile(path, baseDir, varname = astToStr nimjaTmplfVar)
931+
compileTemplateFile(path, baseDir, blockToRender, varname = astToStr nimjaTmplfVar)
932932
nimjaTmplfVar
933933

934-
macro tmplf*(str: static string, baseDir: static string = "", context: untyped = nil): string =
934+
macro tmplf*(str: static string, baseDir: static string = "", blockToRender: static string = "", context: untyped = nil): string =
935935
## Compiles a Nimja template file and returns directly.
936936
## Can be used inline, without a wrapper proc.
937937
##
@@ -950,4 +950,4 @@ macro tmplf*(str: static string, baseDir: static string = "", context: untyped =
950950
##
951951
tmplsMacroImpl()
952952
result.add quote do:
953-
tmplfImpl(`str`, `baseDir`)
953+
tmplfImpl(`str`, `baseDir`, `blockToRender`)

tests/basic/test_case.nim

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ suite "case":
2727
aaa, bbb, ccc, ddd
2828
var foo: Foo = aaa
2929
var isNothing: bool
30-
check "AAA" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: foo})
31-
check "BBB" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: Foo.bbb})
32-
check "CCC" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: ccc})
30+
check "AAA" == tmplf("case" / "case2.nimja", baseDir = getScriptDir(), context = {ee: foo})
31+
check "BBB" == tmplf("case" / "case2.nimja", baseDir = getScriptDir(), context = {ee: Foo.bbb})
32+
check "CCC" == tmplf("case" / "case2.nimja", baseDir = getScriptDir(), context = {ee: ccc})
3333

3434
isNothing = true
35-
check "nothing" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: ddd})
35+
check "nothing" == tmplf("case" / "case2.nimja", baseDir = getScriptDir(), context = {ee: ddd})
3636

3737
isNothing = false
38-
check "something" == tmplf("case" / "case2.nimja", getScriptDir(), {ee: ddd})
38+
check "something" == tmplf("case" / "case2.nimja", baseDir = getScriptDir(), context = {ee: ddd})
3939

4040

tests/basic/test_case_missing.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ include ../../src/nimja/parser
88
type Foo = enum
99
aaa, bbb, ccc, ddd
1010
var foo: Foo = aaa
11-
discard tmplf("case" / "case3.nimja", baseDir = getScriptDir(), {ee: ddd})
11+
discard tmplf("case" / "case3.nimja", baseDir = getScriptDir(), context = {ee: ddd})

tests/basic/test_fragments.nim

+21-23
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ suite "fragments":
1414
{% block second %}second block{% endblock %}
1515
BUG
1616
""", blockToRender = blockToRender)
17-
1817
check "first block" == foo("first")
1918
check "second block" == foo("second")
2019

20+
2121
test "compileTemplateStr with inner block":
2222
proc foo(blockToRender: static string): string =
2323
compileTemplateStr("""
@@ -28,6 +28,7 @@ suite "fragments":
2828
check "first inner block" == foo("first")
2929
check " inner " == foo("inner")
3030

31+
3132
test "compileTemplateStr simple with self":
3233
proc foo(blockToRender: static string): string =
3334
compileTemplateStr("""
@@ -37,7 +38,6 @@ suite "fragments":
3738
{% block inner %} inner {% endblock %}
3839
BUG
3940
""", blockToRender = blockToRender)
40-
4141
check "first inner block" == foo("first")
4242
check "second inner block" == foo("second")
4343

@@ -51,19 +51,17 @@ suite "fragments":
5151
{% block inner %} {{ii}} {% endblock %}
5252
BUG
5353
""", blockToRender = blockToRender)
54-
5554
check "first 1337 block" == foo(1337, "first")
5655
check "second 1337 block" == foo(1337, "second")
5756

5857

5958
test "compileTemplateFile simple":
6059
proc foo(fileToRender: static string, blockToRender: static string): string =
6160
compileTemplateFile(fileToRender, blockToRender = blockToRender, baseDir = getScriptDir())
62-
check "title from index" == foo("fragments/index.nimja", "title") ## TODO test this error message!
63-
check "content from index" == foo("fragments/index.nimja", "content") ## TODO test this error message!
64-
65-
check "title to replace" == foo("fragments/base.nimja", "title") ## TODO test this error message!
66-
check "content to replace" == foo("fragments/base.nimja", "content") ## TODO test this error message!
61+
check "title from index" == foo("fragments/index.nimja", "title")
62+
check "content from index" == foo("fragments/index.nimja", "content")
63+
check "title to replace" == foo("fragments/base.nimja", "title")
64+
check "content to replace" == foo("fragments/base.nimja", "content")
6765

6866

6967
test "importnimja simple":
@@ -73,26 +71,26 @@ suite "fragments":
7371
result = result.strip()
7472
check "title from index" == foo("fragments/index.nimja", "title")
7573
check "content from index" == foo("fragments/index.nimja", "content")
76-
7774
check "title to replace" == foo("fragments/base.nimja", "title")
7875
check "content to replace" == foo("fragments/base.nimja", "content")
7976

8077

81-
# test "tmpls":
82-
# check "title" == tmpls("bug{%block title%}title{%endblock%}bug", baseDir = getScriptDir(), blockToRender = "title")
83-
# echo tmpls("bug{%block title%}title{%endblock%}bug",
84-
# check "titleinner" == tmpls("bug{%block title%}title{%block inner%}inner{%endblock%}{%endblock%}bug",
85-
# baseDir = getScriptDir(), blockToRender = "title")
86-
# check "inner" == tmpls("bug{%block title%}title{%block inner%}inner{%endblock%}{%endblock%}bug",
87-
# baseDir = getScriptDir(), blockToRender = "inner")
78+
test "tmpls":
79+
check "title" == tmpls("bug{%block title%}title{%endblock%}bug",
80+
baseDir = getScriptDir(), blockToRender = "title")
81+
82+
check "titleinner" == tmpls("bug{%block title%}title{%block inner%}inner{%endblock%}{%endblock%}bug",
83+
baseDir = getScriptDir(), blockToRender = "title")
84+
85+
check "inner" == tmpls("bug{%block title%}title{%block inner%}inner{%endblock%}{%endblock%}bug",
86+
baseDir = getScriptDir(), blockToRender = "inner")
8887

89-
# check "title from index" == tmpls("{% importnimja fragments/index.nimja}", baseDir = getScriptDir() , blockToRender = "title")
90-
# check "content from index" == foo("fragments/index.nimja", "content")
91-
#
92-
# check "title to replace" == foo("fragments/base.nimja", "title")
93-
# check "content to replace" == foo("fragments/base.nimja", "content")
88+
test "tmplf":
89+
check "title from index" == tmplf("fragments/index.nimja", blockToRender = "title", baseDir = getScriptDir())
90+
check "content from index" == tmplf("fragments/index.nimja", blockToRender = "content", baseDir = getScriptDir())
91+
check "title to replace" == tmplf("fragments/base.nimja", blockToRender = "title", baseDir = getScriptDir())
92+
check "content to replace" == tmplf("fragments/base.nimja", blockToRender = "content", baseDir = getScriptDir())
93+
9494

95-
# test "tmplf":
96-
# discard
9795

9896
# suite "fragments stolen":

tests/basic/test_tmplf_with_context.nim

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ block:
1717
var ii = 123
1818
doAssert "idx: 123, aa: aaaa, nodes: aaaa, 13.37" ==
1919
tmplf(
20-
"tmplf_with_context.nimja", getScriptDir(),
21-
{
20+
"tmplf_with_context.nimja", baseDir = getScriptDir(),
21+
context = {
2222
idx: ii,
2323
aa: rax.aa,
2424
nodes: rax
@@ -32,4 +32,4 @@ block:
3232
bb: float
3333
var rax = Rax(aa: "aaaa", bb: 13.37)
3434
var foo = 123
35-
doAssert "13.37123" == tmpls("""{% if node.aa == "aaaa" %}{{node.bb}}{% endif %}{{baa}}""", getScriptDir(), {node: rax, baa: foo})
35+
doAssert "13.37123" == tmpls("""{% if node.aa == "aaaa" %}{{node.bb}}{% endif %}{{baa}}""", baseDir = getScriptDir(), context = {node: rax, baa: foo})

0 commit comments

Comments
 (0)