|
| 1 | +:navigation-title: Content Element rendering |
| 2 | +.. include:: /Includes.rst.txt |
| 3 | + |
| 4 | +.. _content-element-rendering: |
| 5 | + |
| 6 | +==================================================== |
| 7 | +Overriding the default templates of content elements |
| 8 | +==================================================== |
| 9 | + |
| 10 | +The content elements that are rendered up to this point are rendered by the |
| 11 | +TYPO3 system extension Fluid Styled Content (:composer:`typo3/cms-fluid-styled-content`). |
| 12 | + |
| 13 | +This extensions offers default templates to render content elements. Without |
| 14 | +such an extension, no content would be rendered at all. The default |
| 15 | +templates provided by this extension can be overridden with site settings |
| 16 | +provided by Fluid Styled Content. |
| 17 | + |
| 18 | +.. contents:: |
| 19 | + |
| 20 | +.. _content-element-rendering-settings: |
| 21 | + |
| 22 | +Use Settings to override template paths of Fluid Styled Content |
| 23 | +=============================================================== |
| 24 | + |
| 25 | +Site settings can be saved both in the site configuration and in the site |
| 26 | +package extension. |
| 27 | + |
| 28 | +We will save the settings to the site package but use the settings editor to |
| 29 | +write the YAML for us. |
| 30 | + |
| 31 | +Go to module :guilabel:`Site Management > Settings` and edit the settings of |
| 32 | +your site. Override the paths to the templates of Fluid Styled Content like this: |
| 33 | + |
| 34 | +.. figure:: /Images/SiteSettingsFsc.png |
| 35 | + :alt: Screenshot demonstrating the site settings module |
| 36 | + |
| 37 | + Override the templates of Fluid Styled Content |
| 38 | + |
| 39 | +If you would click :guilabel:`Save` now, the settings would be saved to your |
| 40 | +site configuration at :path:`config/sites/my-site/settings.yaml`. We however |
| 41 | +want to save the settings to the site set of our site package extension. |
| 42 | + |
| 43 | +Click the button :guilabel:`YAML export` to copy the configuration to your |
| 44 | +Clipboard instead, then save it to the following file: |
| 45 | + |
| 46 | +.. code-block:: yaml |
| 47 | + :caption: packages/site_package/Configuration/Sets/SitePackage/settings.yaml |
| 48 | +
|
| 49 | + styles: |
| 50 | + templates: |
| 51 | + templateRootPath: 'EXT:site_package/Resources/Private/ContentElements/Templates' |
| 52 | + partialRootPath: 'EXT:site_package/Resources/Private/ContentElements/Partials' |
| 53 | + layoutRootPath: 'EXT:site_package/Resources/Private/ContentElements/Layouts' |
| 54 | +
|
| 55 | +Then close the settings editor without saving. You can now start overriding the |
| 56 | +templates: |
| 57 | + |
| 58 | +.. _content-element-rendering-menu-subpages: |
| 59 | + |
| 60 | +Override the "Menu of Subpages" template |
| 61 | +======================================== |
| 62 | + |
| 63 | +On "Page 1" of the example data a content element of type "Subpages" was added. |
| 64 | +We now have to find out what this type is called in the database. The raw values |
| 65 | +saved to the database are displayed in the TYPO3 backend when the debug mode |
| 66 | +is activated: |
| 67 | + |
| 68 | +.. figure:: /Images/MenuContentElement.png |
| 69 | + :alt: Screenshot demonstrating the backend debug mode for Content element "Subpages" on "Page 1" |
| 70 | + |
| 71 | + You can now see that the Type (saved in field `CType`) is stored as `menu_subpages` |
| 72 | + |
| 73 | +Now we must find and copy the original template from Fluid Styled Content. TYPO3 |
| 74 | +extensions are saved by their composer key, here :composer:`typo3/cms-fluid-styled-content`, |
| 75 | +into the folder :path:`vendor` during installation via Composer. You can find |
| 76 | +the files belonging to Fluid Styled Content in folder |
| 77 | +:path:`vendor/typo3/cms-fluid-styled-content` therefore. This folder is |
| 78 | +structured similarly to your site package extension and you can find the original |
| 79 | +templates in folder :path:`Resources/Private/Templates` here. |
| 80 | + |
| 81 | +By convention the templates of Fluid Styled Content have the name of the `CType` in CamelCase. Copy file |
| 82 | +:path:`vendor/typo3/cms-fluid-styled-content/Resources/Private/Templates/MenuSubpages.html` |
| 83 | +into folder :path:`packages/my_site_package/Resources/Private/ContentElements/Templates/MenuSubpages.html` |
| 84 | + |
| 85 | +Edit the file to add some classes as used in menus in Bootstrap, for example |
| 86 | +like this: |
| 87 | + |
| 88 | +.. include:: /CodeSnippets/Fluid/MenuSubpages.rst.txt |
| 89 | + |
| 90 | +In most parts the changes we made are pretty straight forward. In line 9 |
| 91 | +we use the `Fluid inline notation <https://docs.typo3.org/permalink/t3coreapi:fluid-inline-notation>`_ |
| 92 | +of the :ref:`If ViewHelper <f:if> <t3viewhelper:typo3fluid-fluid-if>` to only |
| 93 | +output class `active` if the page in the menu is in the root line. |
| 94 | + |
| 95 | +.. _content-element-rendering-sitemap: |
| 96 | + |
| 97 | +Override the sitemap template |
| 98 | +============================= |
| 99 | + |
| 100 | +In a similar fashion we now copy and adjust the template for the sitemap from |
| 101 | +:path:`vendor/typo3/cms-fluid-styled-content/Resources/Private/Templates/MenuSitemap.html` |
| 102 | +into folder :path:`packages/site_package/Resources/Private/ContentElements/Templates/MenuSitemap.html` |
| 103 | +and then adjust it: |
| 104 | + |
| 105 | +.. include:: /CodeSnippets/Fluid/MenuSitemap.rst.txt |
| 106 | + |
| 107 | +We want to adjust the HTML output of the sitemap for different levels. The |
| 108 | +original template however gives us no means to output the level. |
| 109 | + |
| 110 | +Line 5 uses the ViewHelper :ref:`Render ViewHelper <f:render> <t3viewhelper:typo3-fluid-render>` |
| 111 | +to render everything in the section defined in lines 8-23. |
| 112 | + |
| 113 | +In the original template the argument `menu` was already passed on as variable to |
| 114 | +the section. We now enhance this line to also pass on variable `level` set to 1. |
| 115 | + |
| 116 | +In line 17 the section `Menu` recursively includes itself to display further |
| 117 | +levels of the sitemap. We now add 1 to the level so that within the recursive |
| 118 | +call we are in level 2. |
| 119 | + |
| 120 | +In line 14 we use that level to determine which class to use for the page link, |
| 121 | +using once more the `Fluid inline notation <https://docs.typo3.org/permalink/t3coreapi:fluid-inline-notation>`_ |
| 122 | +of the :ref:`If ViewHelper <f:if> <t3viewhelper:typo3fluid-fluid-if>`. |
| 123 | + |
| 124 | +.. _content-element-rendering-image: |
| 125 | + |
| 126 | +Override the partial template for image rendering |
| 127 | +================================================= |
| 128 | + |
| 129 | +The templates of some content elements use the |
| 130 | +:ref:`Render ViewHelper <f:render> <t3viewhelper:typo3-fluid-render>` to include |
| 131 | +a partial template. This is true wherever images are displayed for example. |
| 132 | + |
| 133 | +Partials in turn can include different partials. |
| 134 | + |
| 135 | +The templates for "Image" and "Textpic" both contain the following line: |
| 136 | + |
| 137 | +.. code-block:: html |
| 138 | + :caption: vendor/typo3/cms-fluid-styled-content/Resources/Private/Templates/Image.html (Excerpt) |
| 139 | + |
| 140 | + <f:render partial="Media/Gallery" arguments="{_all}" /> |
| 141 | + |
| 142 | +If you open that partial, it includes yet another partial: |
| 143 | + |
| 144 | +.. code-block:: html |
| 145 | + :caption: vendor/typo3/cms-fluid-styled-content/Resources/Private/Partials/Media/Gallery.html (Excerpt) |
| 146 | + |
| 147 | + <f:render partial="Media/Type" arguments="{file: column.media, dimensions: column.dimensions, data: data, settings: settings}" /> |
| 148 | + |
| 149 | +Which contains another until we finally arrive at |
| 150 | +:path:`vendor/typo3/cms-fluid-styled-content/Resources/Private/Partials/Media/Rendering/Image.html` |
| 151 | +which does contain the actual :ref:`Media ViewHelper <f:media> <t3viewhelper:typo3-fluid-media>`. |
| 152 | + |
| 153 | +By overriding this one partial we can add a class to all images that are |
| 154 | +displayed with the "Image" or "Text with Media" content elements. For example |
| 155 | +we could display all images as circles by adding the class `rounded-circle`: |
| 156 | + |
| 157 | +.. include:: /CodeSnippets/Fluid/Image.rst.txt |
0 commit comments