2

I'm working out a process to save actions that occur from jquery in my view in cakephp.. I figure an easy way to load the saved values, such as the width and height for a DIV, would be to have cakephp echo a variable as their width / height in the css file, much the same way it would do this in the view file.. I guess I'm not sure exactly where to look for info on this, if its in the cakephp cookbook I guess I'm missing it as I don't see how to do it in there.. any advice is appreciated.

2
  • do you want to be able to save those changes to disk or just keep them for the duration of the page view? Commented Jul 20, 2010 at 8:59
  • I've already got the saving of the data part figured out.. my jquery calls my cakephp controller and saves the DIV info to a database, so the issue for me is just how I can then set these changes into the css file Commented Jul 21, 2010 at 0:50

2 Answers 2

3

This is actually pretty easy (and powerful), and can be done without the aid of CakePHP.

First, make a new file in your webroot called css.php. At the top of that file put the following:

<?php header("Content-Type: text/css"); ?>

Now, link to this file in the head of your layout, just as you would a normal CSS file.

<link rel="stylesheet" href="/path/css.php" type="text/css" />  

And there you have it, a dynamic CSS file. You can pass information to it like so:

<link rel="stylesheet" href="/path/css.php?c=red&fw=700" type="text/css" />  

CLARIFICATION: To access the variables mentioned above, you would use the $_GET variable in the CSS file. Take a look at the link tag above. To access those variables in the css file, you would do something like this:

.class {color:<?php echo $_GET['c']; ?>;font-weight:<?php echo $_GET['fw']; ?>;}

UPDATE: After viewing the link you posted about the CakePHP HTML Helper, I realized that there is a better way to do this if you intend to pass a lot of variables to the css file.

Create a new model and controller called DynamicStyle and DynamicStylesController (or something similar). Then, make a new layout file called css.ctp that all of this controller's views will use. Declare the content-type header statement in that layout file.

The last step would be to link to a method in that controller from the head of your standard layout header.

Now you could make a database table of css rules and use those with the HTML helper in the css view.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm a bit confused on this.. so how would I then pass a variable to the css file? Would I have to declare it in the css.php file first?
2

I just realized CakePHP has something for this as well:

http://book.cakephp.org/view/1440/style

So this may come in handy for anyone who comes across this in the future

1 Comment

This link is not available anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.