Skip to content

Commit a4985b0

Browse files
committed
feat: supports rendering objects as key and value. If value is an object convert to string for rendering
1 parent 7367578 commit a4985b0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

demo/render.object.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
<body>
1212

1313
<div class="template" template_id="abc1">
14+
<div>{{data}}</div>
1415
<h1> {{data.collection}} {{data._id}}</h1>
1516
<div class="template" render-object="data" render-key="one">
16-
<h3>{{one}} : <b att="{{one.key}}, {{one.value}}">{{one}}</b> {{one}}</h3>
17+
<h3>{{one.key}} : {{one.value}}</h3>
1718
</div>
1819
<div class="template" render-array="data.current_org" render-key="current_org" value="{{current_org[]}}">
1920
<h3>{{current_org}}</h3>

src/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ const CoCreateRender = {
5757
if (variables) {
5858
variables.forEach((attr) => {
5959
let value = self.__getValue(data, attr);
60-
if (value && typeof(value) !== "object") {
60+
// if (value && typeof(value) !== "object") {
61+
if (value) {
62+
if (!Array.isArray(value) && typeof(value) == "object") {
63+
let str = '';
64+
for (const [key, val] of Object.entries(value)) {
65+
str += `${key}: ${val}\n`;
66+
}
67+
value = str
68+
}
69+
6170
isPass = true;
6271
inputValue = inputValue.replace(attr, value);
6372
}
@@ -83,7 +92,15 @@ const CoCreateRender = {
8392
const renderObject = template.getAttribute('render-object');
8493
if (renderObject)
8594
arrayData = data[renderObject];
86-
arrayData = Object.keys(arrayData);
95+
96+
let array = []
97+
for (const [key, value] of Object.entries(arrayData)) {
98+
array.push({key: key, value: value})
99+
}
100+
if (array.length > 0)
101+
arrayData = array
102+
103+
// arrayData = Object.entries(arrayData);
87104
type = renderObject || 'data'
88105
}
89106

0 commit comments

Comments
 (0)