Output
Вернуться к: Getting started
Output
Handle selectors, groups, properties are some basic features of HTML_CSS but getting results in different format should be the final step of your process. We have four/five different functions (depending or render you want to get) : HTML_CSS::toArray(), HTML_CSS::toInline(), HTML_CSS::toFile(), HTML_CSS::toString() and HTML_CSS::display().
To Array
HTML_CSS::toArray() must be considered as a debug/help function. An easy way to explore HTML_CSS data structures.
For example, explore these definitions :
html, body { margin: 2px; padding: 0px; border: 0px; } p, body { margin: 4px; } div#header { text-align: center; color: red; }
With script :
<?php
require_once 'HTML/CSS.php';
$css = new HTML_CSS();
$strcss = '
html, body {
margin: 2px;
padding: 0px;
border: 0px;
}
p, body {
margin: 4px;
}
div#header { text-align: center; color: red; }
';
$css->parseString($strcss);
$arr = $css->toArray();
var_export($arr);
?>
To Inline
HTML_CSS::toInline() allow to integrate html tags with styles.
Dissociate html code and presentation layout (CSS) is more than recommanded
Here is a basic example that show a white 'Hello World' on a black background :
<?php
require_once 'HTML/CSS.php';
$css = new HTML_CSS();
$css->setStyle('body', 'background-color', '#0c0c0c');
$css->setStyle('body', 'color', '#ffffff');
echo '<body style="' . $css->toInline('body') . '">';
echo '<p>Hello World</p>';
echo '</body>';
?>
To File
HTML_CSS::toFile() is probably the most interresting solution. You can create a pure CSS file on the fly.
If file already exists, it will be replace without warning !
This script will load an existing style sheet and replace the body definition :
<?php
require_once 'HTML/CSS.php';
$css = new HTML_CSS();
$css->parseFile('example.css');
$css->setStyle('body', 'background-color', '#0c0c0c');
$css->setStyle('body', 'color', '#ffffff');
$css->toFile('example.css');
?>
To String
HTML_CSS::toString() is analogous to HTML_CSS::toFile() except that result is capture to a PHP variable rather than to a text/css file.
<?php
require_once 'HTML/CSS.php';
$css = new HTML_CSS();
$strcss = '
ul, body {
padding: 1em 2em;
}
';
$css->parseString($strcss);
$css->setGroupStyle(1, 'color', 'red');
$str = $css->toString();
echo $str;
// will display
// ul, body { padding: 1em 2em; color: red; }
?>
Display
HTML_CSS::display() is no more no less than just a
<?php
echo $css->toString();
?>
with cache and charset management for browser output.
See also : HTML_CSS::setCharset() and HTML_CSS::setCache() functions.
Вернуться к: Getting started