Block Component

Block is a component helps developer separate a complex layout to varieties components in re-usage way.
Each block contains its own logic and template.

Create your first block

Create php class RecentItems under directory ./PF.Site/Apps/TodoList/Block/

paste following code

<?php

namespace Apps\TodoList\Block;

// RecentMembers block must be a child of \Phpfox_Components
//
class RecentItems extends \Phpfox_Component
{

    // this method will be invoked by phpfox
    public function process()
    {

        // assign variables to template
        $this->template()
            ->assign([
                'sHeader' => 'Recent Items', // block title
                'aItems'  => ['Item 1', 'Item 2', 'Item 3'],
            ]);
        return 'block';
    }
}

Create php template file recent_items.html.php under directory ./PF.Site/Apps/TodoList/Block/

<ul>
{foreach from=$aItems item=item}
<li class="category">
    <p>
        {$item}
    </p>
{/foreach}
</ul>

Update start.php, paste following code

$module->addComponentNames('block', [
    'todo.recent_items' => Block\RecentItems::class,
]);

Update Install.php, change setComponent() and setComponentBlock()


    protected function setComponent()
    {
        $this->component = [
            "block"      => [
                "recent_items"    => "",
            ],
            "controller" => [
                "index"   => "todo.index",
            ]
        ];
    }

    protected function setComponentBlock()
    {
        $this->component_block = [
            "Recent Items" => [
                "type_id"      => "0",
                "m_connection" => "todo.index",
                "component"    => "recent_items",
                "location"     => "3",
                "is_active"    => "1",
                "ordering"     => "1"
            ],
        ];
    }

Then update your app to apply your modification. How to update app configurations
Open browser again, see result:

!images/https://docs.phpfox.com/download/attachments/851993/create-your-first-block.png?api=v2!

Next Chapter