Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

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/FirstApp/Block/

paste following code

<?php

namespace Apps\TodoList\Block;

// RecentMembers block must be 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/FirstApp/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. Open browser again, see result:

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

  • No labels