Add new CSS style from an external file and different class - php

I have a page called index.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
...
<body>
<div class="my_class"></div>
...
</body>
</html>
In mystyle.css there is no declaration for .my_class
I have another file called external_style.css where there is no declaration for .my_class either but there is a class called .new_class
Can not import or append external_style.css - cause it will overwrite other elements.
My question is:
How can I get the properties of .new_class from external_style.css into .my_class without importing the file.
Is there a way to read that file using php?
Is there another better solution?

If .my_class doesn't exist within mystyles.css can you not just take your code from .new_class within external_style.css and create a .my_class in mystyles.css? or have i missed the point?

You may want to look into using a PHP file with a CSS header. This article should get you started: http://sperling.com/examples/pcss/

There are a couple things you can do to achieve what you are trying to accomplish
Move custom layout data to database: Rather than creating custom files for each user store the users custom css information in a database record for the user.
You could easily identify the user and use javascript to update the CSS.
More scalabe style sheets: If importing the the external_style.css is affecting the global layout you should revisit the way you are identifying elements on the page so that does not happen. Then you could simply import the users custom style sheet.
Try using IDs in addition to Classes as well as wrapping sections of content in Divs. Use those identifiers to make sure importing external_style.css does not overwrite everything.

Related

How to use PHP and MySQL to style a website dynamically?

I am looking for a way to use a database table to style parts of a website. I want this because I have multiple websites and I want an easy way to style default parts easily.
Example:
I have a tbl_settings and it contains the columns "Setting_Id, Site_Id, Label, Key, Value".
Key can be "footer_bg" and the value will be "ff3300" for example.
How can I use php to send the value to css/sass?
Maybe create a url that doesnt really exists and echo css into it? Just like with ajax.
Any ideas, I really have no idea...?
Well, i do not think this design is going to work well. Whatever way you are going to code this, you will end up with massive query load on the database. Imagine firing up queries for every class you put into your html, for every user of your app.
I will go for a more "gentle" way of dynamic styling, create some "template" css, store their path into the db and load them into your page dynamically.
The thing is you can link a php file as css.but there is some problems with updating values and between stored values but you can workaround this.
you can link a php file as css like this.
<link rel="stylesheet" href="css/dynamic.php" media="screen">
Than you need to set this file's header.
css/dynamic.php:
<?php
/** set header **/
header("Content-type: text/css");
?>
After that you can pretty much can echo any css rule you want. And you can do your queries in that file.
But this cant solve browser history problem as long as the file name is same if there is a copy in browser history it will be the one used for the site. for fix that you can give your rule sets some unique random identifier. when you change any rule below that identifier you can update that identifier with a new one. how you handel that is in your hands. no metter how you set that you can use this identifier in you html link and pass that as a parameter.
<!-- I assume that identifier stored in '$id' -->
<link rel="stylesheet" href="css/dynamic.php?somekey=<?=$id?>" media="screen">
than you can access this updated value in your file
<?php
/** set header **/
header("Content-type: text/css");
/*Dont forget to escape this before passing it in to sql*/
$id = $_GET["somekey"];
?>
if you handel id's of value sets of your css you can handel browser hisory as well.
I be happy to corrected if I am wrong.

Define php variables that contain both HTML and subsequently, dynamically settable variables for use later in script?

I'm wondering if I want to do may not be possible... My PHP code loops through jpgs and flv files in an image directory and generates content that consists of in some instances HTML, in others CSS combined with dynamically determined values, for example:
'ul.set li.'.$className.
'{background: url(imagessmall/'.$fileName.')
left -2px no-repeat;}'
Since the above (and much longer sections of HTML+variables) occurs more than once in the code, and because it would make the code easier to review and maintain, I'd like to be able to separate out these html + $variable by defining a set of variables up front, then reference them as needed, for example:
$SDImagePreview =
'ul.set li.'.$className.
'{background: url(imagessmall/'.$fileName.')
left -2px no-repeat;}'
.
.
.
//Code that dynamically sets $className and $fileName is here
$write = fwrite($fileCSS, $SDImagePreview);
I've read on stackoverflow and elsewhere about using &, as in &$fieldName to pass values by reference, but haven't found examples of defining a variable that has within it a variable whose value can be set dynamically.
Am I trying to do something that is just not doable? Or are there alternate suggestions re: implementing the general approach I'm describing? Thanks for any suggestions.
Rather than embedding the html in the code itself, I'd like to be able to define a set of variables at the start of the code, then reference them as needed,
Yes its possible you just have to put your CSS directly to your code in the just add :
<style type="text/css" media="screen">
(...) you generate CSS
<?php print $SDImagePreview; ?>
</style>
CSS are most faster to load if is an extarnal .css file for the caching but in your case the CSS need to be generated in PHP so no cache can be done. If you want to have this change the CSS rule already define by other .css file juste put it after.

Yii including CSS and JS files

I've been reading on this quite some time...and i'm puzzled -
Can you help on what is the difference between:
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/some-file.css');
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/some-file.css
Is it a performance issue, or just different syntax?
Thanks,
Danny
registerCssFile always registers the file between the <head> tags, even if you call it somewhere in a view.
This is helpful if you care about HTML validation (a <link> in <body> is invalid), but still want to include a CSS file in a view.
registerCssFile actually aids performance, because the CSS is registered only when you want it (and need it).
The way you are using it, it is identical. To verify this, check the source of the page (in your browser) and check the statement that Yii::app()->clientScript->registerCssFile creates.
However, clientScript lets you control the position of the script in the HTML file. Check out: http://www.yiiframework.com/doc/api/1.1/CClientScript#registerScriptFile-detail and look for POS_HEAD, POS_BEGIN, POS_END.
What is probably more important is this: In the MVC philosophy, you want to have everything related to HTML-output in your view-file. Yii::app()->clientScript lets you add CSS and JS files from within your view files. And that is where you want it.

Using headScript view helper for <head> and <body> scripts

I've got some scripts I'd like to add to the end of the <body> of the page, and some that I need to have in the <head>. I'm wondering if there's a more elegant way to add certain scripts to the <head> and certain in the <body> using a segment or something like that. Say I have two scripts that are going to go in the body:
$this->view->headScript()->prependFile($assetUrl . "/js/jquery.min.js");
$this->view->headScript()->appendFile($assetUrl . "/js/application.js");
And I want this one in the <head> instead:
$this->view->headScript()->prependFile($assetUrl . "/js/modernizr.min.js");
Calling $this->headScript(); outputs all three in both cases. Is there a way to group scripts? I could just paste the HTML snippet manually, but I'd like to have it in code because I switch to minified versions of the javascript if the site is running in the production environment.
I'd make my own helper called htmlScript. You should be able to extend the existing headScript helper, overriding the registry key property only.
Then just echo out your helper in your layout at the end of the document
<?php echo $this->htmlScript() ?>
Edit Been out of the loop for too long ;)
There's already a helper for you - Zend_View_Helper_InlineScript
If you want to override the script files:
$this->view->headScript()->setFile()
EDIT I'm not sure why I got downvoted. I gave an alternative answer to your question, albeit succinctly. If you have prepended/appended two script files, but for a specific controller or module you wish to override the loading of those scripts with a third, then setFile should do exactly what you asked.

Cakephp, dynamically write variables into css file upon load of view?

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.
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.
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

Categories