flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.63k stars 409 forks source link

View class steroids 🙌🏽 #580

Open fadrian06 opened 5 months ago

fadrian06 commented 5 months ago

Componetization syntax

Current:

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

Requested🙌🏽🔥

<div>
  <f-MyComponent attr="value" />
</div>
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 commented 5 months ago

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