forked from sinatra/sinatra.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.es.html
3306 lines (2626 loc) · 161 KB
/
README.es.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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p><a href="http://travis-ci.org/sinatra/sinatra"><img src="https://secure.travis-ci.org/sinatra/sinatra.svg" alt="Build Status"></a></p>
<p>Sinatra es un
<a href="https://es.wikipedia.org/wiki/Lenguaje_espec%C3%ADfico_del_dominio">DSL</a> para
crear aplicaciones web rápidamente en Ruby con un mínimo esfuerzo:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="c1"># miapp.rb</span>
<span class="nb">require</span> <span class="s1">'sinatra'</span>
<span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="s1">'Hola mundo!'</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Instala la gema:</p>
<div class="language-shell highlighter-rouge">
<pre class="highlight"><code>gem install sinatra
ruby miapp.rb
</code></pre>
</div>
<p>Y corre la aplicación:
~~~~shell
gem install sinatra
ruby miapp.rb
~~~~</p>
<p>Ver en <a href="http://localhost:4567">http://localhost:4567</a>.</p>
<p>El código que cambiaste no tendra efecto hasta que reinicies el servidor.
Por favor reinicia el servidor cada vez que cambies tu código o usa <a href="http://www.sinatrarb.com/contrib/reloader">sinatra/reloader</a>.</p>
<p>Se recomienda ejecutar <code class="highlighter-rouge">gem install thin</code>, porque Sinatra lo utilizará si está disponible.</p>
<h2>Tabla de Contenidos</h2>
<h2>Rutas</h2>
<p>En Sinatra, una ruta es un método HTTP junto a un patrón de un URL.
Cada ruta está asociada a un bloque:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">mostrar</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
<span class="n">post</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">crear</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
<span class="n">put</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">reemplazar</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
<span class="n">patch</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">modificar</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
<span class="n">delete</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">aniquilar</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
<span class="n">options</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">informar</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
<span class="n">link</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">afiliar</span> <span class="n">a</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
<span class="n">unlink</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="p">.</span><span class="nf">.</span> <span class="n">separar</span> <span class="n">algo</span> <span class="p">.</span><span class="nf">.</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Las rutas son comparadas en el orden en el que son definidas. La primera ruta
que coincide con la petición es invocada.</p>
<p>Las rutas con barras al final son distintas a las que no tienen:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/foo'</span> <span class="k">do</span>
<span class="c1"># no es igual que "GET /foo/"</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Los patrones de las rutas pueden incluir parámetros nombrados, accesibles a
través del hash <code class="highlighter-rouge">params</code>:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/hola/:nombre'</span> <span class="k">do</span>
<span class="c1"># coincide con "GET /hola/foo" y "GET /hola/bar"</span>
<span class="c1"># params['nombre'] es 'foo' o 'bar'</span>
<span class="s2">"Hola </span><span class="si">#{</span><span class="n">params</span><span class="p">[</span><span class="s1">'nombre'</span><span class="p">]</span><span class="si">}</span><span class="s2">!"</span>
<span class="k">end</span>
</code></pre>
</div>
<p>También puede acceder a los parámetros nombrados usando parámetros de bloque:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/hola/:nombre'</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
<span class="c1"># coincide con "GET /hola/foo" y "GET /hola/bar"</span>
<span class="c1"># params['nombre'] es 'foo' o 'bar'</span>
<span class="c1"># n almacena params['nombre']</span>
<span class="s2">"Hola </span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">!"</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Los patrones de ruta también pueden incluir parámetros splat (o wildcard),
accesibles a través del arreglo <code class="highlighter-rouge">params['splat']</code>:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/decir/*/al/*'</span> <span class="k">do</span>
<span class="c1"># coincide con /decir/hola/al/mundo</span>
<span class="n">params</span><span class="p">[</span><span class="s1">'splat'</span><span class="p">]</span> <span class="c1"># => ["hola", "mundo"]</span>
<span class="k">end</span>
<span class="n">get</span> <span class="s1">'/descargar/*.*'</span> <span class="k">do</span>
<span class="c1"># coincide con /descargar/path/al/archivo.xml</span>
<span class="n">params</span><span class="p">[</span><span class="s1">'splat'</span><span class="p">]</span> <span class="c1"># => ["path/al/archivo", "xml"]</span>
<span class="k">end</span>
</code></pre>
</div>
<p>O, con parámetros de bloque:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/descargar/*.*'</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="p">,</span> <span class="n">ext</span><span class="o">|</span>
<span class="p">[</span><span class="n">path</span><span class="p">,</span> <span class="n">ext</span><span class="p">]</span> <span class="c1"># => ["path/to/file", "xml"]</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Rutas con Expresiones Regulares:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="sr">/\/hola\/([\w]+)/</span> <span class="k">do</span>
<span class="s2">"Hola, </span><span class="si">#{</span><span class="n">params</span><span class="p">[</span><span class="s1">'captures'</span><span class="p">].</span><span class="nf">first</span><span class="si">}</span><span class="s2">!"</span>
<span class="k">end</span>
</code></pre>
</div>
<p>O con un parámetro de bloque:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="sr">%r{/hola/([</span><span class="se">\w</span><span class="sr">]+)}</span> <span class="k">do</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span>
<span class="s2">"Hola, </span><span class="si">#{</span><span class="n">c</span><span class="si">}</span><span class="s2">!"</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Los patrones de ruta pueden contener parámetros opcionales:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/posts/:formato?'</span> <span class="k">do</span>
<span class="c1"># coincide con "GET /posts/" y además admite cualquier extensión, por</span>
<span class="c1"># ejemplo, "GET /posts/json", "GET /posts/xml", etc.</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Las rutas también pueden usar parámetros de consulta:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/posts'</span> <span class="k">do</span>
<span class="c1"># es igual que "GET /posts?title=foo&author=bar"</span>
<span class="n">title</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span>
<span class="n">author</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">'author'</span><span class="p">]</span>
<span class="c1"># usa las variables title y author; la consulta es opcional para la ruta /posts</span>
<span class="k">end</span>
</code></pre>
</div>
<p>A propósito, a menos que desactives la protección para el ataque <em>path
traversal</em> (ver más <a href="#configurando-la-protecci%C3%B3n-contra-ataques">abajo</a>), el path de la petición puede ser modificado
antes de que se compare con los de tus rutas.</p>
<p>Puedes perzonalizar las opciones de <a href="https://github.com/sinatra/mustermann">Mustermann</a> usadas para una ruta pasando
el hash <code class="highlighter-rouge">:mustermann_opts</code>:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'\A/posts\z'</span><span class="p">,</span> <span class="ss">:mustermann_opts</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:type</span> <span class="o">=></span> <span class="ss">:regexp</span><span class="p">,</span> <span class="ss">:check_anchors</span> <span class="o">=></span> <span class="kp">false</span> <span class="p">}</span> <span class="k">do</span>
<span class="c1"># es exactamente igual a /posts, con anclaje explícito</span>
<span class="s2">"¡Si igualas un patrón anclado aplaude!"</span>
<span class="k">end</span>
</code></pre>
</div>
<h2>Condiciones</h2>
<p>Las rutas pueden incluir una variedad de condiciones de selección, como por
ejemplo el user agent:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/foo'</span><span class="p">,</span> <span class="ss">:agent</span> <span class="o">=></span> <span class="sr">/Songbird (\d\.\d)[\d\/]*?/</span> <span class="k">do</span>
<span class="s2">"Estás usando la versión de Songbird </span><span class="si">#{</span><span class="n">params</span><span class="p">[</span><span class="s1">'agent'</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="n">get</span> <span class="s1">'/foo'</span> <span class="k">do</span>
<span class="c1"># Coincide con navegadores que no sean songbird</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Otras condiciones disponibles son <code class="highlighter-rouge">host_name</code> y <code class="highlighter-rouge">provides</code>:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span><span class="p">,</span> <span class="ss">:host_name</span> <span class="o">=></span> <span class="sr">/^admin\./</span> <span class="k">do</span>
<span class="s2">"Área de Administración, Acceso denegado!"</span>
<span class="k">end</span>
<span class="n">get</span> <span class="s1">'/'</span><span class="p">,</span> <span class="ss">:provides</span> <span class="o">=></span> <span class="s1">'html'</span> <span class="k">do</span>
<span class="n">haml</span> <span class="ss">:index</span>
<span class="k">end</span>
<span class="n">get</span> <span class="s1">'/'</span><span class="p">,</span> <span class="ss">:provides</span> <span class="o">=></span> <span class="p">[</span><span class="s1">'rss'</span><span class="p">,</span> <span class="s1">'atom'</span><span class="p">,</span> <span class="s1">'xml'</span><span class="p">]</span> <span class="k">do</span>
<span class="n">builder</span> <span class="ss">:feed</span>
<span class="k">end</span>
</code></pre>
</div>
<p><code class="highlighter-rouge">provides</code> busca el encabezado Accept de la solicitud</p>
<p>Puede definir sus propias condiciones fácilmente:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">set</span><span class="p">(</span><span class="ss">:probabilidad</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">valor</span><span class="o">|</span> <span class="n">condition</span> <span class="p">{</span> <span class="nb">rand</span> <span class="o"><=</span> <span class="n">valor</span> <span class="p">}</span> <span class="p">}</span>
<span class="n">get</span> <span class="s1">'/gana_un_auto'</span><span class="p">,</span> <span class="ss">:probabilidad</span> <span class="o">=></span> <span class="mi">0</span><span class="o">.</span><span class="mi">1</span> <span class="k">do</span>
<span class="s2">"Ganaste!"</span>
<span class="k">end</span>
<span class="n">get</span> <span class="s1">'/gana_un_auto'</span> <span class="k">do</span>
<span class="s2">"Lo siento, perdiste."</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Para una condición que toma multiples valores usa splat:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">set</span><span class="p">(</span><span class="ss">:autorizar</span><span class="p">)</span> <span class="k">do</span> <span class="o">|*</span><span class="n">roles</span><span class="o">|</span> <span class="c1"># <- mira el splat</span>
<span class="n">condition</span> <span class="k">do</span>
<span class="k">unless</span> <span class="n">sesion_iniciada?</span> <span class="o">&&</span> <span class="n">roles</span><span class="p">.</span><span class="nf">any?</span> <span class="p">{</span><span class="o">|</span><span class="n">rol</span><span class="o">|</span> <span class="n">usuario_actual</span><span class="p">.</span><span class="nf">tiene_rol?</span> <span class="n">rol</span> <span class="p">}</span>
<span class="n">redirect</span> <span class="s2">"/iniciar_sesion/"</span><span class="p">,</span> <span class="mi">303</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">get</span> <span class="s2">"/mi/cuenta/"</span><span class="p">,</span> <span class="ss">:autorizar</span> <span class="o">=></span> <span class="p">[</span><span class="ss">:usuario</span><span class="p">,</span> <span class="ss">:administrador</span><span class="p">]</span> <span class="k">do</span>
<span class="s2">"Detalles de mi cuenta"</span>
<span class="k">end</span>
<span class="n">get</span> <span class="s2">"/solo/administradores/"</span><span class="p">,</span> <span class="ss">:autorizar</span> <span class="o">=></span> <span class="ss">:administrador</span> <span class="k">do</span>
<span class="s2">"Únicamente para administradores!"</span>
<span class="k">end</span>
</code></pre>
</div>
<h2>Valores de Retorno</h2>
<p>El valor de retorno de un bloque de ruta determina por lo menos el cuerpo de la respuesta
transmitida al cliente HTTP o por lo menos al siguiente middleware en la pila de Rack.
Lo más común es que sea un string, como en los ejemplos anteriores.
Sin embargo, otros valores también son aceptados.</p>
<p>Puedes retornar cualquier objeto que sea una respuesta Rack válida,
un objeto que represente el cuerpo de una respuesta Rack o un código
de estado HTTP:</p>
<ul>
<li>Un arreglo con tres elementos: <code class="highlighter-rouge">[estado (Integer), cabeceras (Hash), cuerpo de
la respuesta (responde a #each)]</code>
</li>
<li>Un arreglo con dos elementos: <code class="highlighter-rouge">[estado (Integer), cuerpo de la respuesta
(responde a #each)]</code>
</li>
<li>Un objeto que responde a <code class="highlighter-rouge">#each</code> y que le pasa únicamente strings al bloque
dado</li>
<li>Un Integer representando el código de estado</li>
</ul>
<p>De esa manera, por ejemplo, podemos fácilmente implementar un ejemplo de streaming:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="k">class</span> <span class="nc">Stream</span>
<span class="k">def</span> <span class="nf">each</span>
<span class="mi">100</span><span class="p">.</span><span class="nf">times</span> <span class="p">{</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="k">yield</span> <span class="s2">"</span><span class="si">#{</span><span class="n">i</span><span class="si">}</span><span class="se">\n</span><span class="s2">"</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">get</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span> <span class="p">{</span> <span class="no">Stream</span><span class="p">.</span><span class="nf">new</span> <span class="p">}</span>
</code></pre>
</div>
<p>También puedes usar el <code class="highlighter-rouge">stream</code> helper (<a href="#streaming-de-respuestas">descrito abajo</a>)
para reducir el código repetitivo e incrustar la lógica de stream a la ruta</p>
<h2>Comparadores de Rutas Personalizados</h2>
<p>Como se mostró anteriormente, Sinatra permite utilizar strings y expresiones
regulares para definir las rutas. Sin embargo, no termina ahí.Puedes
definir tus propios comparadores muy fácilmente:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="k">class</span> <span class="nc">TodoMenosElPatron</span>
<span class="nc">Match</span> <span class="o">=</span> <span class="no">Struct</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:captures</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">excepto</span><span class="p">)</span>
<span class="vi">@excepto</span> <span class="o">=</span> <span class="n">excepto</span>
<span class="vi">@capturas</span> <span class="o">=</span> <span class="no">Match</span><span class="p">.</span><span class="nf">new</span><span class="p">([])</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">match</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
<span class="vi">@capturas</span> <span class="k">unless</span> <span class="vi">@excepto</span> <span class="o">===</span> <span class="n">str</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">cualquiera_menos</span><span class="p">(</span><span class="n">patron</span><span class="p">)</span>
<span class="no">TodoMenosElPatron</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">patron</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">get</span> <span class="n">cualquiera_menos</span><span class="p">(</span><span class="s2">"/index"</span><span class="p">)</span> <span class="k">do</span>
<span class="c1"># ...</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Tenga en cuenta que el ejemplo anterior es un poco rebuscado. Un resultado
similar puede conseguirse más sencillamente:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="sr">//</span> <span class="k">do</span>
<span class="n">pass</span> <span class="k">if</span> <span class="n">request</span><span class="p">.</span><span class="nf">path_info</span> <span class="o">==</span> <span class="s2">"/index"</span>
<span class="c1"># ...</span>
<span class="k">end</span>
</code></pre>
</div>
<p>O, usando un look ahead negativo:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="sr">%r{(?!/index)}</span> <span class="k">do</span>
<span class="c1"># ...</span>
<span class="k">end</span>
</code></pre>
</div>
<h2>Archivos Estáticos</h2>
<p>Los archivos estáticos son servidos desde el directorio público
<code class="highlighter-rouge">./public</code>. Puede especificar una ubicación diferente ajustando la
opción <code class="highlighter-rouge">:public_folder</code>:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">set</span> <span class="ss">:public_folder</span><span class="p">,</span> <span class="n">__dir__</span> <span class="o">+</span> <span class="s1">'/static'</span>
</code></pre>
</div>
<p>Note que el nombre del directorio público no está incluido en la URL. Por
ejemplo, el archivo <code class="highlighter-rouge">./public/css/style.css</code> se accede a través de
<code class="highlighter-rouge">http://ejemplo.com/css/style.css</code>.</p>
<p>Use la configuración <code class="highlighter-rouge">:static_cache_control</code> para agregar el encabezado
<code class="highlighter-rouge">Cache-Control</code> (Ver mas <a href="#control-del-cache">abajo</a>).</p>
<h2>Vistas / Plantillas</h2>
<p>Cada lenguaje de plantilla se expone a través de un método de renderizado que
lleva su nombre. Estos métodos simplemente devuelven un string:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="n">erb</span> <span class="ss">:index</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Renderiza <code class="highlighter-rouge">views/index.erb</code>.</p>
<p>En lugar del nombre de la plantilla puedes proporcionar directamente el
contenido de la misma:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="n">codigo</span> <span class="o">=</span> <span class="s2">"<%= Time.now %>"</span>
<span class="n">erb</span> <span class="n">codigo</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Los métodos de renderizado, aceptan además un segundo argumento, el hash de
opciones:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="n">erb</span> <span class="ss">:index</span><span class="p">,</span> <span class="ss">:layout</span> <span class="o">=></span> <span class="ss">:post</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Renderiza <code class="highlighter-rouge">views/index.erb</code> incrustado en <code class="highlighter-rouge">views/post.erb</code> (por
defecto, la plantilla <code class="highlighter-rouge">:index</code> es incrustada en <code class="highlighter-rouge">views/layout.erb</code> siempre y
cuando este último archivo exista).</p>
<p>Cualquier opción que Sinatra no entienda le será pasada al motor de renderizado
de la plantilla:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="n">haml</span> <span class="ss">:index</span><span class="p">,</span> <span class="ss">:format</span> <span class="o">=></span> <span class="ss">:html5</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Además, puede definir las opciones para un lenguaje de plantillas de forma
general:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">set</span> <span class="ss">:haml</span><span class="p">,</span> <span class="ss">:format</span> <span class="o">=></span> <span class="ss">:html5</span>
<span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="n">haml</span> <span class="ss">:index</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Las opciones pasadas al método de renderizado tienen precedencia sobre las
definidas mediante <code class="highlighter-rouge">set</code>.</p>
<p>Opciones disponibles:</p>
<dl>
<dt>locals</dt>
<dd>
Lista de variables locales pasadas al documento. Resultan muy útiles cuando
se combinan con parciales.
Ejemplo: <tt>erb "<%= foo %>", :locals => {:foo => "bar"}</tt>
</dd>
<dt>default_encoding</dt>
<dd>
Encoding utilizado cuando el de un string es dudoso. Por defecto toma el
valor de <tt>settings.default_encoding</tt>.
</dd>
<dt>views</dt>
<dd>
Directorio desde donde se cargan las vistas. Por defecto toma el valor de
<tt>settings.views</tt>.
</dd>
<dt>layout</dt>
<dd>
Si es <tt>true</tt> o <tt>false</tt> indica que se debe usar, o no, un layout,
respectivamente. También puede ser un Symbol que especifique qué plantilla
usar. Ejemplo: <tt>erb :index, :layout => !request.xhr?</tt>
</dd>
<dt>content_type</dt>
<dd>
Content-Type que produce la plantilla. El valor por defecto depende de cada
lenguaje de plantillas.
</dd>
<dt>scope</dt>
<dd>
Ámbito en el que se renderiza la plantilla. Por defecto utiliza la instancia
de la aplicación. Ten en cuenta que si cambiás esta opción las variables de
instancia y los helpers van a dejar de estar disponibles.
</dd>
<dt>layout_engine</dt>
<dd>
Motor de renderizado de plantillas que usa para el layout. Resulta
conveniente para lenguajes que no soportan layouts. Por defecto toma el valor
del motor usado para renderizar la plantilla.
Ejemplo: <tt>set :rdoc, :layout_engine => :erb</tt>
</dd>
<dt>layout_options</dt>
<dd>
Opciones especiales usadas únicamente para renderizar el layout. Ejemplo:
<tt>set :rdoc, :layout_options => { :views => 'views/layouts' }</tt>
</dd>
</dl>
<p>Se asume que las plantillas están ubicadas directamente bajo el directorio <code class="highlighter-rouge">./views</code>.
Para usar un directorio diferente:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">set</span> <span class="ss">:views</span><span class="p">,</span> <span class="n">settings</span><span class="p">.</span><span class="nf">root</span> <span class="o">+</span> <span class="s1">'/templates'</span>
</code></pre>
</div>
<p>Es importante acordarse que siempre tienes que referenciar a las plantillas con
símbolos, incluso cuando se encuentran en un subdirectorio (en este caso
tienes que usar: <code class="highlighter-rouge">:'subdir/plantilla'</code> o <code class="highlighter-rouge">'subdir/plantilla'.to_sym</code>). Esto es debido
a que los métodos de renderización van a renderizar directamente cualquier string que
se les pase como argumento.</p>
<h3>Plantillas Literales</h3>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="n">haml</span> <span class="s1">'%div.titulo Hola Mundo'</span>
<span class="k">end</span>
</code></pre>
</div>
<p>Renderiza el string de la plantilla. Opcionalmente puedes especificar
<code class="highlighter-rouge">:path</code> y <code class="highlighter-rouge">:line</code> para un backtrace más claro si hay una ruta del sistema
de archivos o una línea asociada con ese string</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">get</span> <span class="s1">'/'</span> <span class="k">do</span>
<span class="n">haml</span> <span class="s1">'%div.titulo Hola Mundo'</span><span class="p">,</span> <span class="ss">:path</span> <span class="o">=></span> <span class="s1">'ejemplos/archivo.haml'</span><span class="p">,</span> <span class="ss">:line</span> <span class="o">=></span> <span class="mi">3</span>
<span class="k">end</span>
</code></pre>
</div>
<h3>Lenguajes de Plantillas Disponibles</h3>
<p>Algunos lenguajes tienen varias implementaciones. Para especificar que
implementación usar (y para ser thread-safe), deberías requerirla antes de
usarla:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="nb">require</span> <span class="s1">'rdiscount'</span> <span class="c1"># o require 'bluecloth'</span>
<span class="n">get</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span> <span class="p">{</span> <span class="n">markdown</span> <span class="ss">:index</span> <span class="p">}</span>
</code></pre>
</div>
<h4>Plantillas Haml</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://haml.info/" title="haml">haml</a></td>
</tr>
<tr>
<td>Expresiones de Archivo</td>
<td><tt>.haml</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>haml :index, :format => :html5</tt></td>
</tr>
</table>
<h4>Plantillas Erb</h4>
<table>
<tr>
<td>Dependencias</td>
<td>
<a href="http://www.kuwata-lab.com/erubis/" title="erubis">erubis</a>
o erb (incluida en Ruby)
</td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td>
<tt>.erb</tt>, <tt>.rhtml</tt> o <tt>.erubis</tt> (solamente con Erubis)</td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>erb :index</tt></td>
</tr>
</table>
<h4>Plantillas Builder</h4>
<table>
<tr>
<td>Dependencias</td>
<td>
<a href="https://github.com/jimweirich/builder" title="builder">builder</a>
</td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.builder</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>builder { |xml| xml.em "hola" }</tt></td>
</tr>
</table>
<p>También toma un bloque para plantillas inline (ver <a href="#plantillas-inline">ejemplo</a>).</p>
<h4>Plantillas Nokogiri</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://www.nokogiri.org/" title="nokogiri">nokogiri</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.nokogiri</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>nokogiri { |xml| xml.em "hola" }</tt></td>
</tr>
</table>
<p>También toma un bloque para plantillas inline (ver <a href="#plantillas-inline">ejemplo</a>).</p>
<h4>Plantillas Sass</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://sass-lang.com/" title="sass">sass</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.sass</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>sass :stylesheet, :style => :expanded</tt></td>
</tr>
</table>
<h4>Plantillas SCSS</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://sass-lang.com/" title="sass">sass</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.scss</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>scss :stylesheet, :style => :expanded</tt></td>
</tr>
</table>
<h4>Plantillas Less</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://lesscss.org/" title="less">less</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.less</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>less :stylesheet</tt></td>
</tr>
</table>
<h4>Plantillas Liquid</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="https://shopify.github.io/liquid/" title="liquid">liquid</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.liquid</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>liquid :index, :locals => { :clave => 'valor' }</tt></td>
</tr>
</table>
<p>Como no va a poder llamar a métodos de Ruby (excepto por <code class="highlighter-rouge">yield</code>) desde una
plantilla Liquid, casi siempre va a querer pasarle locales.</p>
<h4>Plantillas Markdown</h4>
<table>
<tr>
<td>Dependencias</td>
<td>
<a href="https://github.com/davidfstr/rdiscount" title="RDiscount">RDiscount</a>,
<a href="https://github.com/vmg/redcarpet" title="RedCarpet">RedCarpet</a>,
<a href="https://github.com/ged/bluecloth" title="bluecloth">BlueCloth</a>,
<a href="http://kramdown.gettalong.org/" title="kramdown">kramdown</a> o
<a href="https://github.com/bhollis/maruku" title="maruku">maruku</a>
</td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td>
<tt>.markdown</tt>, <tt>.mkd</tt> y <tt>.md</tt>
</td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>markdown :index, :layout_engine => :erb</tt></td>
</tr>
</table>
<p>No es posible llamar métodos desde markdown, ni pasarle locales. Por lo tanto,
generalmente va a usarlo en combinación con otro motor de renderizado:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">erb</span> <span class="ss">:resumen</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:texto</span> <span class="o">=></span> <span class="n">markdown</span><span class="p">(</span><span class="ss">:introduccion</span><span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>Tenga en cuenta que también puedes llamar al método <code class="highlighter-rouge">markdown</code> desde otras
plantillas:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="o">%</span><span class="n">h1</span> <span class="no">Hola</span> <span class="no">Desde</span> <span class="no">Haml</span><span class="o">!</span>
<span class="o">%</span><span class="nb">p</span><span class="o">=</span> <span class="n">markdown</span><span class="p">(</span><span class="ss">:saludos</span><span class="p">)</span>
</code></pre>
</div>
<p>Como no puede utilizar Ruby desde Markdown, no puede usar layouts escritos en
Markdown. De todos modos, es posible usar un motor de renderizado para el
layout distinto al de la plantilla pasando la opción <code class="highlighter-rouge">:layout_engine</code>.</p>
<h4>Plantillas Textile</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://redcloth.org/" title="RedCloth">RedCloth</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.textile</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>textile :index, :layout_engine => :erb</tt></td>
</tr>
</table>
<p>No es posible llamar métodos desde textile, ni pasarle locales. Por lo tanto,
generalmente vas a usarlo en combinación con otro motor de renderizado:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">erb</span> <span class="ss">:resumen</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:texto</span> <span class="o">=></span> <span class="n">textile</span><span class="p">(</span><span class="ss">:introduccion</span><span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>Ten en cuenta que también puedes llamar al método <code class="highlighter-rouge">textile</code> desde otras
plantillas:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="o">%</span><span class="n">h1</span> <span class="no">Hola</span> <span class="no">Desde</span> <span class="no">Haml</span><span class="o">!</span>
<span class="o">%</span><span class="nb">p</span><span class="o">=</span> <span class="n">textile</span><span class="p">(</span><span class="ss">:saludos</span><span class="p">)</span>
</code></pre>
</div>
<p>Como no puedes utilizar Ruby desde Textile, no puedes usar layouts escritos en
Textile. De todos modos, es posible usar un motor de renderizado para el
layout distinto al de la plantilla pasando la opción <code class="highlighter-rouge">:layout_engine</code>.</p>
<h4>Plantillas RDoc</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://rdoc.sourceforge.net/" title="RDoc">RDoc</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.rdoc</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>rdoc :README, :layout_engine => :erb</tt></td>
</tr>
</table>
<p>No es posible llamar métodos desde rdoc, ni pasarle locales. Por lo tanto,
generalmente vas a usarlo en combinación con otro motor de renderizado:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">erb</span> <span class="ss">:resumen</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:texto</span> <span class="o">=></span> <span class="n">rdoc</span><span class="p">(</span><span class="ss">:introduccion</span><span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>Ten en cuenta que también puedes llamar al método <code class="highlighter-rouge">rdoc</code> desde otras
plantillas:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="o">%</span><span class="n">h1</span> <span class="no">Hola</span> <span class="no">Desde</span> <span class="no">Haml</span><span class="o">!</span>
<span class="o">%</span><span class="nb">p</span><span class="o">=</span> <span class="n">rdoc</span><span class="p">(</span><span class="ss">:saludos</span><span class="p">)</span>
</code></pre>
</div>
<p>Como no puedes utilizar Ruby desde RDoc, no puedes usar layouts escritos en RDoc.
De todos modos, es posible usar un motor de renderizado para el layout distinto
al de la plantilla pasando la opción <code class="highlighter-rouge">:layout_engine</code>.</p>
<h4>Plantillas AsciiDoc</h4>
<table>
<tr>
<td>Dependencia</td>
<td><a href="http://asciidoctor.org/" title="Asciidoctor">Asciidoctor</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td>
<tt>.asciidoc</tt>, <tt>.adoc</tt> and <tt>.ad</tt>
</td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>asciidoc :README, :layout_engine => :erb</tt></td>
</tr>
</table>
<p>Desde que no se puede utilizar métodos de Ruby desde una
plantilla AsciiDoc, casi siempre va a querer pasarle locales.</p>
<h4>Plantillas Radius</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="https://github.com/jlong/radius" title="Radius">Radius</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.radius</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>radius :index, :locals => { :clave => 'valor' }</tt></td>
</tr>
</table>
<p>Desde que no se puede utilizar métodos de Ruby (excepto por <code class="highlighter-rouge">yield</code>) de una
plantilla Radius, casi siempre se necesita pasar locales.</p>
<h4>Plantillas Markaby</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://markaby.github.io/" title="Markaby">Markaby</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.mab</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>markaby { h1 "Bienvenido!" }</tt></td>
</tr>
</table>
<p>También toma un bloque para plantillas inline (ver <a href="#plantillas-inline">ejemplo</a>).</p>
<h4>Plantillas RABL</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="https://github.com/nesquena/rabl" title="Rabl">Rabl</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.rabl</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>rabl :index</tt></td>
</tr>
</table>
<h4>Plantillas Slim</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="http://slim-lang.com/" title="Slim Lang">Slim Lang</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.slim</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>slim :index</tt></td>
</tr>
</table>
<h4>Plantillas Creole</h4>
<table>
<tr>
<td>Dependencias</td>
<td><a href="https://github.com/minad/creole" title="Creole">Creole</a></td>
</tr>
<tr>
<td>Extensiones de Archivo</td>
<td><tt>.creole</tt></td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>creole :wiki, :layout_engine => :erb</tt></td>
</tr>
</table>
<p>No es posible llamar métodos desde creole, ni pasarle locales. Por lo tanto,
generalmente va a usarlo en combinación con otro motor de renderizado:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">erb</span> <span class="ss">:resumen</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:texto</span> <span class="o">=></span> <span class="n">cerole</span><span class="p">(</span><span class="ss">:introduccion</span><span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>Debe tomar en cuenta que también puede llamar al método <code class="highlighter-rouge">creole</code> desde otras
plantillas:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="o">%</span><span class="n">h1</span> <span class="no">Hola</span> <span class="no">Desde</span> <span class="no">Haml</span><span class="o">!</span>
<span class="o">%</span><span class="nb">p</span><span class="o">=</span> <span class="n">creole</span><span class="p">(</span><span class="ss">:saludos</span><span class="p">)</span>
</code></pre>
</div>
<p>Como no puedes utilizar Ruby desde Creole, no puedes usar layouts escritos en
Creole. De todos modos, es posible usar un motor de renderizado para el layout
distinto al de la plantilla pasando la opción <code class="highlighter-rouge">:layout_engine</code>.</p>
<h4>MediaWiki Templates</h4>
<table>
<tr>
<td>Dependencia</td>
<td><a href="https://github.com/nricciar/wikicloth" title="WikiCloth">WikiCloth</a></td>
</tr>
<tr>
<td>Extension de Archivo</td>
<td>
<tt>.mediawiki</tt> and <tt>.mw</tt>
</td>
</tr>
<tr>
<td>Ejemplo</td>
<td><tt>mediawiki :wiki, :layout_engine => :erb</tt></td>
</tr>
</table>
<p>No es posible llamar métodos desde el markup de MediaWiki, ni pasar locales al mismo.
Por lo tanto usualmente lo usarás en combinación con otro motor de renderizado:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="n">erb</span> <span class="ss">:overview</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=></span> <span class="p">{</span> <span class="ss">:text</span> <span class="o">=></span> <span class="n">mediawiki</span><span class="p">(</span><span class="ss">:introduction</span><span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>Nota que también puedes llamar al método <code class="highlighter-rouge">mediawiki</code> desde dentro de otras plantillas:</p>
<div class="language-ruby highlighter-rouge">
<pre class="highlight"><code><span class="o">%</span><span class="n">h1</span> <span class="no">Hello</span> <span class="no">From</span> <span class="no">Haml</span><span class="o">!</span>
<span class="o">%</span><span class="nb">p</span><span class="o">=</span> <span class="n">mediawiki</span><span class="p">(</span><span class="ss">:greetings</span><span class="p">)</span>
</code></pre>
</div>
<p>Debido a que no puedes llamar a Ruby desde MediaWiki, no puedes usar los diseños escritos en MediaWiki.
De todas maneras, es posible usar otro motor de renderizado para esa plantilla pasando la opción :layout_engine.</p>
<h4>Plantillas CoffeeScript</h4>
<table>
<tr>
<td>Dependencias</td>
<td>
<a href="https://github.com/josh/ruby-coffee-script" title="Ruby CoffeeScript">
CoffeeScript
</a> y un
<a href="https://github.com/sstephenson/execjs/blob/master/README.md#readme" title="ExecJS">
mecanismo para ejecutar javascript