advanced01.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. .. _customizing_templates:
  2. Customizing Templates & CSS
  3. ===========================
  4. Overview
  5. ---------
  6. Wagtail CRX is an extension of Wagtail. You can further customize your site by overriding the
  7. built-in templates to suit your needs. For this tutorial, we will assume that you have basic knowledge
  8. of the Django templating system. You can read more about it by visiting
  9. `Django template language <https://docs.djangoproject.com/en/stable/ref/templates/language/>`_.
  10. The templating language uses a series of ``{% %}`` to pull in content from your page models (found in
  11. the ``models.py`` file) and add minimal logic to the page. This allows it to render the page after content
  12. is added in the CMS and allows you to create multiple pages with the same layout. At the top of the page,
  13. you also want to make sure to either specify that you are **extending a page template** and that you are
  14. pulling in Wagtail tags to make your template work the way it should.
  15. .. note::
  16. If you are completely overriding a template, you will not use the ``{% extends "path/to/template" %}``
  17. at the top of your template. You do, however, need to make sure to use the appropriate template
  18. tags at the top of the template or your template will not render.
  19. The templates directory inside your ``website`` app will override the default coderedcms templates if they follow the same name and directory
  20. structure. This uses the standard Django template rendering engine. For example, to change the
  21. formatting of the article page, copy ``coderedcms/templates/coderedcms/pages/article_page.html``
  22. to ``website/templates/coderedcms/pages/article_page.html`` and modify it.
  23. The `source code for built-in templates can be found on GitHub
  24. <https://github.com/coderedcorp/coderedcms/blob/main/coderedcms/templates/coderedcms/>`_.
  25. Example 1: Navbar Customization
  26. -------------------------------
  27. The built-in template for the navbar can be found in ``templates/coderedcms/snippets/navbar.html``. This
  28. file may not actually be in your installation folders for your site; however, you can see its contents
  29. by visiting the Wagtail CRX source code here: `navbar.html <https://github.com/coderedcorp/coderedcms/blob/main/coderedcms/templates/coderedcms/snippets/navbar.html>`_.
  30. Let’s say that you want to have a 2-tiered navbar with the logo on the top tier and the menu items on the
  31. second tier. The default navbar does not have that as an option, so you will want to override this template.
  32. Look at your folder structure for your project. By default, your project starts with a "website" folder (also known as a directory).
  33. In that directory there will be this path to base.html `templates/coderedcms/pages/base.html.`. This file is here for your convenience,
  34. you can add imports (like a google font) and it will override the default ``coderedcms`` base.html template. In the `website/templates/coderedcms` folder
  35. you are able override other ``coderedcms`` templates. To do this, the file structure and name must match the default templates.
  36. The `source code for built-in templates can be found on GitHub <https://github.com/coderedcorp/coderedcms/blob/main/coderedcms/templates/coderedcms/>`_.
  37. Many of your custom templates will go into your ``website`` folder because they are not overriding the
  38. default templates in the CMS but either extending them or creating completely new ones specific to
  39. your site.
  40. .. note::
  41. Adding templates to the ``coderedcms`` templates folder does not change the default templates
  42. throughout all of Wagtail CRX but does override those specific templates for your website app.
  43. Your ``website`` folder currently only has a folder for ``coderedcms`` in the ``templates`` folder.
  44. You can add a new ``website`` folder in ``templates`` (because we will use it in another tutorial),
  45. but for now, you will want to add a ``snippets`` folder inside the ``templates\coderedcms`` folder
  46. so that your folder structure looks something like this:
  47. .. figure:: img/A01/advanced_folder_structure1.png
  48. :alt: Our folder structure for templates.
  49. Our folder structure for templates within our website app.
  50. The folder structure needs to be the same as the default folder structure in the CMS if you want to
  51. override the navbar template. Now you should have ``templates\coderedcms\snippets``. Navigate to
  52. the ``snippets`` folder and create a ``navbar.html`` file inside of that folder. With your server running and the newly created file navbar.html file in place,
  53. look at your website. The navbar should be gone. The empty file is now overriding the default template.
  54. **You are now ready to begin customizing the navbar template!**
  55. 1. Let's copy and paste the `navbar.html template from github. <https://github.com/coderedcorp/coderedcms/blob/dev/coderedcms/templates/coderedcms/snippets/navbar.html>`_.
  56. .. figure:: img/A01/navbar_html.jpeg
  57. :alt: The code for navbar
  58. 2. Look at your homepage, and you'll see the navbar is back to normal.
  59. 3. To make a two 2-tiered Navbar lets move the logo <a> tag above the <nav> tag.
  60. 4. In our navbar.html file, Highlight from line 5 to 12. Use control + x to cut the code out of the document.
  61. 5. Move your cursor to the end of line 2, hit enter to create a new line and control + C to paste the code we just cut.
  62. 6. Copy the <nav class="navbar {% get_navbar_css %}"> tag.
  63. 7. Use <nav class="navbar {% get_navbar_css %}"> to wrap the logo <a> tag we just moved. Remember the closing </nav> tag
  64. 8. We also need to add a Bootstrap class for the logo to be in the center. Use `justify-content-center` in <nav> wrapper.
  65. .. figure:: img/A01/navbar_html.jpeg
  66. :alt: updated code for navbar
  67. 9. Be sure to save the template, then refresh your browser.
  68. .. figure:: img/A01/logo_centered.jpeg
  69. :alt: preview of navbar
  70. .. _adding_custom_css:
  71. Adding Custom CSS
  72. -----------------
  73. Basic installation:
  74. If you are using the basic installation version, a custom.css file will be found in **website > static > css** in your project folder. There you can add custom CSS.
  75. After you add code, all you need to do is save the file and hard refresh your browser to see the changes.
  76. This is a great option if are using a built in theme, the default color theme, a purchased theme, or don't want to deal with compiling the sass for every CSS change.
  77. Once you create a class you can use it anywhere by adding it using the advanced setting on a block and the custom CSS class field. This is also where you can add any bootstrap class.
  78. Be Sure to check which bootstrap version your project is on and to look at the correct documentation. There are a fair number of changes from 4.5 to 5.2.
  79. Let's look at adding a CSS class on a basic install version of `coderedcms`.
  80. * First let's make a class.
  81. * We make a class with text-shadow that can be used anywhere see fit on the site.
  82. * Open **website > static > css > custom.css** in an editor. (I'm using VS code, which is free)
  83. * Add the following code:
  84. .. code-block:: CSS
  85. .text-shadow {
  86. text-shadow: black 1px 1px 12px;
  87. }
  88. .. figure:: img/A01/css_demo.jpg
  89. :alt: screen shot of custom.css
  90. A screen shot of custom.css with the above code added. (screen shot in VS code)
  91. * Save the file.
  92. * Open the admin screen for your page.
  93. * Find where you need the class.
  94. * Open Advanced Settings and type in the class name.
  95. * **Save** and **Publish** or **Preview**
  96. In this example I made a **Hero Unit** with a **Responsive Grid Row** and put my class on the **Column**.
  97. .. figure:: img/A01/custom_css.jpeg
  98. :alt: inserted text-shadow css class
  99. .. figure:: img/A01/before.jpeg
  100. :alt: hard to read hero block
  101. Before custom css class
  102. .. figure:: img/A01/after.jpeg
  103. :alt: easy to read hero block
  104. After custom css class
  105. This is a relatively simple example. You can also use any of the bootstrap utility classes when constructing your site.
  106. Sass installation:
  107. ------------------
  108. We used sass for the tutorial. It goes over how to change :ref:`global-sass-colors` in the _variables.scss.
  109. It also covers adding your own :ref:`custom_css` in the custom.scss file.
  110. The most important things to remember are compile the sass after changes are made and hard refresh the browser after the sass compiles.
  111. Here are the steps to compile the sass. In an activated terminal:
  112. * Stop the server if it's running (control + c)
  113. * Compile the sass with this command:
  114. * ``python manage.py sass website/static/website/src/custom.scss website/static/website/css/custom.css``
  115. * If there are any errors with compiling fix the errors and re-compile.
  116. * Once the sass is compiled (it will say "done" in the terminal) restart the server.
  117. * ``python manage.py runserver``
  118. * Go back to your browser and hard refresh the page. (I usually hold control and click the refresh)
  119. .. note::
  120. If you want to learn more about SASS, we really like this tutorial:
  121. `SASS Guide <https://sass-lang.com/guide>`_