上一篇:
Lysine介绍(四)MVC - Controller
PHP的模版实现分为两种:
- 自定义语法和标签,Smarty为代表
- 直接使用PHP本身作为模版语言
Lysine的模版使用了第二种实现方式
<?php
requie_once '/path/to/lysine/core.php';
$config = array(
'app' => array(
'view' => array(
'view_dir' => __DIR__ .'/app/view',
'file_ext' => 'php', // 默认就是php
),
),
);
Lysine\Config::import($config);
$view = new Lysine\View(cfg('app', 'view'));
假定app/view目录下有,index.php视图,生成视图结果就可以用
<?php
echo $view->fetch('index', array('key' => $val));
只需要传递index即可,目录名和扩展名都会根据配置信息补全
同理,app/view/test/index.php就是
<?php
echo $view->fetch('test/index', array('key' => $val));
fetch()的第一个参数也接受带绝对路径的文件名,只要发现文件名是以"/"开头的,就认为是一个绝对路径文件名,就不会把配置信息里的视图目录路径加上去
<?php
// 显示一个不在view_dir目录下的视图
echo $view->fetch('/other/path/test.php');
这个视图也支持layout机制,即一个视图的内容继承自另外一个视图
app/view/_layout/default.php
<html>
<title>Hello, <?php $this->block('title');>Lysine<?php $this->endblock(); ?></title>
<body>
<?php $this->block('main'); ?>
Hello, Lysine!
<?php $this->endblock(); ?>
<?php $this->block('javascript', 'append'); ?>
<scirpt type="text/javascript" src="/js/mootools.js"></script>
<?php $this->endblock(); ?>
</body>
</html>
app/view/index.php
<?php $this->extend('_layout/default'); ?>
<?php $this->block('main'); ?>
Hello, <?php echo $name; ?>
<?php $this->endblock(); ?>
<?php $this->block('javascript'); ?>
<script type="text/javascript" src="/js/mootools/more.js"></script>
<?php $this->endblock(); ?>
如果用$view->fetch('index', array('name' => 'javaeye')),结果应该是:
<html>
<title>Hello, Lysine</title>
<body>
Hello, javaeye
<scirpt type="text/javascript" src="/js/mootools.js"></script>
<scirpt type="text/javascript" src="/js/mootools/more.js"></script>
</body>
</html>
title block没有被修改过,所以就显示layout title block的内容
main block被修改过,所以显示index main block的内容
javascript block声明为append,所以index javascript block的内容被追加到layout javascript block后面
还是那句话,太阳底下没有新鲜事,这种layout机制很多模版引擎都有实现,包括QeePHP,我也是用习惯了,所以也采用这种机制
如果非要找一个亮点的话,就是这个view类我写得比较精巧,代码加注释一共也才299行,某位高人看过以后扬言“抄袭”,让我很是得意了一把 :-)
下一篇:
Lysine介绍(六)存储服务
分享到:
相关推荐
网络技术-网络基础
Lysine2, 用于restfulweb应用的PHP框架 Lysine不再更新,新框架在 https://github.com/yeaha/owl基于PHP 5.4新特性,命名空间及特性rest式controller,以资源的方式组织web app,通过HTTP标准
键电负性均衡方法的浮动电荷分子力场( ABEEMσπ/ MM),对重组人纤溶酶原 Kringle 1结构域 ( K1Pg)与配体ε-Aminocaproic acid ( EACA),trans-4-( Aminomethyl) cyclohexane-1-carboxylic acid ( AMCHA),L-Lysine( ...
Lys: Lysine; Met: Methonine; MG: Methylguanidine; m-I: myo-Inositol; NAG: N-acetyl glycoprotein signals; OAG: O-acetyl glycoprotein signals; PC: Phosphocholine; Phe: Phenylalanine; Py: Pyruvate; Suc:...
多组学联合,泛素化与蛋白质组学PPI需求对应文献——2016.01-SAHA再来一篇文章.pdf,Quantitative Analysis of ...Proteome and Lysine Acetylome Reveal the Differential Impacts of VPA and SAHA on HL60 Cells
Biodegradable multilayer films of alternating poly- L-lysine(PLL) and deoxyribonucleic acid ( DNA) layers were fabricated onto quartz substrates by the layer-by-layer( LbL) self-assembly meth- od ....
Diethylenetriaminepentaacetic dianhydride was co-polymerized with dodecyl ester and benzyl ester of lysine , and octodecyl ester and benzyl ester of lysine respectively to give two terpolymers ....
pH值对赖氨酸保护作用影响的研究,贺洪,刘慧敏,用紫外吸收研究力竭运动后大鼠血清中赖氨酸(lysine)对丙二醛(MDA)的清除。通过试管反应发现,在适宜的浓度和pH值下,赖氨酸能与M
Motivation: Protein ubiquitination is one of the important post-translational modifications by attaching ubiquitin to specific lysine (K) residues in target proteins, and plays important regulatory ...
2.3.3 Lysine Chemistry – Carbamylation . . . . . . . . . . . . . . . . . . . . . . . . . .20 2.4 Multiplexed Approaches to Proteomics . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 2.5 ...