The HTML Builder
The html builder class is used to manipulate and configure your html document. It is connected to the router class to have everything in one.
Each setting that is set outside a route is set globally for each route.
Caching
Static Page Caching
If your site is not changing dynamicly you can use the build in site cache from the CML Framework using the cache()
function.
cache(string $clearCurrentQuery, string $clearAllQuery, bool $cacheEnabled = false)
Enables or disables caching and sets the cache options.
$app->cache("delCurrent", "dellAll", true);
setCacheData
Sets cache data with a given name, value, and expiration time.$app->setCacheData("cache_data_name", "cache_data_value", $app::MINUTE_IN_SECONDS);
There are predefined constants for the time specifications that you can set for the storage time.
$app::MINUTE_IN_SECONDS
$app::HOUR_IN_SECONDS
$app::DAY_IN_SECONDS
$app::WEEK_IN_SECONDS
$app::MONTH_IN_SECONDS
$app::YEAR_IN_SECONDS
getCacheData
Retrieves data from the cache based on the given name.$data = $app->getCacheData("cache_data_name");
deleteCacheData
Deletes a specific cache data by name.$deleted = $app->deleteCacheData("cache_data_name");
Set The Page Title
Sets the title of the current Route and overwrite the project title.
$app->addRoute('*', '/', function () use ($app) {
$app->setTitle("Home");
});
Meta
Adds a meta to the current Route.
$app->addRoute('*', '/', function () use ($app) {
$app->addMeta('name="description" content="Backend Documentation"');
});
HTML Filter
The html_filter()
function applies a filter to the specified HTML tag attribute.
It can used as an getter
and setter
.
Examples
Set tag attributes:$app->html_filter('html', function ($attr){
$attr = ['class' => 'dark-mode', 'data-theme' => 'dark'];
return $attr;
});
Get tag attributes:
$attributes = $app->html_filter('html', function ($attr){
return $attr;
});
Filter tag attributes:
$app->html_filter('html', function ($attr){
unset($attr['class']);
return $attr;
});
html
body
lang
title
charset
CDN
The addCDN()
function is used to add a CDN link to the stored resources.
addCDN(string $type, string $attr)
Adds a CDN link with a specified type and attribute information to the list of CDNs.
For example, to add a stylesheet link to a CSS file from a CDN:
$app->addCDN('link', 'rel="stylesheet" href="https://cdn.example.com/style.css"');
Styles And Scripts
addScript()
and addStyle()
Add a stylesheet or script to the current Route from the /web/css
or /web/js
directory>.
$app->addRoute('*', '/', function () use ($app) {
$app->addStyle('styles.css');
});
$app->addRoute('*', '/', function () use ($app) {
$app->addScript('scripts.js');
});
You can easily add attributes to your linked JS and CSS files.
$app->addScript("scripts.js", "data-id='10'");
//or
$app->addScript("scripts.js", ["data-id" => 10]);
Include a file from the root directory like so:
$app->addStyle("node_modules/fontawesome/dist/fontawesome.min.css", "", true);
Inline Styles And Scripts
addInlineStyle()
and addInlineScript()
Adds the possibility to put inline styles and script directly into the header without using hooks.
$app->addInlineStyle("html{color: $black}");
//or
$app->addInlineScript("let url = '${$app->url()}'");
Compress
You can easily compress your own JavaScript and CSS files. The files become a one-liner. The function removes all spaces and comments.
$app->addStyle($app->compress("styles.css"));
//or
$app->addScript($app->compress("scripts.js"));