一个简单的解决方案,可以在不使用任何框架或软件架构模式的情况下使用 twig(PHP 的模板引擎)。

v1.0.1 2015-08-22 22:48 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:00:06 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Join the chat at https://gitter.im/shinigamicorei7/view

安装和配置

{
    "require" : {
        "shinigamicorei7/view" : "dev-master"
    }
}

创建配置文件

<?php

return array(
    /**
     * Este campo es obligatorio ya que se usará como directorio root de las plantillas
     *
     * templates_dir string|array
     */
    'templates_dir' => array(
        'path/to/templates/'
    ),
    /**
     * Namespaces que se usaran en la aplicación Ej:
     *
     * echo view('@namespace/index.twig',$data);
     * o
     * echo View::render('@namespace/index.twig',$data);
     *
     * namespaces null|array
     */
    'namespaces' => array(
        'test' => 'path/to/test/views/'
    ),
    /**
     * Opciones diponibles
     *
     * @see \Twig_Environment
     * options array
     */
    'options' => array(
        'debug' => false,
        'cache' => 'path/to/cache/'
    )
);

使用方法

require 'path/to/vendor/autoload.php';

View::setDirConfig('path/to/view/config.php');

/**
* Usando la sobrecarga de clases
*/
echo View::render('index.twig',array('nombre' => 'Shinigamicorei7'));


/**
* Usando la función de ayuda
* echo view('index.twig',array('nombre' => 'Shinigamicorei7'));
*/

/**
* Usando los namespaces definidos en nuestro archivo de configuración
* echo view('@test/index.twig',array('nombre' => 'Shinigamicorei7')); 
*/

ViewManager 继承了 Twig_Environment 类的所有功能,因此我们拥有了该类提供的所有功能

/**
* is_a es una función del core de php, por esa razón la invocamos con una cadena de texto
*/
$is_a = new Twig_SimpleFunction('is_a', 'is_a');

$filter = new Twig_SimpleFilter('array_filter', function ($context, $string) {
    //tu código
}, array('needs_context' => true));

$test = new Twig_SimpleTest('red', function ($value) {
    if (isset($value->color) && $value->color == 'red') {
        return true;
    }
    if (isset($value->paint) && $value->paint == 'red') {
        return true;
    }
    return false;
});

View::addFunction($is_a);

View::addFilter($filter);

View::addTest($test);

许可证

View 依赖项是开源软件,使用的是 MIT 许可证