CappY
CappY

Reputation: 1580

CodeIgniter - pass variables to CSS

I'm rewriting website on Code Igniter, and i need to load external TTF. MySQL db points path to that TTFs. Can I pass somehow these variables to CSS and make foreach loop to 'loads' these fonts.

I tried

$this->load->vars($data);

Upvotes: 1

Views: 2747

Answers (6)

Lekshmi
Lekshmi

Reputation: 45

Here is the answer , I have implemented this and works fine

https://ellislab.com/forums/viewthread/220105/#1014374

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137497

First, deal with serving dynamic CSS. My site has a controller called "resource" which allows me to serve CSS, JS, etc. (maybe images in the future). It loads views based upon the segments passed to it in the url.

So, when http://mysite.com/resource/css/main.css is requested:

  • My Resource controller (.../controllers/resource) handles any specifics of data handling (as is general with an MVC controller). It then loads:
  • A generic view: ".../views/resources/css.php", passing it the name of the desired css file. This view prints out the header, specifying the Content-Type (important!) and any other generic stuff. Then it proceeds to load:
  • The actual CSS file specified, here ".../views/resources/css/main.css.php".

It's a little overkill, but allows for a lot of flexibility, like you sound like you need.

Controller:

...
$segments = $this->uri->segment_array();
array_shift($segments);   // remove the first two
array_shift($segments);
$content['stylesheet'] = $segments[0] . ".php";  //e.g. main.css.php
$content['data'] = array();                      //Font data, etc
$this->load->view('resources/css.php', $content);
..

Generic resources/css.php This loads up the actual .css.php stylesheet

<?php header("Content-Type: text/css"); // This is key! ?>  
/* MySite CSS File (c) 2011 bla bla */
<?php
     $this->load->view("resources/css/$stylesheet", $data);
     echo "\n";
?>

Specific resources/css/main.css.php

<?php echo "/* I can use PHP in my CSS! */\n"; ?>
body { background-color: <?=$data['bg_color']?>; }
p { font-family: <?=$data['p_font_fam'];?>; }

Upvotes: 6

rabidmachine9
rabidmachine9

Reputation: 7985

You probably need to understand how you retrieve data from db and how you display them: http://codeigniter.com/user_guide/database/index.html good luck

EDIT: what you need is probably something like that: after you have retrieved the links from database and let's say you called them $ttf_links

 <?php 
          foreach($ttf_links as $link){
        echo "<link rel='stylesheet' type='text/css' href={$link['row_name']} media='screen' />"
    }
    ?>

and then call the fonts you need in your css

Upvotes: 2

Gelatin
Gelatin

Reputation: 2413

A workaround would be to use CSS in the page itself to load the fonts.

Upvotes: 0

cp3
cp3

Reputation: 2139

The easiest way I see you doing this is with file level CSS and changing values the usual way.

Upvotes: 0

Marnix
Marnix

Reputation: 6547

Passing variables to a CSS doesn't work for as far as I know.

I have read something about CSS templating with PHP, but I can't find the link anymore. Will update this answer as soon as I found the link. But you could look for it yourself as well.

Update

Found it!: http://www.barelyfitz.com/projects/csscolor/

Upvotes: 1

Related Questions