Solución simple para poder usar twig (motor de plantillas para php) sin usar ningún tipo de framework o patrón de arquitectura de software.

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

This package is not auto-updated.

Last update: 2024-04-27 15:26:03 UTC


README

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

Instalación y configuración

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

Creamos un archivo de configuración:

<?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/'
    )
);

Uso

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 hereda todas las funciones de la clase Twig_Environment, así que tenemos todas las funciones brindadas por esta clase:

/**
* 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);

Licencia

La dependencia View es software de tipo OpenSource, usando la licencia MIT