Skip to content

Conversation

@schliesser
Copy link

Issue #1228

Description

The new :html:<f:format.cssVariables> ViewHelper provides an easy way to format array-structured Fluid data as CSS custom properties (variables). This is particularly useful for passing data from fluid variables to CSS, allowing for dynamic and easily maintainable theme styling.

The ViewHelper recursively processes a given Fluid array, converting each key-value pair into a CSS variable. The array keys are transformed into CSS variable names (e.g., camelCaseKey becomes --camelcasekey), and the corresponding values are used as the variable values. Only string values are rendered.

Example

Fluid Template:

<f:variable name="themeData" value="{
    colors: {
        primary: '#ff8700',
        text: '#333333',
        accent: {
            blue: '#5599FF',
            green: '#28a745'
        }
    },
    spacing: {
        small: '8px',
        medium: '16px',
        large: '24px'
    }
}" />

/* Default theme variables */
<f:format.cssVariables variables="{themeData.colors}" />

/* Spacing variables with a prefix */
<f:format.cssVariables variables="{themeData.spacing}" prefix="layout-" />

/* Variables scoped to a specific class */
<f:format.cssVariables variables="{themeData.colors.accent}" selector=".special-content" />

Generated CSS Output:

/* Default theme variables */
:root {
    --primary: #ff8700;
    --text: #333333;
    --accent-blue: #5599FF;
    --accent-green: #28a745;
}

/* Spacing variables with a prefix */
:root {
    --layout-small: 8px;
    --layout-medium: 16px;
    --layout-large: 24px;
}

/* Variables scoped to a specific class */
.special-content {
    --blue: #5599FF;
    --green: #28a745;
}

Impact

This new ViewHelper simplifies the process of creating dynamic and themable components. By allowing developers to pass data directly from Fluid to CSS, it reduces the need for complex workarounds or manual synchronization between backend data and frontend styles. This leads to cleaner, more maintainable code and makes it easier to implement features like user-customizable themes.

@sascha-egerer
Copy link
Contributor

I’m wondering why we should include CSS in the HTML document. I think this viewhelper is more of a personal use case and shouldn’t be part of the fluid itself.

@s2b
Copy link
Contributor

s2b commented Sep 5, 2025

I can see some use cases where this might be helpful, similarly to JSON formatting. I think it doesn't hurt to add this to Fluid, since it's a fairly simple implementation.

But now that I think about it, the ViewHelper should probably do less: It should just create a list of variables from the specified array and return them in one line. This would also allow usage in inline style attributes, just like JSON might be used in data-*.

@schliesser
Copy link
Author

@s2b so I remove the argument "selector" and remove the brackets?

@s2b
Copy link
Contributor

s2b commented Sep 5, 2025

Or you leave it in but don't set a default and remove the brackets if it isn't set. Not sure which one is better.

@s2b s2b added the backport label Sep 15, 2025
@schliesser schliesser force-pushed the feature/1228-format-css-variables branch 2 times, most recently from 9559f1b to e7249be Compare October 20, 2025 15:40
@schliesser schliesser force-pushed the feature/1228-format-css-variables branch from e7249be to 8a0b5cc Compare October 20, 2025 15:42
@schliesser
Copy link
Author

Heyho @s2b
it's been a while. Finally I had time to improve the PR.
I removed the default selector value as you suggested. With an empty selector the css variables are rendered with a space inbetween and no brackets.
If an selector is given, I add curly brackets and linebreaks.

Thanks for review in advance.

*
* {someArray -> f:format.cssVariables()}
*
* ``["array","values"]``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something or does this example not belong here?

* The css variable names are created from the array keys.
* You can add a custom prefix to the CSS variable name.
*
* By default, the variables are defined as `:root`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer correct. Maybe something like this instead?

Optionally, a CSS selector (e. g. :root) can be supplied which will be wrapped around the CSS variables.


public function initializeArguments(): void
{
$this->registerArgument('value', 'mixed', 'Input array which should be provided as CSS variables');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is probably array here?

@s2b
Copy link
Contributor

s2b commented Oct 31, 2025

Thanks! Just some small adjustments, then it can go in. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants