lib: move some examples/* into specific subdirectories of their lib
[nit.git] / lib / template / examples / tmpl_composer.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import template
16
17 ### Here, definition of the specific templates
18
19 # The root template for composers
20 class TmplComposers
21 super Template
22
23 # Short list of composers
24 var composers = new Array[TmplComposer]
25
26 # Detailled list of composers
27 var composer_details = new Array[TmplComposerDetail]
28
29 # Add a composer in both lists
30 fun add_composer(firstname, lastname: String, birth, death: Int)
31 do
32 composers.add(new TmplComposer(lastname))
33 composer_details.add(new TmplComposerDetail(firstname, lastname, birth, death))
34 end
35
36 redef fun rendering do
37 add """
38 COMPOSERS
39 =========
40 """
41 add_all composers
42 add """
43
44 DETAILS
45 =======
46 """
47 add_all composer_details
48 end
49 end
50
51 # A composer in the short list of composers
52 class TmplComposer
53 super Template
54
55 # Short name
56 var name: String
57
58 init(name: String) do self.name = name
59
60 redef fun rendering do add "- {name}\n"
61 end
62
63 # A composer in the detailled list of composers
64 class TmplComposerDetail
65 super Template
66
67 var firstname: String
68 var lastname: String
69 var birth: Int
70 var death: Int
71
72 init(firstname, lastname: String, birth, death: Int) do
73 self.firstname = firstname
74 self.lastname = lastname
75 self.birth = birth
76 self.death = death
77 end
78
79 redef fun rendering do add """
80
81 COMPOSER: {{{firstname}}} {{{lastname}}}
82 BIRTH...: {{{birth}}}
83 DEATH...: {{{death}}}
84 """
85
86 end
87
88 ### Here a simple usage of the templates
89
90 var f = new TmplComposers
91 f.add_composer("Johann Sebastian", "Bach", 1685, 1750)
92 f.add_composer("George Frideric", "Handel", 1685, 1759)
93 f.add_composer("Wolfgang Amadeus", "Mozart", 1756, 1791)
94 f.write_to(stdout)