core icon indicating copy to clipboard operation
core copied to clipboard

View class steroids πŸ™ŒπŸ½

Open fadrian06 opened this issue 1 year ago β€’ 1 comments

Componetization syntax

Current:

<div>
  <?php View::render('myComponent', [
    'attr' => 'value'
  ] ?>
</div>

RequestedπŸ™ŒπŸ½πŸ”₯

<div>
  <f-MyComponent attr="value" />
</div>
  • f prefix can be customizable
  • components folder must be defined first or /views/components is 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
  }
}

fadrian06 avatar Apr 24 '24 07:04 fadrian06

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
    */
  }
}

Add the same as styles but for JS scripts

fadrian06 avatar Apr 24 '24 08:04 fadrian06