|
| 1 | +# Add CSS and JavaScript to a Fluid Template |
| 2 | +<!-- #TYPO3v13 #Beginner #Templating #Frontend --> |
| 3 | + |
| 4 | +Adding assets like CSS and JavaScript directly into your Fluid templates is a straightforward way to style and add interactivity to specific parts of your website. This method is ideal when your styles or scripts are tightly coupled with a particular page layout or template. |
| 5 | + |
| 6 | +This guide will teach you how to link custom CSS and JavaScript files to your site package using Fluid's built-in Asset ViewHelpers. |
| 7 | + |
| 8 | +## Learning objective |
| 9 | + |
| 10 | +In this step-by-step guide, you will learn how to create custom CSS and JavaScript files and include them in your site's Fluid templates using the `f:asset.css` and `f:asset.js` ViewHelpers. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +### Tools and technology |
| 15 | + |
| 16 | +* A local TYPO3 installation (v11 or newer). |
| 17 | +* A site package extension created for your theme. |
| 18 | +* Access to the file system of your TYPO3 installation. |
| 19 | +* A code editor. |
| 20 | + |
| 21 | +### Knowledge and skills |
| 22 | + |
| 23 | +* Basic understanding of the TYPO3 backend. |
| 24 | +* Familiarity with the structure of a site package. |
| 25 | +* Basic knowledge of CSS, JavaScript, and HTML. |
| 26 | + |
| 27 | +## Create Your Asset Files |
| 28 | + |
| 29 | +First, you need to create the CSS and JavaScript files within your site package's directory structure. |
| 30 | + |
| 31 | +1. Navigate to your site package's `Resources/Public/` directory. |
| 32 | +2. Create two new sub-directories: `Css` and `JavaScript`. |
| 33 | +3. Inside the `Css` directory, create a new file named `custom-styles.css`. Add a simple CSS rule for testing: |
| 34 | + |
| 35 | + ```css |
| 36 | + body { |
| 37 | + background-color: #f0f0f0; |
| 38 | + border: 5px solid #ff8700; |
| 39 | + } |
| 40 | + ``` |
| 41 | +4. Inside the `JavaScript` directory, create a new file named `custom-scripts.js`. Add a simple JavaScript alert for testing: |
| 42 | + ```javascript |
| 43 | + alert('Hello from custom-scripts.js!'); |
| 44 | + ``` |
| 45 | + |
| 46 | + Your file structure should now look like this: |
| 47 | + |
| 48 | + ```text |
| 49 | + └── your_sitepackage/ |
| 50 | + └── Resources/ |
| 51 | + └── Public/ |
| 52 | + ├── Css/ |
| 53 | + │ └── custom-styles.css |
| 54 | + ├── JavaScript/ |
| 55 | + │ └── custom-scripts.js |
| 56 | + └── ... |
| 57 | + ``` |
| 58 | + |
| 59 | +## Include Assets in Your Fluid Template |
| 60 | + |
| 61 | +Now, you will link these new files in the main Fluid layout file of your site package. |
| 62 | + |
| 63 | +1. Open the main HTML template file for your site's layout. This is commonly located at `your_sitepackage/Resources/Private/Layouts/Page/Default.html`. |
| 64 | +2. Add the `<f:asset.css>` and `<f:asset.js>` ViewHelpers inside the `<head>` section of the file. These ViewHelpers tell TYPO3 to add the respective files to the page. |
| 65 | + |
| 66 | + Update the code to include the new assets as shown below. The `identifier` is a unique name, and `href` points to the file location using the `EXT:` syntax. |
| 67 | + |
| 68 | + ```html |
| 69 | + <head> |
| 70 | + ... |
| 71 | + <f:asset.css identifier="my-custom-styles" href="EXT:your_sitepackage/Resources/Public/Css/custom-styles.css" /> |
| 72 | + <f:asset.js identifier="my-custom-scripts" href="EXT:your_sitepackage/Resources/Public/JavaScript/custom-scripts.js" /> |
| 73 | + ... |
| 74 | + </head> |
| 75 | + ``` |
| 76 | + Make sure to replace `your_sitepackage` with the actual key of your site package extension. |
| 77 | + |
| 78 | +## Clear the Cache and Verify |
| 79 | + |
| 80 | +Finally, clear the TYPO3 caches to ensure your changes are loaded, and then check the frontend of your website. |
| 81 | + |
| 82 | +1. Log in to the TYPO3 backend. |
| 83 | +2. Click the "Clear all caches" lightning bolt icon in the top toolbar. |
| 84 | +3. Open your website in a new browser tab. |
| 85 | + * You should see the styles from `custom-styles.css` applied to the page (a light gray background and an orange border). |
| 86 | + * You should see a JavaScript alert box appear with the message "Hello from custom-scripts.js!". |
| 87 | + |
| 88 | +## Summary |
| 89 | + |
| 90 | +Congratulations! You have successfully added custom CSS and JavaScript files to your TYPO3 site using Fluid Asset ViewHelpers. You can now easily manage assets that are specific to your templates. |
| 91 | + |
| 92 | +## Next steps |
| 93 | + |
| 94 | +Now that you know how to include assets in Fluid, you might like to: |
| 95 | + |
| 96 | +* [Add Global CSS and JavaScript with TypoScript](AddGlobalCssAndJavaScriptWithTypoScript.md) |
| 97 | +* Explore using other ViewHelpers in your templates. |
| 98 | + |
| 99 | +## Resources |
| 100 | + |
| 101 | +* [f:asset.css ViewHelper Reference](https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-asset-css) |
| 102 | +* [f:asset.js ViewHelper Reference](https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-asset-script) |
0 commit comments