lroot/simple-view

一个类文件,实现了视图系统的核心功能,包括布局、部分和占位符

v1.0.0 2016-12-07 23:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:58:23 UTC


README

实现视图系统核心功能的单个类,包括布局、部分、占位符和缓存。

配置

您可以将 SimpleView 配置为与您的目录结构一起使用

// Setup Configuration
SimpleView::setConfigProperties(array(
    SimpleView::CONFIG_VIEW_DIR   => YOUR_APP_PATH.'/app/views/',
    SimpleView::CONFIG_LAYOUT_DIR => YOUR_APP_PATH.'/app/views/_layouts/',
    SimpleView::CONFIG_PARTS_DIR  => YOUR_APP_PATH.'/app/views/_partials/'
));

渲染

渲染视图很简单

// Output your view script. Parameters: view script, data & layout
echo SimpleView::render('your-view-script', array('name'=>'Amanda'), 'your-layout');

您指定视图脚本,传递一个数据关联数组,并指出您想用哪个布局将视图脚本包装起来。您可以在视图脚本中作为标准变量访问此传入数据。例如 array('name'=>'amanda') 将成为 $view_name

视图

视图脚本是主要模板文件。它们包含您正在渲染的视图的核心布局。在布局中,您可以包含部分内容并定义用于封装布局的占位符内容。

<?php SimpleView::placeholderCaptureStart(SimpleView::PLACEHOLDER_HEAD_CONTENT); ?>
<style>
    /* CSS content... */
    /* consider outputting this placeholder content in the head of your layout */
</style>
<?php SimpleView::placeholderCaptureEnd(); ?>

<?php echo SimpleView::partial('title.php',array('name'=>$view_name)); ?>
<p>Example conetent lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

<?php echo SimpleView::placeholderCaptureStart(SimpleView::PLACEHOLDER_INLINE_SCRIPTS);?>
<script>
    // ... mind blowing js code ...
    // consider outputting this placeholder content at the bottom of your layout
</script>
<?php SimpleView::placeholderCaptureEnd(); ?>

占位符

占位符允许您将内容与一个可以在以后引用的名字关联起来。

SimpleView::placeholderSetContent('placeholder_name','placeholder content');

您还可以“捕获”占位符内容。

<?php SimpleView::placeholderCaptureStart('placeholder_name'); ?>
All content will be captured until we call placeholderCaptureEnd()
<?php SimpleView::placeholderCaptureEnd(); ?>

获取占位符内容很简单。

echo SimpleView::placeholderGetContent('placeholder_name');

给事物命名很难,所以有一组常见的占位符名字可供选择。

<?php SimpleView::placeholderCaptureStart(SimpleView::PLACEHOLDER_INLINE_SCRIPTS);?>
<script>
    // ... mind blowing js code ...
    // consider outputting this at the bottom of your layout
</script>
<?php SimpleView::placeholderCaptureEnd(); ?>

部分

部分就像小的、自包含的视图脚本。您也可以向部分模板传递一个数据关联数组。

// Execute a partial script and echo its content
echo SimpleView::partial('title.php',array('name'=>'Amanda'));

部分模板是简单的 PHP 文件。数据数组将作为变量在部分模板中使用。

<!--Super simple partial template-->
<h1>Hello <?=$parts_name?></h1>

布局

布局是您的“主”模板,将所有内容组合在一起。

<!DOCTYPE html>
<html>
<head>
    <?php
        // This content could be set from anywhere including the view script or a
        // partial included by the view script
        echo SimpleView::placeholderGetContent(SimpleView::PLACEHOLDER_HEAD_CONTENT);
    ?>
</head>
<body>
    <?php
        // This placeholder name is special. It will be populated with the contents
        // of your rendered layout.
        echo SimpleView::placeholderGetContent(SimpleView::PLACEHOLDER_TMPL_CONTENT);
    ?>
    <?php
        // This content could be set from anywhere as well
        echo SimpleView::placeholderGetContent(SimpleView::PLACEHOLDER_FOOTER_CONTENT);
    ?>
</body>
</html>

进一步文档

SimpleView 有详尽的代码级文档和示例。要了解 SimpleView 的工作原理,只需阅读代码即可。

待办事项

  • 添加测试
  • 添加带有功能布局、占位符和部分的示例目录
  • 使指定部分和视图脚本的方式相同(为部分修复扩展要求)
  • 展示如何在模板中处理命名空间使用