-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassing-css-classes-to-jinja2.html
391 lines (363 loc) · 17.6 KB
/
passing-css-classes-to-jinja2.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:og="http://ogp.me/ns#"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>Passing CSS classes when rendering Jinja2 template - Celttechie</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Open Graph tags -->
<meta property="og:type" content="article"/>
<meta property="og:title" content="Passing CSS classes when rendering Jinja2 template"/>
<meta property="og:url" content="http://celttechie.github.io/passing-css-classes-to-jinja2.html"/>
<meta property="og:description" content=""/>
<!-- Bootstrap -->
<link rel="stylesheet" href="http://celttechie.github.io/theme/css/bootstrap.min.css" type="text/css"/>
<link href="http://celttechie.github.io/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="http://celttechie.github.io/theme/css/pygments/native.css" rel="stylesheet">
<link rel="stylesheet" href="http://celttechie.github.io/theme/css/style.css" type="text/css"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="http://celttechie.github.io/" class="navbar-brand">
Celttechie </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><a href="http://celttechie.github.io/pages/resume.html">
Resume
</a></li>
<li class="active">
<a href="http://celttechie.github.io/category/notes.html">Notes</a>
</li>
<li >
<a href="http://celttechie.github.io/category/openstack.html">Openstack</a>
</li>
<li >
<a href="http://celttechie.github.io/category/posts.html">Posts</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="http://celttechie.github.io/archives.html"><i class="fa fa-th-list"></i><span class="icon-label">Archives</span></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="http://celttechie.github.io/passing-css-classes-to-jinja2.html"
rel="bookmark"
title="Permalink to Passing CSS classes when rendering Jinja2 template">
Passing CSS classes when rendering Jinja2 template
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2014-03-27T00:00:00"> Thu 27 March 2014</time>
</span>
<span class="label label-default">Tags</span>
<a href="http://celttechie.github.io/tag/jinja2.html">Jinja2</a>
/
<a href="http://celttechie.github.io/tag/html.html">HTML</a>
/
<a href="http://celttechie.github.io/tag/css.html">CSS</a>
</footer><!-- /.post-info --> </div>
</div>
<p>After reading the article I posted about <a href="/posts/dont-embed-bootstrap-in-html">here</a>, the way I was working with Jinja2 templates, HTML, and Markdown was beginning to bother me.<br />
I really want the "how it looks" to be in the CSS. If I can get the layout and everything else about how the site looks into CSS, then all the Jinja2 templating needs to do is specify what data I'm working with, and what classes should be applied.<br />
So here's what I'm thinking:</p>
<p>I'm already passing data fields and possibly forms to Jinja2, then telling Jinja when to use them in the HTML structure of the page. I'm also using Markdown, so I can write blog posts like this, then have a Jinja filter parse the output, with the result being HTML structure and data. (As an aside, I'm thinking about switching to Creole, but it will still parse to HTML structure.) The end result is always HTML(duh) and the HTML can specify CSS classes to use.</p>
<p>So when I pass data fields to Jinja, I can also pass variables that indicate what CSS classes I want to use.</p>
<p>For example:</p>
<p>when passing data to a template I can pass something like this:</p>
<pre>
context = {
"page": {"title":"Some Title", "subtitle":"Some SubTitle", "body":"Markdown TEXT"}
"form": comment_form,
"classes": {"page_div":"blog"}
}
...
return render_template('/page.html', **context)
</pre>
<p>Then in my Jinja2 template, the page_div gets inserted at the appropriate place:</p>
<pre>
<div class="{{ classes.page_div }}">
<h1 class="title">{{ page.title }}</h1>
<h3 class="subtitle">{{ page.subtitle}}</h3>
<p class="body">{{ page.body|markdown }}<p>
</div>
</pre>
<p>Of course that's a real basic example, but I think it'll work. I'll have to try it out.</p>
</div>
<!-- /.entry-content -->
</article>
</section>
</div>
<div class="col-sm-3 well well-sm" id="sidebar">
<aside>
<section>
<ul class="list-group list-group-flush">
<li class="list-group-item"><h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4>
<ul class="list-group" id="social">
<li class="list-group-item"><a href="http://www.linkedin.com/in/bjarrett"><i
class="fa fa-linkedin-square fa-lg"></i> LinkedIn
</a></li>
</ul>
</li>
<li class="list-group-item"><a href="http://celttechie.github.io/"><h4><i class="fa fa-tags fa-lg"></i><span class="icon-label">Tags</span></h4></a>
<ul class="list-group" id="tags">
<li class="list-group-item tag-1">
<a href="http://celttechie.github.io/tag/ubuntu.html">
Ubuntu
</a>
</li>
<li class="list-group-item tag-1">
<a href="http://celttechie.github.io/tag/.html">
</a>
</li>
<li class="list-group-item tag-1">
<a href="http://celttechie.github.io/tag/ubuntu-1404.html">
Ubuntu 14.04
</a>
</li>
<li class="list-group-item tag-1">
<a href="http://celttechie.github.io/tag/virtualization.html">
virtualization
</a>
</li>
<li class="list-group-item tag-2">
<a href="http://celttechie.github.io/tag/bootstrap.html">
Bootstrap
</a>
</li>
<li class="list-group-item tag-2">
<a href="http://celttechie.github.io/tag/html.html">
HTML
</a>
</li>
<li class="list-group-item tag-2">
<a href="http://celttechie.github.io/tag/xen.html">
Xen
</a>
</li>
<li class="list-group-item tag-2">
<a href="http://celttechie.github.io/tag/laptop.html">
laptop
</a>
</li>
<li class="list-group-item tag-2">
<a href="http://celttechie.github.io/tag/openstack.html">
OpenStack
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/automation.html">
automation
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/git.html">
git
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/mongodb.html">
MongoDB
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/install.html">
install
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/authorization.html">
authorization
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/mcrypt.html">
mcrypt
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/apache.html">
Apache
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/configuration.html">
configuration
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/htpasswd.html">
htpasswd
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/lab.html">
Lab
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/css.html">
CSS
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/jinja2.html">
Jinja2
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/php.html">
PHP
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/guest.html">
guest
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/pyramid.html">
Pyramid
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/packages.html">
packages
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/careerlog.html">
CareerLog
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/virtualbox.html">
VirtualBox
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/authentication.html">
authentication
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/xen-44.html">
Xen 4.4
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/icehouse.html">
Icehouse
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/apache-24.html">
Apache 2.4
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/testing.html">
testing
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/hyper-v.html">
Hyper-V
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/1404.html">
14.04
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/1310.html">
13.10
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/pv-grub.html">
PV-Grub
</a>
</li>
<li class="list-group-item tag-4">
<a href="http://celttechie.github.io/tag/ruckus.html">
Ruckus
</a>
</li>
</ul>
</li>
<li class="list-group-item"><h4><i class="fa fa-external-link-square fa-lg"></i><span class="icon-label">Links</span></h4>
<ul class="list-group" id="links">
<li class="list-group-item">
<a href="http://getpelican.com/" target="_blank">
Pelican
</a>
</li>
<li class="list-group-item">
<a href="http://python.org/" target="_blank">
Python.org
</a>
</li>
<li class="list-group-item">
<a href="http://jinja.pocoo.org/" target="_blank">
Jinja2
</a>
</li>
<li class="list-group-item">
<a href="http://brianjarrett.endlessknot.us" target="_blank">
Old website
</a>
</li>
</ul>
</li>
</ul>
</section>
</aside> </div>
</div>
</div>
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2014 Brian Jarrett
· Powered by <a href="https://github.com/DandyDev/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> </div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="//code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="http://celttechie.github.io/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="http://celttechie.github.io/theme/js/respond.min.js"></script>
</body>
</html>