PHP code example of migears / pages
1. Go to this page and download the library: Download migears/pages 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/ */
migears / pages example snippets
use MiGears\Pages\Compiler;
use MiGears\Pages\Html as h5;
use MiGears\Template\Template;
$compiler = new Compiler();
$tpl = $compiler->compile([
'title' => '用户管理',
'layout' => 'layout/main',
'sections' => [
'content' => [
h5::HEADING(2)->text('用户列表'),
h5::TABLE('users')->columns([
h5::COL('ID')->pop('{{ row.id }}'),
h5::COL('姓名')->pop('{{ row.name }}'),
])->empty('暂无数据'),
],
],
]);
file_put_contents('views/pages/users.tpl.php', $tpl);
$view = new Template(__DIR__ . '/views');
echo $view->render('pages/users', [
'users' => [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
],
]);
use MiGears\Pages\Renderer;
$renderer = new Renderer(new Template(__DIR__ . '/views'), new Compiler(), __DIR__ . '/cache/pages');
echo $renderer->render($page, ['users' => [...]]);
use MiGears\Pages\Html as h5;
$page = [
'layout' => 'layout/admin',
'sections' => ['content' => [
h5::HEADING(2)->text('用户列表')->id('usersTitle'),
h5::IF('users')->THEN([
h5::TABLE('users')->columns([
h5::COL('姓名')->pop('{{ row.name }}'),
h5::COL('操作')->content([
h5::LINK('/users/{{ row.id }}/edit')->text('编辑'),
]),
])->empty('暂无数据'),
])->ELSE([
h5::TEXT('还没有用户'),
]),
]],
];
<div class="my-card">
<h3><?= $this->e($title ?? '')
<!-- views/components/my-card.tpl.php -->
<div class="my-card">
<h3>## $title ?? '' ##</h3>
<div>### $body ?? '' ###</div>
</div>
use MiGears\Pages\Html as h5;
$tpl = new Template(__DIR__ . '/views');
$tpl->addPath(__DIR__ . '/views/components');
$renderer = new Renderer($tpl, new Compiler(), __DIR__ . '/cache/pages');
echo $renderer->render([
'body' => [
h5::COMPONENT('my-card')->data([
'title' => '{{ user.name }}',
'body' => 'body 由组件决定是否转义',
]),
],
], ['user' => ['name' => 'Alice']]);
use MiGears\Pages\Compiler;
use MiGears\Pages\Html as h5;
use MiGears\Template\Template;
$compiler = new Compiler();
$tpl = $compiler->compile([
'title' => '用户管理',
'layout' => 'layout/main',
'sections' => [
'content' => [
h5::HEADING(2)->text('用户列表'),
h5::TABLE('users')->columns([
h5::COL('ID')->pop('{{ row.id }}'),
h5::COL('姓名')->pop('{{ row.name }}'),
])->empty('暂无数据'),
],
],
]);
file_put_contents('views/pages/users.tpl.php', $tpl);
$view = new Template(__DIR__ . '/views');
echo $view->render('pages/users', [
'users' => [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
],
]);
use MiGears\Pages\Renderer;
$renderer = new Renderer(new Template(__DIR__ . '/views'), new Compiler(), __DIR__ . '/cache/pages');
echo $renderer->render($page, ['users' => [...]]);
use MiGears\Pages\Html as h5;
$page = [
'layout' => 'layout/admin',
'sections' => ['content' => [
h5::HEADING(2)->text('用户列表')->id('usersTitle'),
h5::IF('users')->THEN([
h5::TABLE('users')->columns([
h5::COL('姓名')->pop('{{ row.name }}'),
h5::COL('操作')->content([
h5::LINK('/users/{{ row.id }}/edit')->text('编辑'),
]),
])->empty('暂无数据'),
])->ELSE([
h5::TEXT('还没有用户'),
]),
]],
];
<div class="my-card">
<h3><?= $this->e($title ?? '')
<!-- views/components/my-card.tpl.php -->
<div class="my-card">
<h3>## $title ?? '' ##</h3>
<div>### $body ?? '' ###</div>
</div>
use MiGears\Pages\Html as h5;
$tpl = new Template(__DIR__ . '/views');
$tpl->addPath(__DIR__ . '/views/components');
$renderer = new Renderer($tpl, new Compiler(), __DIR__ . '/cache/pages');
echo $renderer->render([
'body' => [
h5::COMPONENT('my-card')->data([
'title' => '{{ user.name }}',
'body' => 'body 由组件决定是否转义',
]),
],
], ['user' => ['name' => 'Alice']]);