block.super not working?
svanimpe opened this issue · 8 comments
Snippet from a stencil using inheritance:
{% block title}{{ block.super }} - Home{% endblock %}
Related snippet from the parent stencil:
<title>{% block title %}Main Title{% endblock %}</title>
I'm trying to use {{ block.super }}
to insert Main Title
as a prefix before - Home
, but this does not work.
By "does not work" I mean I get a Cannot GET <my endpoint>
response from Kitura, but no errors show up in the log. If I remove the line using {{ block.super }}
from the stencil, everything works fine.
We have tests showing this works: https://github.com/kylef/Stencil/blob/master/Tests/StencilTests/fixtures/child-super.html#L2 https://github.com/kylef/Stencil/blob/master/Tests/StencilTests/InheritenceSpec.swift#L22-L25
Could you please share a some code example showing this problem? Or better, a failing test.
These are my full stencils:
base.stencil
:
<html lang="en">
<head>
<title>{% block title %}Around the table{% endblock %}</title>
</head>
<body>
<h1>Around the table</h1>
{% block content %}{% endblock %}
</body>
</html>
home.stencil
:
{% extends "base.stencil" %}
{% block title}{{ block.super }} - Home{% endblock %}
{% block content %}
{{ user.id }}
{{ user.name }}
{% endblock %}
In Kitura, I simply render home.stencil
with the appropriate context:
router.get("/site/home") {
request, response, next in
defer {
next()
}
let context = [
"user": [
"id": request.userProfile!.id,
"name": request.userProfile!.displayName
]
]
try response.render("home.stencil", context: context).end()
}
Is it possible this context overrides the one that includes block.super
?
I modified the Stencil tests to test this template and the tests pass.
diff --git a/Tests/StencilTests/InheritenceSpec.swift b/Tests/StencilTests/InheritenceSpec.swift
index 6b741ae..bd92523 100644
--- a/Tests/StencilTests/InheritenceSpec.swift
+++ b/Tests/StencilTests/InheritenceSpec.swift
@@ -9,19 +9,18 @@ func testInheritence() {
let loader = FileSystemLoader(paths: [path])
let environment = Environment(loader: loader)
- $0.it("can inherit from another template") {
- let template = try environment.loadTemplate(name: "child.html")
- try expect(try template.render()) == "Header\nChild"
- }
-
- $0.it("can inherit from another template inheriting from another template") {
- let template = try environment.loadTemplate(name: "child-child.html")
- try expect(try template.render()) == "Child Child Header\nChild"
- }
-
$0.it("can inherit from a template that calls a super block") {
+ let context = [
+ "user": [
+ "id": "some id",
+ "name": "some namel",
+ ],
+ ]
+
let template = try environment.loadTemplate(name: "child-super.html")
- try expect(try template.render()) == "Header\nChild Body"
+ let rendered = try template.render(context)
+
+ try expect(rendered.contains("Around the table - Home")).to.beTrue()
}
}
}
diff --git a/Tests/StencilTests/fixtures/base.html b/Tests/StencilTests/fixtures/base.html
index 7c74ae0..53a79ec 100644
--- a/Tests/StencilTests/fixtures/base.html
+++ b/Tests/StencilTests/fixtures/base.html
@@ -1,2 +1,12 @@
-{% block header %}Header{% endblock %}
-{% block body %}Body{% endblock %}
\ No newline at end of file
+<html lang="en">
+
+<head>
+ <title>{% block title %}Around the table{% endblock %}</title>
+</head>
+
+<body>
+ <h1>Around the table</h1>
+ {% block content %}{% endblock %}
+</body>
+
+</html>
diff --git a/Tests/StencilTests/fixtures/child-super.html b/Tests/StencilTests/fixtures/child-super.html
index c8964ee..6113026 100644
--- a/Tests/StencilTests/fixtures/child-super.html
+++ b/Tests/StencilTests/fixtures/child-super.html
@@ -1,3 +1,8 @@
{% extends "base.html" %}
-{% block body %}Child {{ block.super }}{% endblock %}
+{% block title %}{{ block.super }} - Home{% endblock %}
+
+{% block content %}
+{{ user.id }}
+{{ user.name }}
+{% endblock %}
Thanks! I will open an issue with Kitura then.
@svanimpe In your comment #107 (comment) you missed closing % in home.stencil
: {% block title}
.
It was printed in the Kitura log:
[2017-04-11T21:52:15.080+03:00] [ERROR] [RouterCreator.swift:206 create()] Failed to render template 'block' tag takes one argument, the block name
After adding the missing %, your example seem to be rendered properly for me.
I can't believe I missed that... I stared at that code for quite some time looking for syntax errors. facepalm
@vadimeisenbergibm How did you get that error in your log? Mine just reads
[HTTPServerRequest.swift:215 parsingCompleted()] HTTP request from=127.0.0.1; proto=http;
over and over again, but no error.
@svanimpe That's OK, I hope you enjoy Kitura :)
Strange, I do get the error in the log, I just used Log.logger = HeliumLogger()
Strange indeed. I was using HeliumLogger.use()
but changing that to your code didn't change much.