Skip to content

Commit f0c1135

Browse files
committed
Fully adaptive renderers in examples
1 parent fa7656e commit f0c1135

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

demos/schemas/simple-post.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"type": "object",
44
"properties": {
55
"title": {"type": "string"},
6-
"content": {"type": "string"}
6+
"content": {"type": "string"},
7+
"mood": {
8+
"enum": ["happy", "neutral", "grumpy"],
9+
"default": "neutral"
10+
}
711
},
812
"required": ["title", "content"]
913
}

php-site/index.html

+14-4
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ <h4>Easy to use</h4>
143143
.custom-content {
144144
padding: 0.4em;
145145
}
146-
.custom-unknown-key {
147-
font-weight: bold;
146+
.custom-othertitle {
148147
padding-left: 0.4em;
148+
font-weight: bold;
149+
font-size: 0.9em;
149150
}
150-
.custom-unknown {
151+
.custom-other {
151152
padding-left: 3em;
152153
}
153154
</style>
@@ -161,6 +162,15 @@ <h4>Customisable</h4>
161162
var result = '&lt;div class=&quot;custom-post&quot;&gt;';
162163
result += '&lt;div class=&quot;custom-title&quot;&gt;' + context.renderHtml(data.property('title')) + '&lt;/div&gt;';
163164
result += '&lt;div class=&quot;custom-content&quot;&gt;' + context.renderHtml(data.property('content')) + '&lt;/div&gt;';
165+
// Render any other documented properties in the schema
166+
data.properties(data.schemas().knownProperties(['title', 'content']), function (key, subData) {
167+
if (subData.defined() || !subData.readOnly()) {
168+
result += '&lt;div class=&quot;custom-othertitle&quot;&gt;' + Jsonary.escapeHtml(key) + ':&lt;/div&gt;';
169+
result += '&lt;div class=&quot;custom-other custom-other-' + Jsonary.escapeHtml(key) + '&quot;&gt;'
170+
result += context.renderHtml(subData);
171+
result += '&lt;/div&gt;'
172+
}
173+
});
164174
return result + '&lt;/div&gt;';
165175
},
166176
filter: function (data, schemas) {
@@ -192,7 +202,7 @@ <h4>Customisable</h4>
192202
targetElementEditable: 'example-target-embed-customise-editable'
193203
});</script>
194204
</div>
195-
<p>It's worth noting that this particular example would not adapt to changes in the schema. However, you can customise the appearance without losing flexibility, and it's actually pretty simple to do.</p>
205+
<p>It's worth noting that the renderer here dynamically adapts to properties in the schema. The "mood" property is defined in the schema, and the renderer displays it when needed.</p>
196206
<!--<p>See "<a href="flexible-renderers.html">Flexible Renderers</a>" for more details.</p>-->
197207
</div>
198208

test/json/schemas/site

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"title": "Website section",
2525
"type": "object",
2626
"properties": {
27-
"title": {"title": "Title"},
27+
"title": {"title": "Title", "type": "string"},
2828
"tabs": {
2929
"title": "Tabs",
3030
"oneOf": [

test/renderers/site.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ Jsonary.render.register({
77
result += context.renderHtml(subData);
88
result += '</div>';
99
});
10-
// Render other properties with class "site-section site-section-????"
10+
// Render other documented properties with class "site-section site-section-????"
1111
data.properties(data.schemas().knownProperties(['title', 'topContent']), function (key, subData) {
12-
result += '<div class="site-section site-section-' + Jsonary.escapeHtml(key) + '">';
13-
result += context.renderHtml(subData);
14-
result += '</div>';
12+
if (subData.defined() || !subData.readOnly()) {
13+
result += '<div class="site-section site-section-' + Jsonary.escapeHtml(key) + '">';
14+
result += context.renderHtml(subData);
15+
result += '</div>';
16+
}
1517
});
1618
return result + '</div>';
1719
},

0 commit comments

Comments
 (0)