theming.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. .. _theming:
  2. Theming
  3. =======
  4. FlaskBB uses the `Flask-Themes2`_ extension for theming.
  5. Getting Started
  6. ---------------
  7. A theme is simply a folder containing static media (like CSS files, images,
  8. and JavaScript) and Jinja2 templates, with some metadata.
  9. A theme folder should look something like this::
  10. my_theme
  11. ├── info.json
  12. ├── LICENSE
  13. ├── static
  14. │ └── style.css
  15. └── templates
  16. └── layout.html
  17. Every theme needs to have a file called **info.json**. The info.json file
  18. contains the theme’s metadata, so that the application can provide a nice
  19. switching interface if necessary. For example, the info.json file for the
  20. aurora theme looks like this:
  21. .. sourcecode:: json
  22. {
  23. "application": "flaskbb",
  24. "identifier": "aurora",
  25. "name": "Aurora",
  26. "author": "Peter Justin",
  27. "license": "BSD 3-Clause",
  28. "website": "https://flaskbb.org",
  29. "description": "The default theme for FlaskBB.",
  30. "preview": "preview.png",
  31. "version": "1.0.0"
  32. }
  33. Field Explanation
  34. ~~~~~~~~~~~~~~~~~
  35. **application**
  36. The name of the application, in our case this should always be **flaskbb**.
  37. **identifier**
  38. The unique name of your theme. This identifier should match the themes
  39. folder name!
  40. **name**
  41. Human readable name of the theme.
  42. **author**
  43. The name of the author.
  44. **license**
  45. A short phrase describing the license, like "GPL", "BSD", "Public Domain",
  46. or "Creative Commons BY-SA 3.0". Every theme should define a license
  47. under which terms the theme can be used. You should also put a copy
  48. of the license in your themes directory (e.g. in a LICENSE file).
  49. **description**
  50. A short description about your theme.
  51. For example: "A minimalistic blue theme".
  52. **website**
  53. The URL of the theme’s Web site. This can be a Web site specifically
  54. for this theme, Web site for a collection of themes that includes
  55. this theme, or just the author’s Web site.
  56. **preview**
  57. The theme's preview image, within the static folder.
  58. **version**
  59. The version of the theme.
  60. Templates
  61. ~~~~~~~~~
  62. `Flask`_ and therefore also FlaskBB uses the `Jinja2`_ templating engine,
  63. so you should read `its documentation <http://jinja.pocoo.org/docs/templates>`_
  64. to learn about the actual syntax of the templates.
  65. All templates are by default loaded from FlaskBB's ``templates/`` folder. In
  66. order to create your own theme, you have to create a ``templates/`` folder in
  67. your themes directory and optionally also copy the ``layout.html`` file from
  68. FlaskBB's template folder over to yours. This ``layout.html`` file is your
  69. starting point. Every template will extend it. If you want to overwrite other
  70. templates, just copy them over from the templates folder and modify them
  71. to your liking.
  72. Each loaded template will have a global function named `theme`
  73. available to look up the theme's templates. For example, if you want to
  74. extend, import, or include another template from your theme, you can use
  75. ``theme(template_name)``, like this:
  76. .. sourcecode:: html+jinja
  77. {% extends theme('layout.html') %}
  78. {% from theme('macros.html') import horizontal_field %}
  79. .. note::
  80. If the template you requested **doesn't** exist within the theme, it will
  81. **fallback** to using the application's template.
  82. If you pass `false` as the second parameter, it will only return the theme's template.
  83. .. sourcecode:: html+jinja
  84. {# This template, for example, does not exist in FlaskBB #}
  85. {% include theme('header.html', false) %}
  86. You can also explicitly import/include templates from FlaskBB. Just use the
  87. tag without calling `theme`.
  88. .. sourcecode:: html+jinja
  89. {% from 'macros.html' import topnav %}
  90. You can also get the URL for the theme's media files with the `theme_static`
  91. function:
  92. .. sourcecode:: html+jinja
  93. <link rel=stylesheet href="{{ theme_static('style.css') }}">
  94. To include the static files that FlaskBB ships with, you just proceed as
  95. normal:
  96. .. sourcecode:: html+jinja
  97. <link rel="stylesheet" href="{{ url_for('static', filename='css/pygments.css') }}">
  98. If you want to get information about the currently active theme, you can do
  99. that with the `theme_get_info` function:
  100. .. sourcecode:: html+jinja
  101. This theme is <a href="{{ theme_get_info('website'}}">
  102. <b>{{ theme_get_info('name') }}</b>
  103. </a>
  104. Advanced Example
  105. -----------------
  106. A more advanced example of a theme, is our own default theme called
  107. **Aurora**. It does not provide an ``layout.html`` because I want to avoid
  108. code duplication and it is easier to maintain as well. For your own theme,
  109. please copy the ``layout.html`` from FlaskBB's ``templates/`` folder into your
  110. themes ``templates/`` folder.
  111. Prerequisites
  112. ~~~~~~~~~~~~~
  113. To use the same build tools, which we also use to develop the Aurora theme,
  114. you have to make sure that you have npm installed. You can install npm by
  115. following the official
  116. `installation guide <https://docs.npmjs.com/getting-started/installing-node>`_.
  117. The theme also uses `SASS <https://sass-lang.com/libsass>`_,
  118. a CSS preprocessor, to make development easier. If you are not familar with
  119. SASS but want to use it, which I can really recommend, follow this
  120. `guide <http://sass-lang.com/guide>`_ to get a basic understanding of it.
  121. As explained in `Field Explanation <#field-explanation>`_, each theme must
  122. have a unique theme **identifier** - so open up ``info.json`` (from your
  123. themes folder) with your favorite editor and adjust all the fields properly.
  124. Next, do the same thing for the ``package.json`` file. This file is used by
  125. npm to install some libraries like Bootstrap. A detailed explanation about
  126. all the fields is available from `package.json documentation page`_.
  127. To install the stated requirements in ``package.json`` just run the
  128. ``npm install`` command in the directory where the ``package.json`` file is
  129. located. Now you have set up the toolchain which is used for the Aurora theme.
  130. Toolchain Commands
  131. ~~~~~~~~~~~~~~~~~~
  132. For the build, minify, etc. process we use npm's task runner. Just hit up
  133. ``npm run`` to get a list with all available commands. Following commands are
  134. used::
  135. Usage
  136. npm run [TASK]
  137. Available tasks
  138. clean
  139. rm -f node_modules
  140. autoprefixer
  141. postcss -u autoprefixer -r static/css/*
  142. scss
  143. ./tools/build_css
  144. uglify
  145. ./tools/build_js
  146. imagemin
  147. imagemin src/img/* -o static/img
  148. fonts
  149. ./tools/build_fonts
  150. build:css
  151. npm run scss && npm run autoprefixer
  152. build:js
  153. npm run uglify
  154. build:images
  155. npm run imagemin && npm run fonts
  156. build:all
  157. npm run build:css && npm run build:js && npm run build:images
  158. watch:css
  159. onchange 'src/scss' -- npm run build:css
  160. watch:js
  161. onchange 'src/js' -- npm run build:js
  162. watch:all
  163. npm-run-all -p watch:css watch:js
  164. For example, to watch for changes in our JS and SCSS files,
  165. you just have to run::
  166. npm run watch:all
  167. and upon changes it will automatically rebuild the files.
  168. TL;DR
  169. -----
  170. 1. Create a new folder within the ``themes/`` folder and give it the name
  171. of your theme.
  172. 2. Copy the content of the ``aurora/`` folder into your folder theme's folder.
  173. 3. Create **2** new folders called ``static/`` and ``templates/`` in your
  174. themes folder.
  175. 4. Copy ``layout.html`` from FlaskBB's ``templates/`` into your themes
  176. ``templates/`` folder and modified to your liking. Feel free to copy
  177. other templates over into your themes. Just make sure that they have the
  178. same name and directory structure to overwrite them.
  179. 5. Add some information about your theme using the ``info.json`` file.
  180. 6. Edit the ``package.json`` to your needs.
  181. 7. Happy theming!
  182. In the end your folder structure should look like this::
  183. ── example_theme/
  184. ├── node_modules
  185. │ └── ...
  186. ├── src
  187. │ ├── img
  188. │ │ └── ...
  189. │ ├── js
  190. │ │ └── ...
  191. │ └── scss
  192. │ └── ...
  193. ├── static
  194. │ ├── img
  195. │ ├── css
  196. │ ├── fonts
  197. │ └── js
  198. ├── templates
  199. │ ├── ...
  200. │ └── layout.html
  201. ├── tools
  202. │ ├── build_css
  203. │ ├── build_fonts
  204. │ └── build_js
  205. ├── info.json
  206. ├── LICENSE
  207. ├── package.json
  208. └── README.md
  209. .. _Jinja2: http://jinja.pocoo.org/
  210. .. _Flask: http://flask.pocoo.org/
  211. .. _Flask-Themes2: https://flask-themes2.readthedocs.io/en/latest/
  212. .. _package.json documentation page: https://docs.npmjs.com/files/package.json