Last active
May 28, 2020 15:41
-
-
Save cosmomathieu/a9ae774deb9b75403d5ac32fd5808a96 to your computer and use it in GitHub Desktop.
Adding styles directly into the page template from the controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// An example of how to generate inpage css for body | |
// Typically saved in a database OR in theme folder as style.custom.css | |
$encodedStyles = json_encode([ | |
'body' => [ | |
'font-family' => '"NeutraText-Book"', | |
'font-size' => '14px', | |
'background-color' => 'rgba(255, 255, 255, 1)', | |
], | |
'h1' => [ | |
'color' => '#000' | |
], | |
'.container' => [ | |
'max-width' => '1180px', | |
], | |
]); | |
// Data from the database OR custom file | |
$styles = json_decode($encodedStyles); | |
$css = ''; // Declared variable | |
foreach($styles as $selector => $declarations){ | |
$css .= $selector . '{' . PHP_EOL; | |
foreach($declarations as $prop => $value){ | |
$css .= $prop . ':' . $value . ';' . PHP_EOL; | |
} | |
$css .= '}' . PHP_EOL; | |
} | |
$this->output->set_header('Content-Type', 'text/css'); | |
$this->output->set_output($css); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jsfiddle.net/1mqejb87/