A new Riff-radio.org site with a static approach.

theming.rst 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. .. title: Theming Nikola
  2. .. slug: theming
  3. .. date: 2012-03-13 12:00:00 UTC-03:00
  4. .. tags:
  5. .. link:
  6. .. description:
  7. .. author: The Nikola Team
  8. :Version: 8.2.3
  9. :Author: Roberto Alsina <ralsina@netmanagers.com.ar>
  10. .. class:: alert alert-primary float-md-right
  11. .. contents::
  12. .. class:: lead
  13. This document is a reference about themes. If you want a tutorial, please read
  14. :doc:`Creating a Theme <creating-a-theme>`. If you’re looking for a ready-made
  15. theme for your site, check out the `Themes Index <https://themes.getnikola.com/>`_.
  16. The Structure
  17. -------------
  18. Themes are located in the ``themes`` folder where Nikola is installed, and in the ``themes`` folder
  19. of your site, one folder per theme. The folder name is the theme name.
  20. A Nikola theme consists of the following folders (they are *all* optional):
  21. assets
  22. This is where you would put your CSS, JavaScript and image files. It will be copied
  23. into ``output/assets`` when you build the site, and the templates will contain
  24. references to them. The default subdirectories are ``css``, ``js``, ``xml``
  25. and ``fonts`` (Bootstrap).
  26. The included themes use `Bootstrap <https://getbootstrap.com/>`_,
  27. `baguetteBox <https://feimosi.github.io/baguetteBox.js/>`_, `Justified Layout by Flickr
  28. <https://flickr.github.io/justified-layout/>`_ and `Luxon
  29. <https://moment.github.io/luxon/>`_, so they are in assets, along with CSS files for
  30. syntax highlighting, reStructuredText and Jupyter, as well as a minified
  31. copy of jQuery.
  32. If you want to base your theme on other frameworks (or on no framework at all)
  33. just remember to put there everything you need for deployment. (Not all of
  34. the listed assets are used by ``base``)
  35. templates
  36. This contains the templates used to generate the pages. While Nikola will use a
  37. certain set of template names by default, you can add others for specific parts
  38. of your site.
  39. messages
  40. Nikola tries to be multilingual. This is where you put the strings for your theme
  41. so that it can be translated into other languages.
  42. less, sass
  43. Files to be compiled into CSS using LESS and Sass (both require plugins)
  44. This mandatory file:
  45. <theme>.theme
  46. An INI file containing theme meta data. The file format is described in
  47. detail below, in the `Theme meta files`_ section.
  48. And these optional files:
  49. parent, engine
  50. One-line text files that contain the names of parent and engine themes,
  51. respectively. Those are needed for older versions (Nikola v7.8.5 and
  52. older).
  53. bundles
  54. A `config <https://docs.python.org/3/library/configparser.html>`_ file
  55. containing a list of files to be turned into bundles. For example:
  56. .. code:: ini
  57. assets/css/all.css=
  58. bootstrap.min.css,
  59. rst_base.css,
  60. nikola_rst.css,
  61. code.css,
  62. baguetteBox.min.css,
  63. theme.css,
  64. custom.css,
  65. This creates a file called "assets/css/all.css" in your output that is the
  66. combination of all the other file paths, relative to the output file.
  67. This makes the page much more efficient because it avoids multiple connections to the server,
  68. at the cost of some extra difficult debugging.
  69. Bundling applies to CSS and JS files.
  70. Templates should use either the bundle or the individual files based on the ``use_bundles``
  71. variable, which in turn is set by the ``USE_BUNDLES`` option.
  72. Theme meta files
  73. ----------------
  74. As of Nikola v7.8.6, Nikola uses meta files for themes. Those are INI files,
  75. with the same name as your theme, and a ``.theme`` extension, eg.
  76. ``bootstrap3.theme``. Here is an example, from the bootstrap3 theme:
  77. .. code:: ini
  78. [Theme]
  79. engine = mako
  80. parent = base
  81. author = The Nikola Contributors
  82. author_url = https://getnikola.com/
  83. based_on = Bootstrap 3 <https://getbootstrap.com/>
  84. license = MIT
  85. tags = bootstrap
  86. [Family]
  87. family = bootstrap3
  88. jinja_version = bootstrap3-jinja
  89. variants = bootstrap3-gradients, bootstrap3-gradients-jinja
  90. [Nikola]
  91. bootswatch = True
  92. The following keys are currently supported:
  93. * ``Theme`` — contains information about the theme.
  94. * ``engine`` — engine used by the theme. Should be ``mako`` or ``jinja``.
  95. * ``parent`` — the parent theme. Any resources missing in this theme, will be
  96. looked up in the parent theme (and then in the grandparent, etc).
  97. The parent is so you don’t have to create a full theme each time: just
  98. create an empty theme, set the parent, and add the bits you want modified.
  99. You **must** define a parent, otherwise many features won’t work due to
  100. missing templates, messages, and assets.
  101. The following settings are recommended:
  102. * If your theme uses Bootstrap 3, inherit the ``bootstrap3`` theme.
  103. * If your theme uses Jinja as a template engine, inherit ``base-jinja``
  104. or ``bootstrap3-jinja``
  105. * In any other case, inherit ``base``.
  106. * ``author``, ``author_url`` — used to identify theme author.
  107. * ``based_on`` — optional list of inspirations, frameworks, etc. used in the
  108. theme. Should be comma-separated, the format ``Name <URL>`` is recommended.
  109. * ``license`` — theme license. Pick MIT if you have no preference.
  110. * ``tags`` — optional list of tags (comma-separated) to describe the theme.
  111. * ``Family`` — contains information about other related themes. All values
  112. optional. (Do not use unless you have related themes.)
  113. * ``family`` — the name of the main theme in a family, which is also used as
  114. the family name.
  115. * ``mako_version``, ``jinja_version`` — name of the mako/jinja version of the
  116. theme.
  117. * ``variants`` — comma-separated list of stylistic variants (other than the
  118. mako/jinja version listed above)
  119. * ``Nikola`` — Nikola-specific information, currently optional.
  120. * ``bootswatch`` — whether or not theme supports Bootswatch styling (optional,
  121. defaults to False)
  122. * ``ignored_assets`` — comma-separated list of assets to ignore (relative to
  123. the ``assets/`` directory, eg. ``css/theme.css``)
  124. Templates
  125. ---------
  126. In templates there is a number of files whose name ends in ``.tmpl``. Those are the
  127. theme’s page templates. They are done using the `Mako <https://www.makotemplates.org>`_
  128. or `Jinja2 <http://jinja.pocoo.org>`_ template languages. If you want to do a theme, you
  129. should learn one first. What engine is used by the theme is declared in the ``engine`` file.
  130. .. Tip::
  131. If you are using Mako templates, and want some extra speed when building the site
  132. you can install Beaker and `make templates be cached <https://docs.makotemplates.org/en/latest/caching.html>`__
  133. Both template engines have a nifty concept of template inheritance. That means that a
  134. template can inherit from another and only change small bits of the output. For example,
  135. ``base.tmpl`` defines the whole layout for a page but has only a placeholder for content
  136. so ``post.tmpl`` only define the content, and the layout is inherited from ``base.tmpl``.
  137. Another concept is theme inheritance. You do not need to duplicate all the
  138. default templates in your theme — you can just override the ones you want
  139. changed, and the rest will come from the parent theme. (Every theme needs a
  140. parent.)
  141. Apart from the `built-in templates`_ listed below, you can add other templates for specific
  142. pages, which the user can then use in his ``POSTS`` or ``PAGES`` option in
  143. ``conf.py``. Also, you can specify a custom template to be used by a post or
  144. page via the ``template`` metadata, and custom templates can be added in the
  145. ``templates/`` folder of your site.
  146. If you want to modify (override) a built-in template, use ``nikola theme -c
  147. <name>.tmpl``. This command will copy the specified template file to the
  148. ``templates/`` directory of your currently used theme.
  149. Keep in mind that your theme is *yours*, so you can require whatever data you
  150. want (eg. you may depend on specific custom ``GLOBAL_CONTEXT`` variables, or
  151. post meta attributes). You don’t need to keep the same theme structure as the
  152. default themes do (although many of those names are hardcoded). Inheriting from
  153. at least ``base`` (or ``base-jinja``) is heavily recommended, but not strictly
  154. required (unless you want to share it on the Themes Index).
  155. Built-in templates
  156. ------------------
  157. These are the templates that come with the included themes:
  158. ``base.tmpl``
  159. This template defines the basic page layout for the site. It’s mostly plain HTML
  160. but defines a few blocks that can be re-defined by inheriting templates.
  161. It has some separate pieces defined in ``base_helper.tmpl``,
  162. ``base_header.tmpl`` and ``base_footer.tmpl`` so they can be
  163. easily overridden.
  164. ``index.tmpl``
  165. Template used to render the multipost indexes. The posts are in a ``posts`` variable.
  166. Some functionality is in the ``index_helper.tmpl`` helper template.
  167. ``archive_navigation_helper.tmpl`` (internal)
  168. Code that implements archive navigation (previous/up/next). Included by
  169. archive templates.
  170. ``archiveindex.tmpl``
  171. Used to display archives, if ``ARCHIVES_ARE_INDEXES`` is True.
  172. By default, it just inherits ``index.tmpl``, with added archive navigation
  173. and feeds.
  174. ``author.tmpl``
  175. Used to display author pages.
  176. ``authorindex.tmpl``
  177. Used to display author indexes, if ``AUTHOR_PAGES_ARE_INDEXES`` is True.
  178. By default, it just inherits ``index.tmpl``, with added feeds.
  179. ``comments_helper.tmpl`` (internal)
  180. This template handles comments. You should probably never touch it :-)
  181. It uses a bunch of helper templates, one for each supported comment system
  182. (all of which start with ``comments_helper``)
  183. ``ui_helper.tmpl``, ``pagination_helper.tmpl``
  184. These templates help render specific UI items, and can be tweaked as needed.
  185. ``gallery.tmpl``
  186. Template used for image galleries. Interesting data includes:
  187. * ``post``: A post object, containing descriptive ``post.text()`` for the gallery.
  188. * ``crumbs``: A list of ``link, crumb`` to implement breadcrumbs.
  189. * ``folders``: A list of folders to implement hierarchical gallery navigation.
  190. * ``enable_comments``: To enable/disable comments in galleries.
  191. * ``thumbnail_size``: The ``THUMBNAIL_SIZE`` option.
  192. * ``photo_array``: a list of dictionaries, each containing:
  193. + ``url``: URL for the full-sized image.
  194. + ``url_thumb``: URL for the thumbnail.
  195. + ``title``: The title of the image.
  196. + ``size``: A dict containing ``w`` and ``h``, the real size of the thumbnail.
  197. * ``photo_array_json``: a JSON dump of photo_array, used by the
  198. ``justified-layout`` script
  199. ``list.tmpl``
  200. Template used to display generic lists of links, which it gets in ``items``,
  201. a list of *(text, link, count)* elements.
  202. ``list_post.tmpl``
  203. Template used to display generic lists of posts, which it gets in ``posts``.
  204. ``listing.tmpl``
  205. Used to display code listings.
  206. ``math_helper.tmpl`` (internal)
  207. Used to add MathJax/KaTeX code to pages.
  208. ``post.tmpl``
  209. Template used by default for blog posts, gets the data in a ``post`` object
  210. which is an instance of the Post class. Some functionality is in the
  211. ``post_helper.tmpl`` and ``post_header.tmpl`` templates.
  212. ``post_list_directive.tmpl``
  213. Template used by the ``post_list`` reStructuredText directive.
  214. ``page.tmpl``
  215. Used for pages that are not part of a blog, usually a cleaner, less
  216. intrusive layout than ``post.tmpl``, but same parameters.
  217. ``tag.tmpl``
  218. Used to show the contents of a single tag or category.
  219. ``tagindex.tmpl``
  220. Used to show the contents of a single tag or category, if ``TAG_PAGES_ARE_INDEXES`` is True.
  221. By default, it just inherits ``index.tmpl``, with added feeds and some
  222. extra features.
  223. ``tags.tmpl``
  224. Used to display the list of tags and categories.
  225. Variables available in templates
  226. --------------------------------
  227. The full, complete list of variables available in templates is maintained in a separate
  228. document: `Template variables <https://getnikola.com/template-variables.html>`_
  229. Customizing themes to user color preference, colorizing category names
  230. ----------------------------------------------------------------------
  231. The user’s preference for theme color is exposed in templates as
  232. ``theme_color`` set in the ``THEME_COLOR`` option.
  233. This theme color is exposed to the browser in default themes — some browsers
  234. might use this color in the user interface (eg. Chrome on Android in light mode
  235. displays the toolbar in this color).
  236. Nikola also comes with support for auto-generating colors similar to a base
  237. color. This can be used with ``theme_color`` and eg. category names. This
  238. feature is exposed to templates as two functions: ``colorize_str(string,
  239. hex_color, presets)`` and ``colorize_str_from_base_color(string, hex_color)``.
  240. If you want to display the category name in the color, first define a list of
  241. overrides in your ``conf.py`` file:
  242. .. code:: python
  243. # end of conf.py
  244. GLOBAL_CONTEXT = {
  245. "category_colors": {
  246. "Blue": "#0000FF"
  247. }
  248. }
  249. With that definition, you can now use ``colorize_str`` in your templates like this:
  250. .. code:: html+mako
  251. <!-- Mako -->
  252. <span style="background-color: ${colorize_str(post.meta('category'), theme_color, category_colors)}">${post.meta('category')}</span>
  253. .. code:: html+jinja
  254. <!-- Jinja2 -->
  255. <span style="background-color: {{ colorize_str(post.meta('category'), theme_color, category_colors) }}">{{ post.meta('category') }}</span>
  256. Note that the category named “Blue” will be displyed in #0000FF due to the
  257. override specified in your config; other categories will have an auto-generated
  258. color visually similar to your theme color.
  259. Hex color values, like that returned by the theme or string colorization can be
  260. altered in the HSL colorspace through the function
  261. ``color_hsl_adjust_hex(hex_string, adjust_h, adjust_s, adjust_l)``.
  262. Adjustments are given in values between 1.0 and -1.0. For example, the theme
  263. color can be made lighter using this code:
  264. .. code:: html+mako
  265. <!-- Mako -->
  266. <span style="color: ${color_hsl_adjust_hex(theme_color, adjust_l=0.05)}">
  267. .. code:: html+jinja
  268. <!-- Jinja2 -->
  269. <span style="color: {{ color_hsl_adjust_hex(theme_color, adjust_l=0.05) }}">
  270. Identifying and customizing different kinds of pages with a shared template
  271. ---------------------------------------------------------------------------
  272. Nikola provides a ``pagekind`` in each template contexts that can be used to
  273. modify shared templates based on the context it’s being used. For example,
  274. the ``base_helper.tmpl`` is used in all pages, ``index.tmpl`` is used in
  275. many contexts and you may want to add or remove something from only one of
  276. these contexts.
  277. Example of conditionally loading different resources on all index pages
  278. (archives, author pages, and tag pages), and others again to the front page
  279. and in every post pages:
  280. .. code:: html+mako
  281. <!-- Mako -->
  282. <head>
  283. % if 'index' in pagekind:
  284. <link href="/assets/css/multicolumn.css" rel="stylesheet">
  285. % endif
  286. % if 'front_page' in pagekind:
  287. <link href="/assets/css/fancy_homepage.css" rel="stylesheet">
  288. <script src="/assets/js/post_carousel.js"></script>
  289. % endif
  290. % if 'post_page' in pagekind:
  291. <link href="/assets/css/article.css" rel="stylesheet">
  292. <script src="/assets/js/comment_system.js"></script>
  293. % endif
  294. </head>
  295. .. code:: html+jinja
  296. <!-- Jinja2 -->
  297. <head>
  298. {% if 'index' in pagekind %}
  299. <link href="/assets/css/multicolumn.css" rel="stylesheet">
  300. {% endif %}
  301. {% if 'front_page' in pagekind %}
  302. <link href="/assets/css/fancy_homepage.css" rel="stylesheet">
  303. <script src="/assets/js/post_carousel.js"></script>
  304. {% endif %}
  305. {% if 'post_page' in pagekind %}
  306. <link href="/assets/css/article.css" rel="stylesheet">
  307. <script src="/assets/js/comment_system.js"></script>
  308. {% endif %}
  309. </head>
  310. Promoting visits to the front page when visiting other filtered
  311. ``index.tmpl`` page variants such as author pages and tag pages. This
  312. could have been included in ``index.tmpl`` or maybe in ``base.tmpl``
  313. depending on what you want to achieve.
  314. .. code:: html+mako
  315. <!-- Mako -->
  316. % if 'index' in pagekind:
  317. % if 'author_page' in pagekind:
  318. <p>These posts were written by ${author}. See posts by all
  319. authors on the <a href="/">front page</a>.</p>
  320. % elif 'tag_page' in pagekind:
  321. <p>This is a filtered selection of posts tagged “${tag}”, visit
  322. the <a href="/">front page</a> to see all posts.</p>
  323. % endif
  324. % endif
  325. .. code:: html+jinja
  326. <!-- Jinja2 -->
  327. {% if 'index' in pagekind %}
  328. {% if 'author_page' in pagekind %}
  329. <p>These posts were written by {{ author }}. See posts by all
  330. authors on the <a href="/">front page</a>.</p>
  331. {% elif 'tag_page' in pagekind %}
  332. <p>This is a filtered selection of posts tagged “{{ tag }}”, visit
  333. the <a href="/">front page</a> to see all posts.</p>
  334. {% endif %}
  335. {% endif %}
  336. List of page kinds provided by default plugins:
  337. * front_page
  338. * index
  339. * index, archive_page
  340. * index, author_page
  341. * index, main_index
  342. * index, tag_page
  343. * list
  344. * list, archive_page
  345. * list, author_page
  346. * list, tag_page
  347. * list, tags_page
  348. * post_page
  349. * page_page
  350. * story_page
  351. * listing
  352. * generic_page
  353. * gallery_front
  354. * gallery_page
  355. Messages and Translations
  356. -------------------------
  357. The included themes are translated into a variety of languages. You can add your own translation
  358. at https://www.transifex.com/projects/p/nikola/
  359. If you want to create a theme that has new strings, and you want those strings to be translatable,
  360. then your theme will need a custom ``messages`` folder.
  361. `LESS <http://lesscss.org/>`__ and `Sass <https://sass-lang.com/>`__
  362. --------------------------------------------------------------------
  363. .. note::
  364. The LESS and Sass compilers were moved to the Plugins Index in
  365. Nikola v7.0.0.
  366. If you want to use those CSS extensions, you can — just store your files
  367. in the ``less`` or ``sass`` directory of your theme.
  368. In order to have them work, you need to create a list of ``.less`` or
  369. ``.scss/.sass`` files to compile — the list should be in a file named
  370. ``targets`` in the respective directory (``less``/``sass``).
  371. The files listed in the ``targets`` file will be passed to the respective
  372. compiler, which you have to install manually (``lessc`` which comes from
  373. the Node.js package named ``less`` or ``sass`` from a Ruby package aptly
  374. named ``sass``). Whatever the compiler outputs will be saved as a CSS
  375. file in your rendered site, with the ``.css`` extension.
  376. .. note::
  377. Conflicts may occur if you have two files with the same base name
  378. but a different extension. Pay attention to how you name your files
  379. or your site won’t build! (Nikola will tell you what’s wrong when
  380. this happens)