Skip to content

Instantly share code, notes, and snippets.

@cosmomathieu
Last active May 28, 2020 15:41
Show Gist options
  • Save cosmomathieu/a9ae774deb9b75403d5ac32fd5808a96 to your computer and use it in GitHub Desktop.
Save cosmomathieu/a9ae774deb9b75403d5ac32fd5808a96 to your computer and use it in GitHub Desktop.
Adding styles directly into the page template from the controller
<?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);
@cosmomathieu
Copy link
Author

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