core
core copied to clipboard
View class steroids ππ½
Componetization syntax
Current:
<div>
<?php View::render('myComponent', [
'attr' => 'value'
] ?>
</div>
Requestedππ½π₯
<div>
<f-MyComponent attr="value" />
</div>
-
fprefix can be customizable - components folder must be defined first or
/views/componentsis taken by default and map functions or classes
function MyComponent(array $props): string {
return <<<html
<div>{$props['attr']}</div>
html;
}
// or
class MyComponentClass {
function __construct(array $props) {
// initialize properties, call preparation methods or whatever
}
function render(): string {
// return html code
}
}
Component classes can optionally declare methods styles() that returns css in string for that component and it only be applied one time when component be used multiple times
class MyComponent {
function render(): string {
// render html
}
function styles(): string {
return <<<css
body {
color-scheme: light dark;
}
css;
/* or if you want to link and external file, or you want to add attributes to the <style> or <link>
return <<<html
<style media="print">
</style>
html
*/
}
}