summaryrefslogtreecommitdiffstats
path: root/posts/demo
diff options
context:
space:
mode:
Diffstat (limited to 'posts/demo')
-rw-r--r--posts/demo/SafeString.md34
-rw-r--r--posts/demo/demo.md27
-rw-r--r--posts/demo/execution-1.md (renamed from posts/demo/index-1.md)15
3 files changed, 59 insertions, 17 deletions
diff --git a/posts/demo/SafeString.md b/posts/demo/SafeString.md
new file mode 100644
index 0000000..a999814
--- /dev/null
+++ b/posts/demo/SafeString.md
@@ -0,0 +1,34 @@
+<!--_PAGEDATA
+{
+ "title": "HTML 转义配合 SafeString",
+ "githubissuesid": 2,
+ "keywords": "js,handlebars,javascript",
+ "description":" Handlebars 中使用 helper 输出链接",
+ "doc_text":"HTML 转义配合 SafeString",
+ "doc_link":"../index.html#SafeString",
+ "_template": "demo"
+}
+_PAGEDATA-->
+
+<!-- 模板 -->
+<script class="show" id="source" type="text/x-handlebars-template" >
+{{{link "<em>html</em>" "http://www.handlebarsjs.org"}}}
+</script>
+
+<!-- 数据 -->
+<script class="show json-format-error" id="data" type="text/json">
+
+</script>
+
+<!-- helper实现 -->
+<script class="show" id="helper">
+Handlebars.registerHelper('link', function(text, url) {
+ // 将下行 text = ... 注释以查看未作安全性转义导致的文字倾斜效果
+ text = Handlebars.Utils.escapeExpression(text);
+ url = Handlebars.Utils.escapeExpression(url);
+
+ var result = '<a href="' + url + '">' + text + '</a>';
+
+ return new Handlebars.SafeString(result);
+});
+</script> \ No newline at end of file
diff --git a/posts/demo/demo.md b/posts/demo/demo.md
index bbb8833..1c7f30f 100644
--- a/posts/demo/demo.md
+++ b/posts/demo/demo.md
@@ -12,26 +12,31 @@ _PAGEDATA-->
<!-- 模板 -->
<script class="show" id="source" type="text/x-handlebars-template" >
-{{{link "See more..." story.url}}}
+{{#nimo}}
+{{.}} {{root.a.b.c}}
+{{/nimo}}
</script>
<!-- 数据 -->
<script class="show json-format-error" id="data" type="text/json">
{
- "story": {
- "url": "http://www.handlebarsjs.org"
- }
+ "a":{
+ "b":{
+ "c":"cccc"
+ }
+ },
+ "nimo": [
+ 1
+ ,
+ 2
+ ]
}
</script>
<!-- helper实现 -->
<script class="show" id="helper">
-Handlebars.registerHelper('link', function(text, url) {
- url = Handlebars.escapeExpression(url);
- text = Handlebars.escapeExpression(text);
-
- return new Handlebars.SafeString(
- "<a href='" + url + "'>" + text + "</a>"
- );
+Handlebars.registerHelper('helper-name', function(obj) {
+ return 'output'
+
});
</script> \ No newline at end of file
diff --git a/posts/demo/index-1.md b/posts/demo/execution-1.md
index 51fd90f..6be175b 100644
--- a/posts/demo/index-1.md
+++ b/posts/demo/execution-1.md
@@ -1,6 +1,6 @@
<!--_PAGEDATA
{
- "title": "Handlebars 中文网:轻逻辑语义化的模板引擎",
+ "title": "Handlebars 执行渲染",
"github":"nimojs/handlebarsjs.org",
"githubissuesid": 1,
"createData": "2015-04-10",
@@ -30,16 +30,19 @@ var template = Handlebars.compile(source);
<script class="show">
// 数据
var data = {
- "title": "Handlebars 中文网",
- "body": "用 Handlebars 抛弃低效率的 HTML 拼接吧!"
+ title: "My New Post",
+ body: "This is my first post!"
}
// 渲染
$(function () {
var html = template(data);
$("#result,#dom").html(html);
-
- // 通过 pre 让 textarea 高度自适应,与 Handlebars 无关
- $("#expandingPre").html(Handlebars.escapeExpression(html));
})
</script>
+<style>
+#expandingPre{
+ height: 200px;
+}
+</style>
+