It's possible to define a main color and a secondary color (hex-codes) in my application, those are saved to the db.
The secondary color for example is used for links. I don't want to say Text but instead <a href="#" class=secColor>Text</a> where .secColor has something like
.secColor {
color: $fromDatabase;
}
I'm using Laravel btw.
You can include a .php file as a css by using following code:
index.html:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="style.php">
</head>
<body>
<!-- stuff goes here -->
</body>
</html>
style.php:
<?php
header("Content-type: text/css");
?>
//DB Query
.secColor{
color: <?php echo $fromDatabase;?>
}
You can do this:
Create new route rule:
Route::get('style/generate.css', function ($id) {
// take your color
$data['firstColor']= Colors::where('alias', '=', 'firstColor')->get();
...
return View::make('css.colors', $data)
});
And create new view in resources/views/css/colors:
.firstColor{
color: $colors['firstColor'];
}
And in your main views
I use this method for create js custom file
Related
I have embedded my CSS file as PHP to make it more dynamic as shown in various examples online but I can't seem to get it working
<link rel='stylesheet' href='myphpstylesheet.php'>
My PHP file which I added in the link
<?php
$bgcolor = '#FF00FF';
?>
<style>
Div #container {
Background-color: <?php echo $bgcolor ; ?>
}
</style>
My HTML file
<html>
<head>
<link rel='stylesheet' href='myphpstylesheet.php'>
</head>
<body>
<div id="container ">
I am a div with lorem ipusum
</div>
</body>
</html>
But the color does not apply please help
I did some search on embedding css as php file,
I don't really see the reason for this just use css as intended.
Anyway you can try these methods hope it fixes you problem also take note of herf and hrefas pointed out by Riggs Folly as they are not the same.
METHOD 1
You can edit your httpd.conf or .htaccess with this line
AddType application/x-httpd-php .css
the web server will now parse PHP code that is within the CSS files but i would
avoid messing with those at all costs.
METHOD 2
You can also include a PHP file, in the same manner as you include a CSS file
<link rel="stylesheet" href="style.php" media="screen">
this should be done within the head of the HTML document that is the <head></head> tags
Then your style.php file should look something like this:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "#990000";
$linkColor = "#555555";
$CDNURL = "http://cdn.blahblah.net";
?>
Then your css file should look somthing like this:
#header {
background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
color: <?php echo $linkColor; ?>;
}
ul li a {
color: <?php echo $linkColor; ?>;
}
You should use PHP's include command to inject the external PHP:
<?php include 'myphpstylesheet.php'; ?>
Inside that PHP file, you have a missing PHP closing tag, and the CSS needs to be declared properly within <style> tags:
<?php
$bgcolor = '#FF00FF';
?>
<style>
div #container {
background-color: <?php echo $bgcolor ; ?>
}
</style>
There's many ways to have dynamic control over CSS.
If you wanted to maintain the method, and truly feed it a dynamic PHP file, you have to make sure the PHP output is ONLY css. Here's an article on that.
Good evening,
I started using Codeigniter as Framework for my newest projekt... and already got problems on the first page.
I want to link a CSS file to my site. It looks perfectly good in the Code. But just nothing happens.
<html>
<head>
<title>Tec.Net</title>
<?php
$this->load->helper('html');
echo link_tag($data['css']);
?>
</head>
<body>
<h1><?php echo $title ?></h1>
This is the code of my header.php
body {
background-image: url("application/views/images/Console Background.png");
color: white;
}
The standard.css
<html>
<head>
<title>Tec.Net</title>
<link href="localhost/TecNet/standard.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Home</h1>
<h2>this is a home site</h2> <em>© 2015</em>
</body>
</html>
And at last. The code i receive from Firefox. The Link is perfectly fine. If i try to access it directly from my browser it opens the css file. As inline CSS the code works perfect just that link refuses to work
EDIT:
my URL Structure for the controller.
http://localhost/TecNet/index.php/tecnet/view
And the controller itself
<?php
class Tecnet extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php')) {
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
Try to load your style sheet like this:
<link rel="stylesheet" href="<?= base_url('TecNet/standard.css') ?>">
since CodeIgniter is based on the Model-View-Controller development pattern. Better you put resoures of css, img, js, etc.. in root folder instead on view like you did:
background-image: url("application/views/images/Console Background.png");
assume structures:
/standard.css
/assets/img/console-background.png
your standart.css file:
body {
background-image: url("assets/img/console-background.png");
color: white;
}
easy to call in view:
<link rel="stylesheet" href="<?= base_url('standard.css') ?>">
I use assests a bit differently in CI, load with controller dynamically, but maybe this can work for you:
<link href="<?php echo base_url(); ?>assets/css/your_style.css" rel="stylesheet" />
just replace path, so they will match to yours
I have a problem to add css assets to the collection called from view layout.phtml
This is my code:
<?php echo $this->tag->getDoctype() ?>
<html>
<head>
<?php $this->assets->outputCss('header') ?>
</head>
<body>
<?php $this->assets->get('header')->addCss('test.css'); ?>
<?php $this->assets->get('footer')->addJs('test.js'); ?>
<?php $this->assets->outputJs('footer') ?>
</body>
</html>
And this is output in browser:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="/test.js"></script>
</body>
</html>
Why is not the output of CSS tag in the head?
The Assets component basically works like this:
You add your assets to the service in your controller action (because that's where the application logic is supposed to reside)
In your view, you can then access the Assets component and request the output of single assets or an entire asset group
Suppose the view you mentioned is displayed via this controller:
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
public function index()
{
// Add your header assets here
$this->assets
->collection('header')
->addCss('test.css');
// Add your footer assets here
$this->assets
->collection('footer')
->addJs('test.js');
}
}
Now your view should look like this:
<?php echo $this->tag->getDoctype() ?>
<html>
<head>
<?php $this->assets->outputCss('header') ?>
</head>
<body>
<?php $this->assets->outputJs('footer') ?>
</body>
</html>
I have a css file named test.css and I want to use into it of $var.$var is at test.php. test.css is attached in test.php. My structure is something like this:
//test.php
<html>
<head>
<?php $var = 'anything';?>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
</html>
and this is test.css:
// test.css
.<?php echo $var> { // css property }
Currently test.css does not work. In fact, I want to knoe how can I use of a php variable as a class name into a css file ?
Actually you can.
1st Solution
Instead of using the .css file extension, use .php
Set up variables
<?php
header("Content-type: text/css; charset: UTF-8"); //look carefully to this line
$brandColor = "#990000";
$linkColor = "#555555";
?>
Use variables
#header {
background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
color: <?php echo $linkColor; ?>;
}
...
ul#main-nav li a {
color: <?php echo $linkColor; ?>;
}
2nd and short solution
Create a file and name it like style.php, then in your style.php set your styles in tags like below
style.php
<style>
.blabla{
....
}
#heeeHoo{
...
}
</style>
then include style.php to your file (test.php) like
<html>
<head>
<?php include 'style.php'; ?>
</head>
<body>
</body>
</html>
That is the correct answer. Think like inline css but that is actually in external file
You can use <style> in the PHP file.
//test.php
<html>
<head>
<?php $var = 'anything';?>
</head>
<body>
<style>
<?php echo $var; ?>
</style>
</body>
</html>
The style can also be put in the <head>.
1) database
2) php
3) css
4) html form
The user enters some properties for example background: red in the html form and it saves in the db, then php loades from the db the color option in background css... and then how can I put php variable in the css properties...
also some hostings does not allow to add handlers in .htaccess, is there other options?
Yes, it is possible.
You can serve a css file with php by setting the content-type.
<?php header('content-type: text/css; charset=utf-8'); ?>
You would output the css from the php file.
You could store values in a mySQL or other table with a form, and later retrieve them with the mySQLi or other library.
This is an example of the php script:
<?
header('content-type: text/css; charset=utf-8');
$bgColor = "#FFF"; // Get it from database, I'm setting manually for this example
?>
body{
background-color:<?php echo "$bgColor" ?>;
}
Alternatively, you could output inline css on the page. Either directly in to a tag, or inline on elements.
If really needed a native css file for some reason (I can't think of one), you could technically overwrite a specific css file using php's fwrite() function, though I wouldn't recommend doing it this way.
<?php
$filename = "phpstyle.css";
$fp = fopen($filename, 'w');
$bgcolor = "#FFF";
$css = "body{";
$css .= "background-color:".$bgcolor.";";
$css .= "}";
fwrite($fp, $css);
fclose($fp);
?>
Yes you can serve a css through php script.
One method is to output style elements through php.
<?php
#css on php file
$color = '#FFFFFF';
?>
<html>
<head>
<title>Css through php</title>
<style>
.test{
text-color: <?php echo $color; ?>
}
</style>
</head>
<body>
<p class="test">Your body </p>
</body>
</html>
Another method is to generate css dynamically in a php file and linking it on an html page.
<?php
#css on php file [test_css.php]
header("Content-type: text/css");
$color = '#FFFFFF';
?>
.test{
text-color: <?php echo $color; ?>
}
<html>
<!-- test.html -->
<head>
<title>Css through php</title>
<link rel="stylesheet" type="text/css" href="test_css.php">
</head>
<body>
<p class="test">Your body </p>
</body>
</html>
In addition to Khaleel's answer, you can put CSS in the HTML document itself, in a
<style>
...
</style>
section. You can use PHP variables to fill this in.
Or you can use inline styles in the HTML
<span style="...">...</span>
Why not just:
<?php if (myCondition) { ?>
<style>.myClass {
background: <?php echo $myColor; ?> !important;
} </style>
<?php } ?>
The myCondition triggers the insertion of in body styling. The !important tag overwrites all other existing value of that property.
Simple to understand and easy to use. Works every time.
ether create a css file using php dynamically, such as:
<link href="../styles/styles.php" type="text/css" />
or use php to write the css inline on your html file:
<html>
<head>
<style type="text/css">
body {
background-color: <?php echo $redcolor; ?>;
}
</style>
</head>
<body></body>
</html>
however, you cannot create a standard css file with php.