Add CSS and JavaScript to a Fluid Template
Tested in: TYPO3v13 Categories: Beginner Templating Frontend
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.
This guide will teach you how to link custom CSS and JavaScript files to your site package using Fluid's built-in Asset ViewHelpers.
Learning objective
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.
Prerequisites
Tools and technology
- A local TYPO3 installation (v11 or newer).
- A site package extension created for your theme.
- Access to the file system of your TYPO3 installation.
- A code editor.
Knowledge and skills
- Basic understanding of the TYPO3 backend.
- Familiarity with the structure of a site package.
- Basic knowledge of CSS, JavaScript, and HTML.
Create Your Asset Files
First, you need to create the CSS and JavaScript files within your site package's directory structure.
- Navigate to your site package's
Resources/Public/directory. - Create two new sub-directories:
CssandJavaScript. -
Inside the
Cssdirectory, create a new file namedcustom-styles.css. Add a simple CSS rule for testing:body { background-color: #f0f0f0; border: 5px solid #ff8700; }Copied! -
Inside the
JavaScriptdirectory, create a new file namedcustom-scripts.js. Add a simple JavaScript alert for testing:alert('Hello from custom-scripts.js!');Copied!Your file structure should now look like this:
└── your_sitepackage/ └── Resources/ └── Public/ ├── Css/ │ └── custom-styles.css ├── JavaScript/ │ └── custom-scripts.js └── ...Copied!
Include Assets in Your Fluid Template
Now, you will link these new files in the main Fluid layout file of your site package.
- Open the main HTML template file for your site's layout. This is commonly located at
your_sitepackage/Resources/Private/Layouts/Page/Default.html. -
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.Update the code to include the new assets as shown below. The
identifieris a unique name, andhrefpoints to the file location using theEXT:syntax.<head> ... <f:asset.css identifier="my-custom-styles" href="EXT:your_sitepackage/Resources/Public/Css/custom-styles.css" /> <f:asset.js identifier="my-custom-scripts" href="EXT:your_sitepackage/Resources/Public/JavaScript/custom-scripts.js" /> ... </head>Copied!Make sure to replace
your_sitepackagewith the actual key of your site package extension.
Clear the Cache and Verify
Finally, clear the TYPO3 caches to ensure your changes are loaded, and then check the frontend of your website.
- Log in to the TYPO3 backend.
- Click the "Clear all caches" lightning bolt icon in the top toolbar.
-
Open your website in a new browser tab.
- You should see the styles from
custom-styles.cssapplied to the page (a light gray background and an orange border). - You should see a JavaScript alert box appear with the message "Hello from custom-scripts.js!".
- You should see the styles from
Summary
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.
Next steps
Now that you know how to include assets in Fluid, you might like to:
- Add Global CSS and JavaScript with TypoScript
- Explore using other ViewHelpers in your templates.