PHP code example of jesspinkman / pure

1. Go to this page and download the library: Download jesspinkman/pure library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jesspinkman / pure example snippets



use Pure\Pure;

echo Pure::h3()
    ->id('my-first-pure-component')
    ->class('badge badge-primary')
    ->my_custom_prop('pure')
    ->___('Hello World!');



echo Pure::div()
    ->class('container')
    ->___(
        Pure::p()
            ->class('inner')
            ->___("I'm nested !")
    );



use Pure\Component;
use Pure\Pure;

//Create a select component, to spped up its creation
//Some magic to add the name attribute, as well as class for children

class Select extends Component {

    function __construct( string $name, ...$children )
    {
        parent::__construct( 'select' );    //create component with 'select' markup
        $this
            ->class('form__select')         //add class attribute = 'form__select'
            ->name( $name )                 //assign attribute name with passed $name in constructor
            ->___($children);            //append children
    }
}

class Option extends Component
{

    public function __construct( string $label, $value = null, bool $disabled = false )
    {
        parent::__construct('option');      //create component with 'option' markup
        $this
            ->class('form__option')         //add class attribute = 'form__option'
            ->value($value ?? false)        //add value attribute if it is defined, otherwise no value attribute
            ->disabled($disabled)           //add disabled attribute if necessary
            ->___($label);               //append label inside in inner html
    }
}

echo Pure::form()
    ->id('select-form')
    ->___(
        new Select('user-preference', [
            new Option( 'Select your preference', null, true ),
            new Option( 'A is the best', 'a' ),
            new Option( 'B is better', 'b' ),
            new Option( 'C is amazing', 'c' )
        ]);
    );



//default page view, already  Component
{
    public function __construct(...$inner_content)
    {
        parent::__construct('html');
        $this->___(
            new PageHead(),
            Pure::body(
                new HeaderView(),
                Pure::main($inner_content),
                new FooterView()
            )
        );
    }

    // Overwrite __toString in order to     //overwrite ___ method in order to abstract a grid specific structure, each child is a div with a specific class
    public function ___(...$children): self
    {
        foreach ($children as $child) {
            parent::___(
                Pure::div()
                    ->class('grid_child')
                    ->___($child)
            );
        }
    }
}

//reusable component to render a single product tile html
class ProductView extends Component
{

    public function __construct(array $product)
    {
        parent::__construct('article');             // create component with article markup
        $this
            ->class('product__tile')                //add class
            ->data_product_id($product['id'])       //add custom attribute
            ->___(
                Pure::h1()                          //first child, the title
                    ->class('product__tile_title')
                    ->___($product['name']),
                Pure::img()                         //second child, the image
                    ->class('product__tile_img')
                    ->src($product['imgURL'])
                    ->alt($product['name']),
                Pure::span()                        //third child, the price
                    ->class('product__tile_price tag')
                    ->class($product['promotion'] ? 'product__tile_price--promotion' : 'product__tile_price--no-promotion') //conditionally load class
                    ->___("USD $product->price"),
            );
    }
}



//to use inside your view controller
$products = Product::getProductList();
$product_views = array_map(fn ($single) => new ProductView($single), $products);

echo new DefaultPageView(new GridView($product_views));