theming.rst 9.1 KB

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