]> git.mxchange.org Git - friendica.git/blob - doc/themes.md
Merge pull request #7988 from friendica/MrPetovan-notice
[friendica.git] / doc / themes.md
1 # Themes
2
3 * [Home](help)
4
5 To change the look of friendica you have to touch the themes.
6 The current default theme is [Vier](https://github.com/friendica/friendica/tree/master/view/theme/vier) but there are numerous others.
7 Have a look at [friendica-themes.com](http://friendica-themes.com) for an overview of the existing themes.
8 In case none of them suits your needs, there are several ways to change a theme.
9
10 So, how to work on the UI of friendica.
11
12 You can either directly edit an existing theme.
13 But you might loose your changes when the theme is updated by the friendica team.
14
15 If you are almost happy with an existing theme, the easiest way to cover your needs is to create a new theme, inheritating most of the properties of the parent theme and change just minor stuff.
16 The below for a more detailed description of theme heritage.
17
18 Some themes also allow users to select *variants* of the theme.
19 Those theme variants most often contain an additional [CSS](https://en.wikipedia.org/wiki/CSS) file to override some styling of the default theme values.
20 From the themes in the main repository *duepunto zero* and *vier* are using this methods for variations.
21 Quattro is using a slightly different approach.
22
23 Third you can start your theme from scratch.
24 Which is the most complex way to change friendicas look.
25 But it leaves you the most freedom.
26 So below for a *detailed* description and the meaning of some special files.
27
28 ### Styling
29
30 If you want to change the styling of a theme, have a look at the themes CSS file.
31 In most cases, you can found these in
32
33     /view/theme/**your-theme-name**/style.css
34
35 sometimes, there is also a file called style.php in the theme directory.
36 This is only needed if the theme allowes the user to change certain things of the theme dynamically.
37 Say the font size or set a background image.
38
39 ### Templates
40
41 If you want to change the structure of the theme, you need to change the templates used by the theme.
42 Friendica themes are using [SMARTY3](http://www.smarty.net/) for templating.
43 The default template can be found in
44
45     /view/templates
46
47 if you want to override any template within your theme create your version of the template in
48
49     /view/theme/**your-theme-name**/templates
50
51 any template that exists there will be used instead of the default one.
52
53 ### Javascript
54
55 The same rule applies to the JavaScript files found in
56
57     /js
58
59 they will be overwritten by files in
60
61     /view/theme/**your-theme-name**/js.
62
63 ## Expand an existing Theme
64
65 ### Theme Variations
66
67 Many themes are more *theme families* than only one theme.
68 *duepunto zero* and *vier* allow easily to add new theme variation.
69 We will go through the process of creating a new variation for *duepunto zero*.
70 The same  (well almost, some names change) procedure applies to the *vier* theme.
71 And similar steps are needed for *quattro* but this theme is using [lesscss](http://lesscss.org/#docs) to maintain the CSS files..
72
73 In
74
75     /view/theme/duepuntozero/deriv
76
77 you find a couple of CSS files that define color derivations from the duepunto theme.
78 These resemble some of the now as unsupported marked themes, that were inherited by the duepunto theme.
79 Darkzero and Easter Bunny for example.
80
81 The selection of the colorset is done in a combination of a template for a new form in the settings and aome functions in the theme.php file.
82 The template (theme_settings.tpl)
83
84     {{include file="field_select.tpl" field=$colorset}}
85     <div class="settings-submit-wrapper">
86         <input type="submit" value="{{$submit}}" class="settings-submit" name="duepuntozero-settings-submit" />
87     </div>
88
89 defines a formular consisting of a [select](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select) pull-down which contains all aviable variants and s submit button.
90 See the documentation about [SMARTY3 templates](/help/snarty3-templates.md) for a summary of friendica specific blocks other than the select element.
91 But we don't really need to change anything at the template itself.
92
93 The template alone wont work though.
94 You make friendica aware of its existance and tell it how to use the template file, by defining a config.php file.
95 It needs to define at least the following functions
96
97 * theme_content
98 * theme_post
99
100 and may also define functions for the admin interface
101
102 * theme_admin
103 * theme_admin_post.
104
105 theme_content and theme_admin are used to make the form available in the settings, repectively the admin panel.
106 The _post functions handle the processing of the send form, in this case they save to selected variand in friendicas database.
107
108 To make your own variation appear in the menu, all you need to do is to create a new CSS file in the deriv directoy and include it in the array in the config.php:
109
110     $colorset = array(
111         'default'=>L10n::t('default'),
112         'greenzero'=>L10n::t('greenzero'),
113         'purplezero'=>L10n::t('purplezero'),
114         'easterbunny'=>L10n::t('easterbunny'),
115         'darkzero'=>L10n::t('darkzero'),
116         'comix'=>L10n::t('comix'),
117         'slackr'=>L10n::t('slackr'),
118     );
119
120 the 1st part of the line is the name of the CSS file (without the .css) the 2nd part is the common name of the variant.
121 Calling the L10n::t() function with the common name makes the string translateable.
122 The selected 1st part will be saved in the database by the theme_post function.
123
124     function theme_post(App $a){
125         // non local users shall not pass
126         if (! local_user()) {
127             return;
128         }
129         // if the one specific submit button was pressed then proceed
130         if (isset($_POST['duepuntozero-settings-submit'])){
131             // and save the selection key into the personal config of the user
132             PConfig::set(local_user(), 'duepuntozero', 'colorset', $_POST['duepuntozero_colorset']);
133         }
134     }
135
136 Now that this information is set in the database, what should friendica do with it?
137 For this, have a look at the theme.php file of the *duepunto zero*.
138 There you'll find somethink alike
139
140         $colorset = PConfig::get( local_user(), 'duepuntozero','colorset');
141         if (!$colorset)
142             $colorset = Config::get('duepuntozero', 'colorset');
143         if ($colorset) {
144             if ($colorset == 'greenzero')
145                 $a->page['htmlhead'] .= '<link rel="stylesheet" href="view/theme/duepuntozero/deriv/greenzero.css" type="text/css" media="screen" />'."\n";
146             /* some more variants */
147         }
148
149 which tells friendica to get the personal config of a user.
150 Check if it is set and if not look for the global config.
151 And finally if a config for the colorset was found, apply it by adding a link to the CSS file into the HTML header of the page.
152 So you'll just need to add a if selection, fitting your variant keyword and link to the CSS file of it.
153
154 Done.
155 Now you can use the variant on your system.
156 But remember once the theme.php or the config.php you have to readd your variant to them.
157 If you think your color variation could be benifical for other friendica users as well, feel free to generate a pull request at github so we can include your work into the repository.
158
159 ### Inheritation
160
161 Say, you like the duepuntozero but you want to have the content of the outer columns left and right exchanged.
162 That would be not a color variation as shown above.
163 Instead we will create a new theme, duepuntozero_lr, inherit the properties of duepuntozero and make small changes to the underlying php files.
164
165 So create a directory called duepunto_lr and create a file called theme.php with your favorite text editor.
166 The content of this file should be something like
167
168     <?php
169     /* meta informations for the theme, see below */
170     use Friendica\App;
171     
172     function duepuntozero_lr_init(App $a) {
173         $a-> theme_info = array(
174             'extends' => 'duepuntozero'.
175         );
176         $a->set_template_engine('smarty3');
177         /* and more stuff e.g. the JavaScript function for the header */
178     }
179
180 Next take the default.php file found in the /view direcotry and exchange the aside and right_aside elements.
181 So the central part of the file now looks like this:
182
183     <body>
184         <?php if(!empty($page['nav'])) echo $page['nav']; ?>
185         <aside><?php if(!empty($page['right_aside'])) echo $page['right_aside']; ?></aside>
186         <section><?php if(!empty($page['content'])) echo $page['content']; ?>
187                 <div id="page-footer"></div>
188         </section>
189         <right_aside><?php if(!empty($page['aside'])) echo $page['aside']; ?></right_aside>
190         <footer><?php if(!empty($page['footer'])) echo $page['footer']; ?></footer>
191     </body>
192
193 Finally we need a style.css file, inheriting the definitions from the parent theme and containing out changes for the new theme.
194 ***Note***:You need to create the style.css and at lest import the base CSS file from the parent theme.
195
196     @import url('../duepuntozero/style.css');
197
198 Done.
199 But I agree it is not really useful at this state.
200 Nevertheless, to use it, you just need to activate in the admin panel.
201 That done, you can select it in the settings like any other activated theme.
202
203 ## Creating a Theme from Scratch
204
205 Keep patient.
206 Basically what you have to do is identify which template you have to change so it looks more like what you want.
207 Adopt the CSS of the theme accordingly.
208 And iterate the process until you have the theme the way you want it.
209
210 *Use the source Luke.* and don't hesitate to ask in @[developers](https://forum.friendi.ca/profile/developers) or @[helpers](https://forum.friendi.ca/profile/helpers).
211
212 ## Special Files
213
214 ### unsupported
215
216 If a file with this name (which might be empty) exists in the theme directory, the theme is marked as *unsupported*.
217 An unsupported theme may not be selected by a user in the settings.
218 Users who are already using it wont notice anything.
219
220 ### README(.md)
221
222 The contents of this file, with or without the .md which indicates [Markdown](https://daringfireball.net/projects/markdown/) syntax, will be displayed at most repository hosting services and in the theme page within the admin panel of friendica.
223
224 This file should contain information you want to let others know about your theme.
225
226 ### screenshot.[png|jpg]
227
228 If you want to have a preview image of your theme displayed in the settings you should take a screenshot and save it with this name.
229 Supported formats are PNG and JPEG.
230
231 ### theme.php
232
233 This is the main definition file of the theme.
234 In the header of that file, some meta information is stored.
235 For example, have a look at the theme.php of the *quattro* theme:
236
237     <?php
238     /**
239      * Name: Quattro
240      * Version: 0.6
241      * Author: Fabio <http://kirgroup.com/profile/fabrixxm>
242      * Maintainer: Fabio <http://kirgroup.com/profile/fabrixxm>
243      * Maintainer: Tobias <https://f.diekershoff.de/profile/tobias>
244      */
245
246 You see the definition of the theme's name, it's version and the initial author of the theme.
247 These three pieces of information should be listed.
248 If the original author is no longer working on the theme, but a maintainer has taken over, the maintainer should be listed as well.
249 The information from the theme header will be displayed in the admin panel.
250
251 The first thing in file is to import the `App` class from `\Friendica\` namespace.
252
253     use Friendica\App;
254
255 This will make our job a little easier, as we don't have to specify the full name every time we need to use the `App` class.
256
257 The next crucial part of the theme.php file is a definition of an init function.
258 The name of the function is <theme-name>_init.
259 So in the case of quattro it is
260
261     function quattro_init(App $a) {
262       $a->theme_info = array();
263       $a->set_template_engine('smarty3');
264     }
265
266 Here we have set the basic theme information, in this case they are empty.
267 But the array needs to be set.
268 And we have set the template engine that should be used by friendica for this theme.
269 At the moment you should use the *smarty3* engine.
270 There once was a friendica specific templating engine as well but that is not used anymore.
271 If you like to use another templating engine, please implement it.
272
273 When you want to inherit stuff from another theme you have to *announce* this in the theme_info:
274
275     $a->theme_info = array(
276       'extends' => 'duepuntozero',
277     );
278
279 which declares *duepuntozero* as parent of the theme.
280
281 If you want to add something to the HTML header of the theme, one way to do so is by adding it to the theme.php file.
282 To do so, add something alike
283
284     $a->page['htmlhead'] .= <<< EOT
285     /* stuff you want to add to the header */
286     EOT;
287
288 The $a variable holds the friendica application.
289 So you can access the properties of this friendica session from the theme.php file as well.
290
291 ### default.php
292
293 This file covers the structure of the underlying HTML layout.
294 The default file is in
295
296     /view/default.php
297
298 if you want to change it, say adding a 4th column for banners of your favourite FLOSS projects, place a new default.php file in your theme directory.
299 As with the theme.php file, you can use the properties of the $a variable with holds the friendica application to decide what content is displayed.