theming.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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**. We do not have a ``layout.html`` file because we want to avoid code
  108. duplication and are just falling back to the one that FlaskBB ships with in
  109. its ``templates/`` folder. In order to use your own stylesheets you have to
  110. create a ``layout.html`` file. It's probably the easiest to just copy the
  111. ``layout.html`` from FlaskBB's ``templates/`` folder into your themes
  112. ``templates/`` folder.
  113. For example, the `forums <https://forums.flaskbb.org>`_ on FlaskBB are using
  114. a slightly modified version of the Aurora theme. It is available on GitHub
  115. here: `Aurora Mod <https://github.com/sh4nks/flaskbb-theme-aurora-mod>`_.
  116. The modified version just adds a top navigation and uses a different footer.
  117. Prerequisites
  118. ~~~~~~~~~~~~~
  119. To use the same build tools, which we also use to develop the Aurora theme,
  120. you have to make sure that you have npm installed. You can install npm by
  121. following the official
  122. `installation guide <https://docs.npmjs.com/getting-started/installing-node>`_.
  123. The theme also uses `SASS <https://sass-lang.com/libsass>`_,
  124. a CSS preprocessor, to make development easier. If you are not familar with
  125. SASS but want to use it, which I can really recommend, follow this
  126. `guide <http://sass-lang.com/guide>`_ to get a basic understanding of it.
  127. As explained in `Field Explanation <#field-explanation>`_, each theme must
  128. have a unique theme **identifier** - so open up ``info.json`` (from your
  129. themes folder) with your favorite editor and adjust all the fields properly.
  130. Next, do the same thing for the ``package.json`` file. This file is used by
  131. npm to install some libraries like Bootstrap. A detailed explanation about
  132. all the fields is available from `package.json documentation page`_.
  133. To install the stated requirements in ``package.json`` just run the
  134. ``npm install`` command in the directory where the ``package.json`` file is
  135. located. Now you have set up the toolchain which is used for the Aurora theme.
  136. Toolchain Commands
  137. ~~~~~~~~~~~~~~~~~~
  138. For the build, minify, etc. process we use npm's task runner. Just hit up
  139. ``npm run`` to get a list with all available commands. Following commands are
  140. used::
  141. Usage
  142. npm run [TASK]
  143. Available tasks
  144. clean
  145. rm -f node_modules
  146. autoprefixer
  147. postcss -u autoprefixer -r static/css/*
  148. scss
  149. ./tools/build_css
  150. uglify
  151. ./tools/build_js
  152. imagemin
  153. imagemin src/img/* -o static/img
  154. fonts
  155. ./tools/build_fonts
  156. build:css
  157. npm run scss && npm run autoprefixer
  158. build:js
  159. npm run uglify
  160. build:images
  161. npm run imagemin && npm run fonts
  162. build:all
  163. npm run build:css && npm run build:js && npm run build:images
  164. watch:css
  165. onchange 'src/scss' -- npm run build:css
  166. watch:js
  167. onchange 'src/js' -- npm run build:js
  168. watch:all
  169. npm-run-all -p watch:css watch:js
  170. For example, to watch for changes in our JS and SCSS files,
  171. you just have to run::
  172. npm run watch:all
  173. and upon changes it will automatically rebuild the files.
  174. TL;DR
  175. -----
  176. 1. Create a new folder within the ``themes/`` folder and give it the name
  177. of your theme.
  178. 2. Copy the content of the ``aurora/`` folder into your folder theme's folder.
  179. 3. Create **2** new folders called ``static/`` and ``templates/`` in your
  180. themes folder.
  181. 4. Copy ``layout.html`` from FlaskBB's ``templates/`` into your themes
  182. ``templates/`` folder and modified to your liking. Feel free to copy
  183. other templates over into your themes. Just make sure that they have the
  184. same name and directory structure to overwrite them.
  185. 5. Add some information about your theme using the ``info.json`` file.
  186. 6. Edit the ``package.json`` to your needs.
  187. 7. Happy theming!
  188. In the end your folder structure should look like this::
  189. ── example_theme/
  190. ├── node_modules
  191. │ └── ...
  192. ├── src
  193. │ ├── img
  194. │ │ └── ...
  195. │ ├── js
  196. │ │ └── ...
  197. │ └── scss
  198. │ └── ...
  199. ├── static
  200. │ ├── img
  201. │ ├── css
  202. │ ├── fonts
  203. │ └── js
  204. ├── templates
  205. │ ├── ...
  206. │ └── layout.html
  207. ├── tools
  208. │ ├── build_css
  209. │ ├── build_fonts
  210. │ └── build_js
  211. ├── info.json
  212. ├── LICENSE
  213. ├── package.json
  214. └── README.md
  215. .. _Jinja2: http://jinja.pocoo.org/
  216. .. _Flask: http://flask.pocoo.org/
  217. .. _Flask-Themes2: https://flask-themes2.readthedocs.io/en/latest/
  218. .. _package.json documentation page: https://docs.npmjs.com/files/package.json