-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.html
190 lines (149 loc) · 6.15 KB
/
examples.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Caramel.js - Use JavaScript variables inside the HTML</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" type="text/javascript"></script>
<script src="./object-examples.js" type="text/javascript"></script>
<script src="./caramel.js" type="text/javascript"></script>
<script>
$.get('https://reqres.in/api/users?page=2', function(response) {
apiExampleList = response;
});
</script>
</head>
<body>
<h1>Caramel.js</h1>
<p>
One of the biggest problems of using jQuery or pure JavaScript is that you can't iterate through objects, after a server response for example.
You can't even write the content of a JS variable in a readable way.
<br>Caramel is the solution to this problems. You can:
<ul>
<li>Iterate through arrays and print variables and objects inside the HTML.</li>
<li>Check for variable values and hide/show blocks of code if the condition is not true.</li>
<li>Load external HTML components (<i>requires a web server</i>).</li>
</ul>
<a href="https://github.com/molloeduardo/caramel-js" target="_blank">Learn more on GitHub</a>
</p>
<hr>
<h3>Simple variable</h3>
<h4>The following text comes from a simple variable</h4>
<p>{{ simpleVariable }}</p>
<hr>
<h3>Variable from nested object</h3>
<h4>The following text comes from a simple nested object</h4>
<p>{{ nestedObject.test.type }}</p>
<hr>
<h3>Variable from array</h3>
<h4>The following text comes from an array</h4>
<p>{{ arrayExample[2] }}</p>
<hr>
<h3>Variable inside HTML parameter</h3>
<h4>The URL inside the following link comes from a variable</h4>
<a href="{{ googleURL }}">Go to Google</a>
<hr>
<h3>Variable and text inside HTML parameter</h3>
<h4>The URL inside the following link comes from a variable combined with text</h4>
<a href="{{ googleURL }}?q=hello">Search on Google</a>
<hr>
<h3>If condition</h3>
<h4>The following text is visible because the condition is true.</h4>
<p cmIf="tryVariable.test === 1">Hello!</p>
<hr>
<h3>Array iteration</h3>
<h4>The following list comes from an array</h4>
<ul>
<li cmFor="arrayExample" cmItem="fruit">
<p>{{ fruit }}</p>
</li>
</ul>
<hr>
<h3>Array iteration with conditions</h3>
<h4>The following list comes from an array</h4>
<ul>
<li cmFor="arrayExample" cmItem="fruit">
<p>{{ fruit }}<b cmIf="fruit === 'Orange'">This is my favourite!</b></p>
</li>
</ul>
<hr>
<h3>Complex nested array iteration</h3>
<h4>The following list comes from a complex nested array of objects</h4>
<ul>
<li cmFor="complexObject.batters.batter" cmItem="item">
{{ item.type }}
</li>
</ul>
<hr>
<h3>Array iteration and get multiple values</h3>
<h4>The following list comes from an object with array of objects</h4>
<ul>
<li cmFor="objectWithArrayOfObjects.batter" cmItem="item">
{{ item.id }} - {{ item.type }}
</li>
</ul>
<hr>
<h3>Nested Array iteration<span style="color: #ee0000;"> (not working properly)</span></h3>
<h4>The following list comes from an object with nested array of objects</h4>
<ul>
<li cmFor="nestedArray" cmItem="parent">
{{ parent.title }}
<li cmFor="parent.items" cmItem="child">
{{ child.title }}
</li>
</li>
</ul>
<hr>
<h3>Check if an element is first or is last</h3>
<h4>Knowing if an array item is first or last</h4>
<ul>
<li cmFor="objectWithArrayOfObjects.batter" cmItem="item">
<p ifNotLast ifNotFirst>{{ item.type }}</p>
<p ifFirst>{{ item.type }} <b>THIS IS FIRST</b></p>
<p ifLast>{{ item.type }} <b>THIS IS LAST</b></p>
</li>
</ul>
<hr>
<h3>Check if an element is even or odd</h3>
<h4>Knowing if an array item is even or odd</h4>
<ul>
<li cmFor="arrayOfNumbers" cmItem="item">
<p ifEven ifNumber [color]="item">{{ item }}<b>THIS IS EVEN</b></p>
<p ifOdd ifNumber>{{ item }}<b>THIS IS ODD</b></p>
</li>
</ul>
<hr>
<h3>CSS attributes</h3>
<h4>This text color comes from a variable and is dynamic</h4>
<p [color]="textColor">Yeah! This is me!</p>
<hr>
<h3>Array with CSS attributes<span style="color: #ee0000;"> (not working properly)</span></h3>
<h4>This text colors come from an array of objects</h4>
<ul>
<li cmFor="cssArray" cmItem="item">
<p [color]="item.color">{{ item.text }}</p>
</li>
</ul>
<hr>
<h3>Templates</h3>
<h4>This form comes from a template</h4>
<div cm-template="inputs"></div>
<hr>
<h3>API request example</h3>
<h4>Finally, this is an example calling an API service and printing the result</h4>
<ul>
<li cmFor="apiExampleList.data" cmItem="item" api="true">
{{ item.first_name }}
</li>
</ul>
<hr>
<h3>Components<span style="color: #ffcc00;"> (testing)</span></h3>
<cm-component name="test"></cm-component>
</body>
</html>
<cm-template name="inputs">
<input type="text" placeholder="Test 1">
<input type="text" placeholder="Test 2">
<button>Submit</button>
</cm-template>