sunkan / blueprint
3.0.1
2019-06-06 12:07 UTC
Requires
- php: >=7.2.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ~6.0
- sunkan/actus: ~2.0
Suggests
- sunkan/actus: Allows more advancer template finder
README
安装
安装此库的首选方法是通过在项目根目录下运行以下命令使用 Composer
$ composer require sunkan/blueprint
使用
简单示例
无外部依赖
//index.php
$blueprint = new \Blueprint\Simple(new \Blueprint\Helper\ResolverList());
$blueprint->name = 'Blueprint';
//With the simple renderer youo have to add the complete path to template
echo $blueprint->render('tpls/index.php');
以及 tpl 文件
<!--tpls/index.php-->
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Welcome: <?=$name?></h1>
</body>
</html>
带有模板查找器的示例
//index.php
//if multiple directorys are specified they are search in lifo order
$finder = new \Blueprint\DefaultFinder();
$finder->addPath(__DIR__ . '/tpls/');
$template = new \Blueprint\Extended(
$finder,
new \Blueprint\Helper\ResolverList()
);
$template->name = "Sunkan";
//will include tpl from tpls/welcome.php
$response = $template->render('welcome');
//will include tpl from tpls/welcome.test.php
$response = $template->render('welcome', 'test');
echo $response;
以及 tpl 文件
<!--tpls/welcome.php-->
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Welcome: <?=$name?></h1>
</body>
</html>
布局示例 1
//index.php
//Same as previews example
//But you need to add a resolver to find the helper classes
$resolver = new \Blueprint\Helper\Resolver(function($cls) use ($finder) {
return new $cls($finder);
});
$resolver->addNs('Blueprint\DesignHelper');
$blueprint->addResolver($resolver);
以及 tpl 文件
<!--tpls/index.php-->
<?php $view->design()->header()?>
<h1>Welcome: <?=$name?></h1>
<?php $view->design()->footer()?>
<!--tpls/layout/header.php-->
<html>
<head>
<title>Layout type 1</title>
</head>
<body>
<!--tpls/layout/footer.php-->
</body>
</html>
输出
<html>
<head>
<title>Layout type 1</title>
</head>
<body>
<h1>Welcome: Blueprint test</h1>
</body>
</html>
布局示例 2
//index.php
//if multiple directorys are specified they are search in lifo order
$finder = new \Blueprint\DefaultFinder();
$finder->addPath(__DIR__ . '/tpls/');
$resolver = new \Blueprint\Helper\Resolver(function($cls) use ($finder) {
return new $cls($finder);
});
$resolver->addNs('Blueprint\DesignHelper');
$resolverList = new \Blueprint\Helper\ResolverList([$resolver]);
$content = new \Blueprint\Extended($finder, $resolverList);
$content->setTemplate('index.php');
$content->name = "Sunkan";
$blueprint = new \Blueprint\Layout($finder, $resolverList);
$blueprint->setTemplate('layout/main');
$blueprint->setContent($content);
echo $blueprint->render();
以及 tpl 文件
<!--tpls/index.php-->
<?php $view->title('Set title from tpl');?>
<h1>Welcome: <?=$name?></h1>
<!--tpls/layout/main.php-->
<html>
<head>
<title>Layout type 2 - <?=$view->title()?></title>
</head>
<body>
<?=$content?>
</body>
</html>
输出
<html>
<head>
<title>Layout type 2 - Set title from tpl</title>
</head>
<body>
<h1>Welcome: Blueprint test</h1>
</body>
</html>
配置
待办事项