-
Notifications
You must be signed in to change notification settings - Fork 219
/
README.de.html
2456 lines (1933 loc) · 79.8 KB
/
README.de.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<div class='toc'>
<ol class='level-1'>
<li><a href='#Routen'>Routen</a></li>
<ol class='level-2'>
<li><a href='#Bedingungen'>Bedingungen</a></li>
<li><a href='#R%C3%BCckgabewerte'>Rückgabewerte</a></li>
<li><a href='#Eigene%20Routen-Muster'>Eigene Routen-Muster</a></li>
</ol>
<li><a href='#Statische%20Dateien'>Statische Dateien</a></li>
<li><a href='#Views/Templates'>Views/Templates</a></li>
<ol class='level-2'>
<li><a href='#Verf%C3%BCgbare%20Templatesprachen'>Verfügbare Templatesprachen</a></li>
<li><a href='#Haml%20Templates'>Haml Templates</a></li>
<li><a href='#Erb%20Templates'>Erb Templates</a></li>
<li><a href='#Builder%20Templates'>Builder Templates</a></li>
<li><a href='#Nokogiri%20Templates'>Nokogiri Templates</a></li>
<li><a href='#Sass%20Templates'>Sass Templates</a></li>
<li><a href='#SCSS%20Templates'>SCSS Templates</a></li>
<li><a href='#Less%20Templates'>Less Templates</a></li>
<li><a href='#Liquid%20Templates'>Liquid Templates</a></li>
<li><a href='#Markdown%20Templates'>Markdown Templates</a></li>
<li><a href='#Textile%20Templates'>Textile Templates</a></li>
<li><a href='#RDoc%20Templates'>RDoc Templates</a></li>
<li><a href='#Radius%20Templates'>Radius Templates</a></li>
<li><a href='#Markaby%20Templates'>Markaby Templates</a></li>
<li><a href='#Slim%20Templates'>Slim Templates</a></li>
<li><a href='#Creole%20Templates'>Creole Templates</a></li>
<li><a href='#CoffeeScript%20Templates'>CoffeeScript Templates</a></li>
<li><a href='#Eingebettete%20Templates'>Eingebettete Templates</a></li>
<li><a href='#Auf%20Variablen%20in%20Templates%20zugreifen'>Auf Variablen in Templates zugreifen</a></li>
<li><a href='#Inline-Templates'>Inline-Templates</a></li>
<li><a href='#Benannte%20Templates'>Benannte Templates</a></li>
<li><a href='#Dateiendungen%20zuordnen'>Dateiendungen zuordnen</a></li>
<li><a href='#Eine%20eigene%20Template-Engine%20hinzuf%C3%BCgen'>Eine eigene Template-Engine hinzufügen</a></li>
</ol>
<li><a href='#Filter'>Filter</a></li>
<li><a href='#Helfer'>Helfer</a></li>
<ol class='level-2'>
<li><a href='#Sessions%20verwenden'>Sessions verwenden</a></li>
</ol>
<li><a href='#Anhalten'>Anhalten</a></li>
<li><a href='#Weiterspringen'>Weiterspringen</a></li>
<ol class='level-2'>
<li><a href='#Eine%20andere%20Route%20ansteuern'>Eine andere Route ansteuern</a></li>
<li><a href='#Body,%20Status-Code%20und%20Header%20setzen'>Body, Status-Code und Header setzen</a></li>
<li><a href='#Response-Streams'>Response-Streams</a></li>
<li><a href='#Logger'>Logger</a></li>
</ol>
<li><a href='#Mime-Types'>Mime-Types</a></li>
<ol class='level-2'>
<li><a href='#URLs%20generieren'>URLs generieren</a></li>
<li><a href='#Browser-Umleitung'>Browser-Umleitung</a></li>
<li><a href='#Cache%20einsetzen'>Cache einsetzen</a></li>
<li><a href='#Dateien%20versenden'>Dateien versenden</a></li>
</ol>
<li><a href='#Das%20Request-Objekt'>Das Request-Objekt</a></li>
<ol class='level-2'>
<li><a href='#Anh%C3%A4nge'>Anhänge</a></li>
<li><a href='#Umgang%20mit%20Datum%20und%20Zeit'>Umgang mit Datum und Zeit</a></li>
<li><a href='#Nachschlagen%20von%20Template-Dateien'>Nachschlagen von Template-Dateien</a></li>
</ol>
<li><a href='#Konfiguration'>Konfiguration</a></li>
<ol class='level-2'>
<li><a href='#Einstellung%20des%20Angriffsschutzes'>Einstellung des Angriffsschutzes</a></li>
<li><a href='#M%C3%B6gliche%20Einstellungen'>Mögliche Einstellungen</a></li>
</ol>
<li><a href='#Umgebungen'>Umgebungen</a></li>
<li><a href='#Fehlerbehandlung'>Fehlerbehandlung</a></li>
<ol class='level-2'>
<li><a href='#Nicht%20gefunden'>Nicht gefunden</a></li>
<li><a href='#Fehler'>Fehler</a></li>
</ol>
<li><a href='#Rack-Middleware'>Rack-Middleware</a></li>
<li><a href='#Testen'>Testen</a></li>
<li><a href='#Sinatra::Base%20-%20Middleware,%20Bibliotheken%20und%20modulare%20Anwendungen'>Sinatra::Base - Middleware, Bibliotheken und modulare Anwendungen</a></li>
<ol class='level-2'>
<li><a href='#Modularer%20vs.%20klassischer%20Stil'>Modularer vs. klassischer Stil</a></li>
<li><a href='#Eine%20modulare%20Applikation%20bereitstellen'>Eine modulare Applikation bereitstellen</a></li>
<li><a href='#Eine%20klassische%20Anwendung%20mit%20einer%20config.ru%20verwenden'>Eine klassische Anwendung mit einer config.ru verwenden</a></li>
<li><a href='#Wann%20sollte%20eine%20config.ru-Datei%20verwendet%20werden?'>Wann sollte eine config.ru-Datei verwendet werden?</a></li>
<li><a href='#Sinatra%20als%20Middleware%20nutzen'>Sinatra als Middleware nutzen</a></li>
<li><a href='#Dynamische%20Applikationserstellung'>Dynamische Applikationserstellung</a></li>
</ol>
<li><a href='#Geltungsbereich%20und%20Bindung'>Geltungsbereich und Bindung</a></li>
<ol class='level-2'>
<li><a href='#Anwendungs-%20oder%20Klassen-Scope'>Anwendungs- oder Klassen-Scope</a></li>
<li><a href='#Anfrage-%20oder%20Instanz-Scope'>Anfrage- oder Instanz-Scope</a></li>
<li><a href='#Delegation-Scope'>Delegation-Scope</a></li>
</ol>
<li><a href='#Kommandozeile'>Kommandozeile</a></li>
<li><a href='#Systemanforderungen'>Systemanforderungen</a></li>
<li><a href='#Der%20neuste%20Stand%20(The%20Bleeding%20Edge)'>Der neuste Stand (The Bleeding Edge)</a></li>
<ol class='level-2'>
<li><a href='#Mit%20Bundler'>Mit Bundler</a></li>
<li><a href='#Eigenes%20Repository'>Eigenes Repository</a></li>
<li><a href='#Gem%20erstellen'>Gem erstellen</a></li>
</ol>
<li><a href='#Versions-Verfahren'>Versions-Verfahren</a></li>
<li><a href='#Mehr'>Mehr</a></li>
</ol>
</div>
<p><em>Wichtig: Dieses Dokument ist eine Übersetzung aus dem Englischen und
unter Umständen nicht auf dem aktuellen Stand.</em></p>
<p>Sinatra ist eine DSL, die das schnelle Erstellen von Webanwendungen in Ruby
mit minimalem Aufwand ermöglicht:</p>
<pre># myapp.rb
require 'sinatra'
get '/' do
'Hallo Welt!'
end</pre>
<p>Einfach via <tt>rubygems</tt> installieren und starten:</p>
<pre>gem install sinatra
ruby -rubygems myapp.rb</pre>
<p>Die Seite kann nun unter <a href="http://localhost:4567">localhost:4567</a>
betrachtet werden.</p>
<p>Es wird empfohlen, den Thin-Server via <tt>gem install thin</tt> zu
installieren, den Sinatra dann, soweit vorhanden, automatisch verwendet.</p>
<a name='Routen'></a>
<h2>Routen</h2>
<p>In Sinatra wird eine Route durch eine HTTP-Methode und ein URL-Muster
definiert. Jeder dieser Routen wird ein Ruby-Block zugeordnet:</p>
<pre>get '/' do
.. zeige etwas ..
end
post '/' do
.. erstelle etwas ..
end
put '/' do
.. update etwas ..
end
delete '/' do
.. entferne etwas ..
end
options '/' do
.. zeige, was wir können ..
end</pre>
<p>Die Routen werden in der Reihenfolge durchlaufen, in der sie definiert
wurden. Das erste Routen-Muster, das mit dem Request übereinstimmt, wird
ausgeführt.</p>
<p>Die Muster der Routen können benannte Parameter beinhalten, die über den
<tt>params</tt>-Hash zugänglich gemacht werden:</p>
<pre>get '/hallo/:name' do
# passt auf "GET /hallo/foo" und "GET /hallo/bar"
# params[:name] ist 'foo' oder 'bar'
"Hallo #{params[:name]}!"
end</pre>
<p>Man kann auf diese auch mit Block-Parametern zugreifen:</p>
<pre>get '/hallo/:name' do |n|
"Hallo #{n}!"
end</pre>
<p>Routen-Muster können auch mit Splat- oder Wildcard-Parametern über das
<tt>params[:splat]</tt>-Array angesprochen werden:</p>
<pre>get '/sag/*/zu/*' do
# passt auf /sag/hallo/zu/welt
params[:splat] # => ["hallo", "welt"]
end
get '/download/*.*' do
# passt auf /download/pfad/zu/datei.xml
params[:splat] # => ["pfad/zu/datei", "xml"]
end</pre>
<p>Oder mit Block-Parametern:</p>
<pre>get '/download/*.*' do |pfad, endung|
[pfad, endung] # => ["Pfad/zu/Datei", "xml"]
end</pre>
<p>Routen mit regulären Ausdrücken sind auch möglich:</p>
<pre>get %r{/hallo/([\w]+)} do
"Hallo, #{params[:captures].first}!"
end</pre>
<p>Und auch hier können Block-Parameter genutzt werden:</p>
<pre>get %r{/hallo/([\w]+)} do |c|
"Hallo, #{c}!"
end</pre>
<p>Routen-Muster können auch mit optionalen Parametern ausgestattet werden:</p>
<pre>get '/posts.?:format?' do
# passt auf "GET /posts" sowie jegliche Erweiterung
# wie "GET /posts.json", "GET /posts.xml" etc.
end</pre>
<p>Anmerkung: Solange man den sog. Path Traversal Attack-Schutz nicht
deaktiviert (siehe weiter unten), kann es sein, dass der Request-Pfad noch
vor dem Abgleich mit den Routen modifiziert wird.</p>
<a name='Bedingungen'></a>
<h3>Bedingungen</h3>
<p>An Routen können eine Vielzahl von Bedingungen angehängt werden, die
erfüllt sein müssen, damit der Block ausgeführt wird. Möglich wäre
etwa eine Einschränkung des User-Agents:</p>
<pre>get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
"Du verwendest Songbird Version #{params[:agent][0]}"
end
get '/foo' do
# passt auf andere Browser
end</pre>
<p>Andere mitgelieferte Bedingungen sind <tt>host_name</tt> und
<tt>provides</tt>:</p>
<pre>get '/', :host_name => /^admin\./ do
"Adminbereich, Zugriff verweigert!"
end
get '/', :provides => 'html' do
haml :index
end
get '/', :provides => ['rss', 'atom', 'xml'] do
builder :feed
end</pre>
<p>Es können auch andere Bedingungen relativ einfach hinzugefügt werden:</p>
<pre>set(:probability) { |value| condition { rand <= value } }
get '/auto_gewinnen', :probability => 0.1 do
"Du hast gewonnen!"
end
get '/auto_gewinnen' do
"Tut mir leid, verloren."
end</pre>
<p>Bei Bedingungen, die mehrere Werte annehmen können, sollte ein Splat
verwendet werden:</p>
<pre>set(:auth) do |*roles| # <- hier kommt der Splat ins Spiel
condition do
unless logged_in? && roles.any? {|role| current_user.in_role? role }
redirect "/login/", 303
end
end
end
get "/mein/account/", :auth => [:user, :admin] do
"Mein Account"
end
get "/nur/admin/", :auth => :admin do
"Nur Admins dürfen hier rein!"
end</pre>
<a name='R%C3%BCckgabewerte'></a>
<h3>Rückgabewerte</h3>
<p>Durch den Rückgabewert eines Routen-Blocks wird mindestens der
Response-Body festgelegt, der an den HTTP-Client, bzw. die nächste
Rack-Middleware, weitergegeben wird. Im Normalfall handelt es sich hierbei,
wie in den vorangehenden Beispielen zu sehen war, um einen String. Es
werden allerdings auch andere Werte akzeptiert.</p>
<p>Es kann jedes gültige Objekt zurückgegeben werden, bei dem es sich
entweder um einen Rack-Rückgabewert, einen Rack-Body oder einen
HTTP-Status-Code handelt:</p>
<ul><li>
<p>Ein Array mit drei Elementen: <tt>[Status (Fixnum), Headers (Hash),
Response-Body (antwortet auf #each)]</tt>.</p>
</li><li>
<p>Ein Array mit zwei Elementen: <tt>[Status (Fixnum), Response-Body
(antwortet auf #each)]</tt>.</p>
</li><li>
<p>Ein Objekt, das auf <tt>#each</tt> antwortet und den an diese Methode
übergebenen Block nur mit Strings als Übergabewerte aufruft.</p>
</li><li>
<p>Ein Fixnum, das den Status-Code festlegt.</p>
</li></ul>
<p>Damit lässt sich relativ einfach Streaming implementieren:</p>
<pre>class Stream
def each
100.times { |i| yield "#{i}\n" }
end
end
get('/') { Stream.new }</pre>
<p>Ebenso kann die <tt>stream</tt>-Helfer-Methode (s.u.) verwendet werden, die
Streaming direkt in die Route integriert.</p>
<a name='Eigene%20Routen-Muster'></a>
<h3>Eigene Routen-Muster</h3>
<p>Wie oben schon beschrieben, ist Sinatra von Haus aus mit Unterstützung
für String-Muster und Reguläre Ausdrücke zum Abgleichen von Routen
ausgestattet. Das muss aber noch nicht alles sein, es können ohne großen
Aufwand eigene Routen-Muster erstellt werden:</p>
<pre>class AllButPattern
Match = Struct.new(:captures)
def initialize(except)
@except = except
@captures = Match.new([])
end
def match(str)
@captures unless @except === str
end
end
def all_but(pattern)
AllButPattern.new(pattern)
end
get all_but("/index") do
# ...
end</pre>
<p>Beachte, dass das obige Beispiel etwas übertrieben wirkt. Es geht auch
einfacher:</p>
<pre>get // do
pass if request.path_info == "/index"
# ...
end</pre>
<p>Oder unter Verwendung eines negativen look ahead:</p>
<pre>get %r{^(?!/index$)} do
# ...
end</pre>
<a name='Statische%20Dateien'></a>
<h2>Statische Dateien</h2>
<p>Statische Dateien werden aus dem <tt>./public</tt>-Ordner ausgeliefert. Es
ist möglich, einen anderen Ort zu definieren, indem man die
<tt>:public_folder</tt>-Option setzt:</p>
<pre>set :public_folder, File.dirname(__FILE__) + '/static'</pre>
<p>Zu beachten ist, dass der Ordnername public nicht Teil der URL ist. Die
Datei <tt>./public/css/style.css</tt> ist unter
<tt>http://example.com/css/style.css</tt> zu finden.</p>
<p>Um den <tt>Cache-Control</tt>-Header mit Informationen zu versorgen,
verwendet man die <tt>:static_cache_control</tt>-Einstellung (s.u.).</p>
<a name='Views/Templates'></a>
<h2>Views/Templates</h2>
<p>Alle Templatesprachen verwenden ihre eigene Renderingmethode, die jeweils
einen String zurückgibt:</p>
<pre>get '/' do
erb :index
end</pre>
<p>Dieses Beispiel rendert <tt>views/index.erb</tt>.</p>
<p>Anstelle eines Templatenamens kann man auch direkt die Templatesprache
verwenden:</p>
<pre>get '/' do
code = "<%= Time.now %>"
erb code
end</pre>
<p>Templates nehmen ein zweite Argument an, den Options-Hash:</p>
<pre>get '/' do
erb :index, :layout => :post
end</pre>
<p>Dieses Beispiel rendert <tt>views/index.erb</tt> eingebettet in
<tt>views/post.erb</tt> (Voreinstellung ist <tt>views/layout.erb</tt>,
sofern es vorhanden ist.)</p>
<p>Optionen, die Sinatra nicht versteht, werden an das Template
weitergereicht:</p>
<pre>get '/' do
haml :index, :format => :html5
end</pre>
<p>Für alle Templates können auch generelle Einstellungen festgelegt werden:</p>
<pre>set :haml, :format => :html5
get '/' do
haml :index
end</pre>
<p>Optionen, die an die Rendermethode weitergegeben werden, überschreiben die
Einstellungen, die mit <tt>set</tt> festgelegt wurden.</p>
<p>Mögliche Einstellungen:</p>
<dl class="rdoc-list"><dt>locals</dt>
<dd>
<p>Liste von lokalen Variablen, die and das Dokument weitergegeben werden.
Praktisch für Partials. Beispiel: <tt>erb "<%= foo %>",
:locals => {:foo => "bar"}</tt></p>
</dd><dt>default_encoding</dt>
<dd>
<p>Gibt die Stringkodierung an, die verwendet werden soll. Voreingestellt auf
<tt>settings.default_encoding</tt>.</p>
</dd><dt>views</dt>
<dd>
<p>Ordner, aus dem die Templates heraus geladen werden. Voreingestellt auf
<tt>settings.views</tt>.</p>
</dd><dt>layout</dt>
<dd>
<p>Legt fest, ob ein Layouttemplate verwendet werden soll oder nicht
(<tt>true</tt> oder <tt>false</tt>). Ist es ein Symbol, dass legt es fest,
welches Template als Layout verwendet wird. Beispiel:</p>
<pre><tt>erb :index, :layout => !request.xhr?</tt></pre>
</dd><dt>content_type</dt>
<dd>
<p>Content-Type den das Template ausgibt. Voreinstellung hängt von der
Templatesprache ab.</p>
</dd><dt>scope</dt>
<dd>
<p>Scope, in dem das Template gerendert wird. Liegt standardmäßig innerhalb
der App-Instanz. Wird Scope geändert, sind Instanzvariablen und
Helfermethoden nicht verfügbar.</p>
</dd><dt>layout_engine</dt>
<dd>
<p>Legt fest, welcher Renderer für das Layout verantwortlich ist. Hilfreich
für Sprachen, die sonst keine Templates unterstützen. Voreingestellt auf
den Renderer, der für das Template verwendet wird. Beispiel: <tt>set
:rdoc, :layout_engine => :erb</tt></p>
</dd></dl>
<p>Sinatra geht davon aus, dass die Templates sich im <tt>./views</tt>
Verzeichnis befinden. Es kann jedoch ein anderer Ordner festgelegt werden:</p>
<pre>set :views, settings.root + '/templates'</pre>
<p>Es ist zu beachten, dass immer mit Symbolen auf Templates verwiesen werden
muss, auch dann, wenn sie sich in einem Unterordner befinden:</p>
<pre>haml :'unterverzeichnis/template'</pre>
<p>Rendering-Methoden rendern jeden String direkt.</p>
<a name='Verf%C3%BCgbare%20Templatesprachen'></a>
<h3>Verfügbare Templatesprachen</h3>
<p>Einige Sprachen haben mehrere Implementierungen. Um festzulegen, welche
verwendet wird (und dann auch Thread-sicher ist), verwendet man am besten
zu Beginn ein 'require':</p>
<pre>require 'rdiscount' # oder require 'bluecloth'
get('/') { markdown :index }</pre>
<a name='Haml%20Templates'></a>
<h3>Haml Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://haml.info/">haml</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.haml</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>haml :index, :format => :html5</tt></p>
</td></tr></table>
<a name='Erb%20Templates'></a>
<h3>Erb Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://www.kuwata-lab.com/erubis/">erubis</a> oder</p>
</td></tr></table>
<pre>erb (included in Ruby)</pre>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.erb</tt>, <tt>.rhtml</tt> oder <tt>.erubis</tt></p>
</td></tr></table>
<pre>(nur Erubis)</pre>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>erb :index</tt></p>
</td></tr></table>
<a name='Builder%20Templates'></a>
<h3>Builder Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://builder.rubyforge.org/">builder</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.builder</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>builder { |xml| xml.em "Hallo" }</tt></p>
</td></tr></table>
<p>Nimmt ebenso einen Block für Inline-Templates entgegen (siehe Beispiel).</p>
<a name='Nokogiri%20Templates'></a>
<h3>Nokogiri Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://nokogiri.org/">nokogiri</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.nokogiri</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>nokogiri { |xml| xml.em "Hallo" }</tt></p>
</td></tr></table>
<p>Nimmt ebenso einen Block für Inline-Templates entgegen (siehe Beispiel).</p>
<a name='Sass%20Templates'></a>
<h3>Sass Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://sass-lang.com/">sass</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.sass</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>sass :stylesheet, :style => :expanded</tt></p>
</td></tr></table>
<a name='SCSS%20Templates'></a>
<h3>SCSS Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://sass-lang.com/">sass</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.scss</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>scss :stylesheet, :style => :expanded</tt></p>
</td></tr></table>
<a name='Less%20Templates'></a>
<h3>Less Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://www.lesscss.org/">less</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.less</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>less :stylesheet</tt></p>
</td></tr></table>
<a name='Liquid%20Templates'></a>
<h3>Liquid Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://www.liquidmarkup.org/">liquid</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.liquid</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>liquid :index, :locals => { :key => 'Wert' }</tt></p>
</td></tr></table>
<p>Da man aus dem Liquid-Template heraus keine Ruby-Methoden aufrufen kann
(ausgenommen <tt>yield</tt>), wird man üblicherweise locals verwenden
wollen, mit denen man Variablen weitergibt.</p>
<a name='Markdown%20Templates'></a>
<h3>Markdown Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="https://github.com/rtomayko/rdiscount">rdiscount</a>,</p>
</td></tr></table>
<pre>{redcarpet}[https://github.com/tanoku/redcarpet],
{bluecloth}[http://deveiate.org/projects/BlueCloth],
{kramdown}[http://kramdown.rubyforge.org/] *oder*
{maruku}[http://maruku.rubyforge.org/]</pre>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.markdown</tt>, <tt>.mkd</tt> und <tt>.md</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>markdown :index, :layout_engine => :erb</tt></p>
</td></tr></table>
<p>Da man aus den Markdown-Templates heraus keine Ruby-Methoden aufrufen und
auch keine locals verwenden kann, wird man Markdown üblicherweise in
Kombination mit anderen Renderern verwenden wollen:</p>
<pre>erb :overview, :locals => { :text => markdown(:einfuehrung) }</pre>
<p>Beachte, dass man die <tt>markdown</tt>-Methode auch aus anderen Templates
heraus aufrufen kann:</p>
<pre>%h1 Gruß von Haml!
%p= markdown(:Grüße)</pre>
<p>Da man Ruby nicht von Markdown heraus aufrufen kann, können auch Layouts
nicht in Markdown geschrieben werden. Es ist aber möglich, einen Renderer
für die Templates zu verwenden und einen anderen für das Layout, indem
die <tt>:layout_engine</tt>-Option verwendet wird.</p>
<a name='Textile%20Templates'></a>
<h3>Textile Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://redcloth.org/">RedCloth</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.textile</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>textile :index, :layout_engine => :erb</tt></p>
</td></tr></table>
<p>Da man aus dem Textile-Template heraus keine Ruby-Methoden aufrufen und
auch keine locals verwenden kann, wird man Textile üblicherweise in
Kombination mit anderen Renderern verwenden wollen:</p>
<pre>erb :overview, :locals => { :text => textile(:einfuehrung) }</pre>
<p>Beachte, dass man die <tt>textile</tt>-Methode auch aus anderen Templates
heraus aufrufen kann:</p>
<pre>%h1 Gruß von Haml!
%p= textile(:Grüße)</pre>
<p>Da man Ruby nicht von Textile heraus aufrufen kann, können auch Layouts
nicht in Textile geschrieben werden. Es ist aber möglich, einen Renderer
für die Templates zu verwenden und einen anderen für das Layout, indem
die <tt>:layout_engine</tt>-Option verwendet wird.</p>
<a name='RDoc%20Templates'></a>
<h3>RDoc Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://rdoc.rubyforge.org/">rdoc</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.rdoc</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>textile :README, :layout_engine => :erb</tt></p>
</td></tr></table>
<p>Da man aus dem RDoc-Template heraus keine Ruby-Methoden aufrufen und auch
keine locals verwenden kann, wird man RDoc üblicherweise in Kombination
mit anderen Renderern verwenden wollen:</p>
<pre>erb :overview, :locals => { :text => rdoc(:einfuehrung) }</pre>
<p>Beachte, dass man die <tt>rdoc</tt>-Methode auch aus anderen Templates
heraus aufrufen kann:</p>
<pre>%h1 Gruß von Haml!
%p= rdoc(:Grüße)</pre>
<p>Da man Ruby nicht von RDoc heraus aufrufen kann, können auch Layouts nicht
in RDoc geschrieben werden. Es ist aber möglich, einen Renderer für die
Templates zu verwenden und einen anderen für das Layout, indem die
<tt>:layout_engine</tt>-Option verwendet wird.</p>
<a name='Radius%20Templates'></a>
<h3>Radius Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://radius.rubyforge.org/">radius</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.radius</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>radius :index, :locals => { :key => 'Wert' }</tt></p>
</td></tr></table>
<p>Da man aus dem Radius-Template heraus keine Ruby-Methoden aufrufen kann,
wird man üblicherweise locals verwenden wollen, mit denen man Variablen
weitergibt.</p>
<a name='Markaby%20Templates'></a>
<h3>Markaby Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://markaby.github.com/">markaby</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.mab</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>markaby { h1 "Willkommen!" }</tt></p>
</td></tr></table>
<p>Nimmt ebenso einen Block für Inline-Templates entgegen (siehe Beispiel).</p>
<a name='Slim%20Templates'></a>
<h3>Slim Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="http://slim-lang.com/">slim</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.slim</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>slim :index</tt></p>
</td></tr></table>
<a name='Creole%20Templates'></a>
<h3>Creole Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="https://github.com/minad/creole">creole</a></p>
</td></tr><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.creole</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>creole :wiki, :layout_engine => :erb</tt></p>
</td></tr></table>
<p>Da man aus dem Creole-Template heraus keine Ruby-Methoden aufrufen und auch
keine locals verwenden kann, wird man Creole üblicherweise in Kombination
mit anderen Renderern verwenden wollen:</p>
<pre>erb :overview, :locals => { :text => creole(:einfuehrung) }</pre>
<p>Beachte, dass man die <tt>creole</tt>-Methode auch aus anderen Templates
heraus aufrufen kann:</p>
<pre>%h1 Gruß von Haml!
%p= creole(:Grüße)</pre>
<p>Da man Ruby nicht von Creole heraus aufrufen kann, können auch Layouts
nicht in Creole geschrieben werden. Es ist aber möglich, einen Renderer
für die Templates zu verwenden und einen anderen für das Layout, indem
die <tt>:layout_engine</tt>-Option verwendet wird.</p>
<a name='CoffeeScript%20Templates'></a>
<h3>CoffeeScript Templates</h3>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Abhängigkeit</p></td>
<td>
<p><a href="https://github.com/josh/ruby-coffee-script">coffee-script</a></p>
</td></tr></table>
<pre>und eine {Möglichkeit JavaScript auszuführen}[https://github.com/sstephenson/execjs/blob/master/README.md#readme]</pre>
<table class="rdoc-list"><tr><td class="rdoc-term"><p>Dateierweiterungs</p></td>
<td>
<p><tt>.coffee</tt></p>
</td></tr><tr><td class="rdoc-term"><p>Beispiel</p></td>
<td>
<p><tt>coffee :index</tt></p>
</td></tr></table>
<a name='Eingebettete%20Templates'></a>
<h3>Eingebettete Templates</h3>
<pre>get '/' do
haml '%div.title Hallo Welt'
end</pre>
<p>Rendert den eingebetteten Template-String.</p>
<a name='Auf%20Variablen%20in%20Templates%20zugreifen'></a>
<h3>Auf Variablen in Templates zugreifen</h3>
<p>Templates werden in demselben Kontext ausgeführt wie Routen.
Instanzvariablen in Routen sind auch direkt im Template verfügbar:</p>
<pre>get '/:id' do
@foo = Foo.find(params[:id])
haml '%h1= @foo.name'
end</pre>
<p>Oder durch einen expliziten Hash von lokalen Variablen:</p>
<pre>get '/:id' do
foo = Foo.find(params[:id])
haml '%h1= bar.name', :locals => { :bar => foo }
end</pre>
<p>Dies wird typischerweise bei Verwendung von Subtemplates (partials) in
anderen Templates eingesetzt.</p>
<a name='Inline-Templates'></a>
<h3>Inline-Templates</h3>
<p>Templates können auch am Ende der Datei definiert werden:</p>
<pre>require 'sinatra'
get '/' do
haml :index
end
__END__
@@ layout
%html
= yield
@@ index
%div.title Hallo Welt!!!!!</pre>
<p>Anmerkung: Inline-Templates, die in der Datei definiert sind, die
<tt>require 'sinatra'</tt> aufruft, werden automatisch geladen. Um andere
Inline-Templates in anderen Dateien aufzurufen, muss explizit <tt>enable
:inline_templates</tt> verwendet werden.</p>
<a name='Benannte%20Templates'></a>
<h3>Benannte Templates</h3>
<p>Templates können auch mit der Top-Level <tt>template</tt>-Methode
definiert werden:</p>
<pre>template :layout do
"%html\n =yield\n"
end
template :index do
'%div.title Hallo Welt!'
end
get '/' do
haml :index
end</pre>
<p>Wenn ein Template mit dem Namen "layout" existiert, wird es bei jedem
Aufruf verwendet. Durch <tt>:layout => false</tt> kann das Ausführen
verhindert werden:</p>
<pre>get '/' do
haml :index, :layout => request.xhr?
end</pre>
<a name='Dateiendungen%20zuordnen'></a>
<h3>Dateiendungen zuordnen</h3>
<p>Um eine Dateiendung einer Template-Engine zuzuordnen, kann
<tt>Tilt.register</tt> genutzt werden. Wenn etwa die Dateiendung
<tt>tt</tt> für Textile-Templates genutzt werden soll, lässt sich dies
wie folgt bewerkstelligen:</p>
<pre>Tilt.register :tt, Tilt[:textile]</pre>
<a name='Eine%20eigene%20Template-Engine%20hinzuf%C3%BCgen'></a>
<h3>Eine eigene Template-Engine hinzufügen</h3>
<p>Zu allererst muss die Engine bei Tilt registriert und danach eine
Rendering-Methode erstellt werden:</p>
<pre>Tilt.register :mtt, MeineTolleTemplateEngine
helpers do
def mtt(*args) render(:mtt, *args) end
end
get '/' do
mtt :index
end</pre>
<p>Dieser Code rendert <tt>./views/application.mtt</tt>. Siehe <a
href="https://github.com/rtomayko/tilt">github.com/rtomayko/tilt</a>, um
mehr über Tilt zu lernen.</p>
<a name='Filter'></a>
<h2>Filter</h2>
<p>Before-Filter werden vor jedem Request in demselben Kontext, wie danach die
Routen, ausgeführt. So können etwa Request und Antwort geändert werden.
Gesetzte Instanzvariablen in Filtern können in Routen und Templates
verwendet werden:</p>
<pre>before do
@note = 'Hi!'
request.path_info = '/foo/bar/baz'
end
get '/foo/*' do
@note #=> 'Hi!'
params[:splat] #=> 'bar/baz'
end</pre>
<p>After-Filter werden nach jedem Request in demselben Kontext ausgeführt und
können ebenfalls Request und Antwort ändern. In Before-Filtern gesetzte
Instanzvariablen können in After-Filtern verwendet werden:</p>
<pre>after do
puts response.status
end</pre>
<p>Filter können optional auch mit einem Muster ausgestattet werden, welches
auf den Request-Pfad passen muss, damit der Filter ausgeführt wird:</p>
<pre>before '/protected/*' do
authenticate!
end
after '/create/:slug' do |slug|
session[:last_slug] = slug
end</pre>
<p>Ähnlich wie Routen können Filter auch mit weiteren Bedingungen
eingeschränkt werden:</p>
<pre>before :agent => /Songbird/ do
# ...
end
after '/blog/*', :host_name => 'example.com' do
# ...
end</pre>
<a name='Helfer'></a>
<h2>Helfer</h2>
<p>Durch die Top-Level <tt>helpers</tt>-Methode werden sogenannte
Helfer-Methoden definiert, die in Routen und Templates verwendet werden
können:</p>
<pre>helpers do
def bar(name)
"#{name}bar"
end
end
get '/:name' do
bar(params[:name])
end</pre>
<a name='Sessions%20verwenden'></a>
<h3>Sessions verwenden</h3>
<p>Sessions werden verwendet, um Zustände zwischen den Requests zu speichern.
Sind sie aktiviert, kann ein Session-Hash je Benutzer-Session verwendet
werden.</p>
<pre>enable :sessions
get '/' do
"value = " << session[:value].inspect
end
get '/:value' do
session[:value] = params[:value]
end</pre>
<p>Beachte, dass <tt>enable :sessions</tt> alle Daten in einem Cookie
speichert. Unter Umständen kann dies negative Effekte haben, z.B.
verursachen viele Daten höheren, teilweise überflüssigen Traffic. Um das
zu vermeiden, kann eine Rack- Session-Middleware verwendet werden. Dabei
wird auf <tt>enable :sessions</tt> verzichtet und die Middleware wie
üblich im Programm eingebunden:</p>
<pre>use Rack::Session::Pool, :expire_after => 2592000
get '/' do
"value = " << session[:value].inspect
end
get '/:value' do
session[:value] = params[:value]
end</pre>
<p>Um die Sicherheit zu erhöhen, werden Cookies, die Session-Daten führen,
mit einem sogenannten Session-Secret signiert. Da sich dieses Geheimwort
bei jedem Neustart der Applikation automatisch ändert, ist es sinnvoll,
ein eigenes zu wählen, damit sich alle Instanzen der Applikation dasselbe
Session-Secret teilen:</p>