zendx/viewautoescape

Zend Framework 1 自动转义

1.5.1 2016-04-28 10:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:28:53 UTC


README

此项目为您提供了一个具有自动转义所有分配视图变量的 ViewRenderer。它还可以防止您在视图脚本中使用对象变量来调用方法。

查看示例和测试以了解外观概念。

Build Status

要求

  • Zend Framework 1.X
  • PHP 5.2.x (PHPUnit 3.6 需要 PHP 5.2.7 或更高版本)
  • PHPUnit >= 3.5 (用于测试和开发)

安装

Composer

...
"require": {
    "zendx/viewautoescape": "~1.5",
}

下载

将源代码复制到您的项目库路径,并将 ZendX 命名空间添加到项目自动加载器。

autoloaderNamespaces[] = "ZendX_"

配置

在 bootstrap.php 中初始化视图

protected function _initView()
{
    $resources = $this->getOption('resources');
    $options = array();
    if (isset($resources['view'])) {
        $options = $resources['view'];
    }
    $view = new ZendX_View_Autoescape($options);

    if (isset($options['doctype'])) {
        $view->doctype()->setDoctype(strtoupper($options['doctype']));
        if (isset($options['charset']) && $view->doctype()->isHtml5()) {
            $view->headMeta()->setCharset($options['charset']);
        }
    }
    if (isset($options['contentType'])) {
        $view->headMeta()->appendHttpEquiv('Content-Type', $options['contentType']);
    }
    
    $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
    $viewRenderer->setView($view);
    Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
    return $view;
}

示例

控制器

public function indexAction()
{
    $this->view->productClass = 'simpleString';
    $this->view->products = array(
        'make<XSS>' => array(
            'name' => '<i>Hacking</i> Session',
            'price' => 672.45
    );
}

视图脚本

<h3><?php echo $this->productClass ?></h3>
<div id="products" class="productList">
    <?php foreach ($this->products as $escapedKey => $product): ?>
        <div id="product<?php echo $escapedKey ?>">
            <?php echo $product->html('name') ?> <strong>[<?php echo $product->html('price') ?> €]</strong>
        </div>
    <?php endforeach; ?>
</div>