advanced01.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. Customizing HTML/CSS in Templates
  2. ==================================
  3. Overview
  4. ---------
  5. Wagtail CRX is an extension of Wagtail. You can further customize your site by overriding the
  6. built-in templates to suit your needs. For this tutorial, we will assume that you have basic knowledge
  7. of the Django templating system. You can read more about it by visiting
  8. `Django template language <https://docs.djangoproject.com/en/stable/ref/templates/language/>`_.
  9. The templating language uses a series of ``{% %}`` to pull in content from your page models (found in
  10. the ``models.py`` file) and add minimal logic to the page. This allows it to render the page after content
  11. is added in the CMS and allows you to create multiple pages with the same layout. At the top of the page,
  12. you also want to make sure to either specify that you are **extending a page template** and that you are
  13. pulling in Wagtail tags to make your template work the way it should.
  14. .. note::
  15. If you are completely overriding a template, you will not use the ``{% extends "path/to/template" %}``
  16. at the top of your template. You do, however, need to make sure to use the appropriate template
  17. tags at the top of the template or your template will not render.
  18. The templates directory inside your ``website`` app is empty by default. Any templates you put
  19. in here will override the default wagtailcrx 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 ``wagtailcrx/templates/wagtailcrx/pages/article_page.html``
  22. to ``website/templates/wagtailcrx/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/wagtailcrx/blob/dev/wagtailcrx/templates/wagtailcrx/>`_.
  25. Example 1: Navbar Customization
  26. -------------------------------
  27. The built-in template for the navbar can be found in ``templates/wagtailcrx/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/wagtailcrx/blob/dev/wagtailcrx/templates/wagtailcrx/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. In the ``website`` folder, you should see another folder
  33. called ``templates``. In there are two folders as well: ``website`` and ``wagtailcrx``. The ``wagtailcrx`` template
  34. folder is likely empty at this point because the CMS is pulling in the default templates from source, but you can
  35. add templates to the ``wagtailcrx`` folder **if you are overriding the default templates**.
  36. Most of your custom templates will go into your ``website`` folder because they are not overriding the
  37. default templates in the CMS but either extending them or creating completely new ones specific to
  38. your site.
  39. .. note::
  40. Adding templates to the ``wagtailcrx`` templates folder does not change the default templates
  41. throughout all of Wagtail CRX but does override those specific templates for your website app.
  42. Your ``website`` folder currently only has a folder for ``wagtailcrx`` in the ``templates`` folder.
  43. You can add a new ``website`` folder in ``templates`` (because we will use it in another tutorial),
  44. but for now, you will want to add a ``snippets`` folder inside the ``templates\wagtailcrx`` folder
  45. so that your folder structure looks something like this:
  46. .. figure:: img/advanced_folder_structure1.png
  47. :alt: Our folder structure for templates.
  48. Our folder structure for templates within our website app.
  49. The folder structure needs to be the same as the default folder structure in the CMS if you want to
  50. override the navbar template. Now you should have ``templates\wagtailcrx\snippets``. Navigate to
  51. the ``snippets`` folder and create a ``navbar.html`` file inside of that folder.
  52. **You are now ready to begin customizing the navbar template!**
  53. 1. Examine the default template for the navbar. What code will we want to use from it? You can use
  54. what’s there in your customization.
  55. 2. We will need the Wagtail tags at the top, so copy those and paste them into
  56. your ``navbar.html`` file.
  57. .. code-block:: Django
  58. {% load wagtailcore_tags wagtailsettings_tags wagtailimages_tags wagtailcrx_tags i18n %}
  59. 3. Next, we need to figure out how to move the logo (aka the ``navbar-brand``) into its own section for
  60. the navbar. Maybe we could essentially create two navbars, one that just has the logo and one that has
  61. the menu. Hmm, let’s try that!
  62. 4. We want to preserve the basic functionality of the navbar, so we should keep the tags for CMS settings
  63. and the overall layout inside of a container.
  64. 5. The 2-tiered navbar will have two navbars on top of each other but one will only have the
  65. ``navbar-brand`` (logo) while the other will allow for adding menu items via the CMS. So, the top
  66. navbar is not going to have access to CSS settings in the CMS that are reserved for the main navbar –-
  67. which means that you will need to add any custom classes to the top navbar, such as the background
  68. color or where you want the logo to be placed. Keep that in mind.
  69. .. code-block:: Django
  70. {% load wagtailcore_tags wagtailsettings_tags wagtailimages_tags wagtailcrx_tags i18n %}
  71. {% if not settings.wagtailcrx.LayoutSettings.navbar_wrapper_fluid %}
  72. <div class="container">
  73. {% endif %}
  74. <nav class="navbar navbar-header bg-warning">
  75. {% if not settings.wagtailcrx.LayoutSettings.navbar_content_fluid %}
  76. <div class="container">
  77. {% endif %}
  78. <div>
  79. <a class="navbar-brand" href="/">
  80. {% if settings.wagtailcrx.LayoutSettings.logo %}
  81. {% image settings.wagtailcrx.LayoutSettings.logo original as logo %}
  82. <img class="img-fluid" src="{{logo.url}}" alt="{{site.site_name}}" />
  83. {% else %}
  84. {{site.site_name}}
  85. {% endif %}
  86. </a>
  87. </div>
  88. {% if not settings.wagtailcrx.LayoutSettings.navbar_content_fluid %}
  89. </div><!-- /.container -->
  90. {% endif %}
  91. </nav>
  92. We have set the foundation for the top navbar, which will be the banner section for the logo. Instead of
  93. ``<nav class="navbar {% get_navbar_css %}">``, we have added our own Bootstrap classes since this part of the
  94. navbar will not be getting its CSS settings from the CMS.
  95. However, we did keep the ``{% if settings.wagtailcrx.LayoutSettings.logo %} {% endif %}`` block because we want
  96. to show the name of the site **if no logo is uploaded in the CMS**.
  97. 6. Now we can include the code block for the normal navbar beneath it. Place this code below the ``</nav>`` in
  98. your template. We want to preserve majority of the navbar as-is (without the block for ``navbar-brand``) so that
  99. when we add menu items in the CMS, those items will show up as navigation links.
  100. .. code-block:: Django
  101. <!--Put this below the previous nav closing tag -->
  102. <nav class="navbar {% get_navbar_css %}">
  103. {% if not settings.wagtailcrx.LayoutSettings.navbar_content_fluid %}
  104. <div class="container">
  105. {% endif %}
  106. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
  107. <span class="navbar-toggler-icon"></span>
  108. </button>
  109. <div class="collapse navbar-collapse" id="navbar">
  110. {% get_navbars as navbars %}
  111. {% for navbar in navbars %}
  112. <ul class="navbar-nav {{navbar.custom_css_class}}"
  113. {% if navbar.custom_id %}id="{{navbar.custom_id}}"{% endif %} >
  114. {% for item in navbar.menu_items %}
  115. {% include_block item with liclass="nav-item" aclass="nav-link" ga_event_category="Navbar" %}
  116. {% endfor %}
  117. </ul>
  118. {% endfor %}
  119. {% if settings.wagtailcrx.LayoutSettings.navbar_search %}
  120. <form class="ml-auto form-inline" action="{% url 'codered_search' %}" method="GET">
  121. {% load bootstrap4 %}
  122. {% get_searchform request as form %}
  123. {% bootstrap_form form layout='inline' %}
  124. <div class="form-group">
  125. <button class="btn btn-outline-primary ml-2" type="submit">{% trans 'Search' %}</button>
  126. </div>
  127. </form>
  128. {% endif %}
  129. </div>
  130. {% if not settings.wagtailcrx.LayoutSettings.navbar_content_fluid %}
  131. </div><!-- /.container -->
  132. {% endif %}
  133. </nav>
  134. {% if not settings.wagtailcrx.LayoutSettings.navbar_wrapper_fluid %}
  135. </div><!-- /.container -->
  136. {% endif %}
  137. {# Navbar offset #}
  138. {% if settings.wagtailcrx.LayoutSettings.navbar_fixed %}
  139. {% if settings.wagtailcrx.LayoutSettings.logo %}
  140. <div class="{{settings.wagtailcrx.LayoutSettings.navbar_format}}-fixed-img-offset {{settings.wagtailcrx.LayoutSettings.navbar_collapse_mode}}"></div>
  141. {% else %}
  142. <div class="{{settings.wagtailcrx.LayoutSettings.navbar_format}}-fixed-offset {{settings.wagtailcrx.LayoutSettings.navbar_collapse_mode}}"></div>
  143. {% endif %}
  144. {% endif %}
  145. Let's talk about what is happening here. So, we pulled in the code for the navbar a second time, with the removal of
  146. ``navbar-brand`` section from the original template, but preserved majority of the default code for this section.
  147. The ``if`` statements refer to whether or not some settings are chosen in the CMS and tells the template what to do in those
  148. cases. We also needed to close to top-level ``container``.
  149. Another section that we kept was for the ``navbar-toggler``, which sets the hamburger menu when the screen sizes change.
  150. Finally, we also kept the ``{% get_navbar_css %}`` tag in the class for the ``nav`` because we can use CSS classes for this
  151. navbar from the CMS.
  152. .. note::
  153. To add classes in the CMS, look for the line **Custom CSS Class**, which can be found as a field in sections of
  154. the admin for a snippet or page, or in the **Advanced** section of a Layout Block. This is where you would put a class
  155. like ``bg-warning`` from Bootstrap or a class that you created yourself, like ``logo-banner``.
  156. Adding Custom CSS to the Navbar
  157. '''''''''''''''''''''''''''''''
  158. If you noticed, we have a few custom classes that are not found in Bootstrap. To style our navbar with these classes,
  159. we need to include them in our CSS file and set the styles the way we want. Once you've done that and saved your work,
  160. your navbar is ready to show the world!
  161. CSS files will be found in **website > static > css** in your project folder. Unless you are using SASS, you
  162. will be editing the ``custom.css`` file. For those using SASS, you will want to create a ``navbar.scss`` file in your ``src``
  163. folder and add a link to it in your ``custom.scss`` file.
  164. .. note::
  165. If you want to learn how to use SASS, we really like this tutorial:
  166. `SASS Guide <https://sass-lang.com/guide>`_.
  167. This is the CSS that we used for our navbar:
  168. .. code-block:: CSS
  169. .navbar .nav-link {
  170. font-family: 16px;
  171. text-transform: uppercase;
  172. }
  173. As you can see, you may not need to use a lot of custom CSS. Sometimes a Bootstrap class will work perfectly.
  174. Sometimes all you need to do is customize your template HTML and then add Bootstrap classes as needed. It all
  175. depends on your use case.
  176. For our custom navbar, we needed to un-check the "fixed navbar" option in the CMS via **Settings > Layout** in
  177. order for it to work. Check out what our 2-tiered navbar looks like:
  178. .. figure:: img/advanced_two_tiered_navbar.png
  179. :alt: Our 2-tiered navbar.
  180. Our custom 2-tiered navbar on our website.
  181. Example 2: Footer Customization
  182. -------------------------------
  183. Our footer does not need a customized HTML template; however, we think it does need some custom CSS to make it
  184. look the way we want. Some of our CSS can easily be done in the CMS -- without even touching our CSS file!
  185. First, go to the Footer Snippet in the **Snippets > Footers** admin in CMS. We had previously added a Bootstrap
  186. class of ``bg-warning`` to the Attributes section in the Main Footer, but now we want to add CSS classes to each
  187. of the Layout Blocks for the footer as well.
  188. 1. All of our footer menu items brush up against the top of the footer block. We can add some padding to
  189. the footer using `Bootstrap spacing utilities <https://getbootstrap.com/docs/4.0/utilities/spacing/>`_.
  190. 2. Let's add the padding class ``pt-5`` (which means "padding-top spacer 5") in the Attributes section of
  191. the Main Footer. Save and check it out.
  192. .. figure:: img/advanced_footer_overall_padding.png
  193. :alt: We added padding to the Attributes section of footer.
  194. Our footer with pt-5 added as a class in Attributes section.
  195. 3. We want to change the way that the links look, but it doesn't seem as if there is a Bootstrap class for that.
  196. That means that it's time to go into our CSS file.
  197. 4. We want our links to be that cherry-red, so we will need to use custom CSS and include it in our CSS file.
  198. But we also don't want to make ALL of our links this color. That means we should create a class that can be used
  199. to specify the link. For example, we could add a class called ``cherry-links`` and target the ``a`` tag.
  200. .. code:: CSS
  201. .cherry-links a {
  202. color: #f75990;
  203. }
  204. Then we place the ``cherry-links`` class in the **Advanced** section of the Layout Block that contains the text
  205. for the links, like this:
  206. .. figure:: img/advanced_customcss1.png
  207. :alt: Adding a custom class to the Layout Block
  208. Our custom class added to the Layout Block in CMS
  209. We add it to every Layout Block that needs it. In this case, we have three blocks with links.
  210. .. figure:: img/advanced_footer_front.png
  211. :alt: Our footer right now
  212. Our footer with the custom classes
  213. .. note::
  214. We changed the ``pt-5`` class to ``py-5`` to add padding to the top and bottom. Sometimes you will need to try and
  215. see which classes will give you the results that you want.
  216. What else could we do to make the footer look better? Take some time to play around with Bootstrap classes in the
  217. CMS or create some of your own classes to target elements in the footer.
  218. Making More Drastic CSS Changes Sitewide
  219. ----------------------------------------
  220. **What we did:** So, we went back and changed some of our classes in the HTML template and in the CMS to reflect some
  221. new classes that we created, such as ``bg-lightyellow`` and ``bg-cherry``.
  222. We've also added some additional padding classes in places where we thought it would look good.
  223. Finally, we decided that our logo needed an update as well. So, we swapped our original logo for one
  224. that fit our new color scheme.
  225. This is what our website looks like now with all of our customizations and updates:
  226. .. figure:: img/advanced_improved_website1.png
  227. :alt: Our customized website so far
  228. Our updated and customized website so far
  229. And this is our CSS file at the moment:
  230. .. code:: CSS
  231. /*Navbar */
  232. .navbar .nav-link {
  233. font-family: 16px;
  234. text-transform: uppercase;
  235. }
  236. /* Custom CSS classes */
  237. .cherry-links a {
  238. color: #f75990;
  239. }
  240. .bg-lightyellow {
  241. background-color: #fff685;
  242. }
  243. .bg-cherry {
  244. background-color: #f75990;
  245. }
  246. With the combination of using Bootstrap classes directly in the CMS and making our own classes, which we can use in the CMS
  247. and in custom templates, we can quickly update our site with our changes. There's more that we want to do, but now
  248. we have a good start on a beautiful, customized website!