Exemple #1
0
 /**
  * Class constructor.
  *
  * @param \Phalcon\Mvc\ViewInterface $view
  * @param \Phalcon\DiInterface       $dependencyInjector
  */
 public function __construct($view, $dependencyInjector = null)
 {
     $this->mustache = new \Mustache_Engine();
     $loader = new MustachePartialsLoader($view, $this);
     $this->mustache->setPartialsLoader($loader);
     if ($dependencyInjector !== null) {
         $this->setDi($dependencyInjector);
     }
     $this->mustache->addHelper('uppercase', function ($value) {
         return strtoupper((string) $value);
     });
     parent::__construct($view, $dependencyInjector);
 }
Exemple #2
0
function view($name, $props)
{
    if (isset($_SERVER['HTTP_ACCEPTS']) && $_SERVER['HTTP_ACCEPTS'] == 'application/json') {
        header('Content-Type: application/json');
        foreach ($props as $k => $v) {
            if (preg_match('/json/', $k)) {
                unset($props[$k]);
            } else {
                if (is_object($props[$k]) || is_array($props[$k])) {
                    foreach ($props[$k] as $kk => $vv) {
                        if (preg_match('/json/', $kk)) {
                            unset($props[$k][$kk]);
                        }
                    }
                }
            }
        }
        return json_encode($props);
    }
    $partials = [];
    $iterator = new \DirectoryIterator(__DIR__ . "/../templates");
    foreach ($iterator as $file) {
        if ($file->isFile() && preg_match("/\\.mustache\$/", $file->getFilename())) {
            $partials[substr($file->getFilename(), 0, -9)] = file_get_contents($file->getPath() . "/" . $file->getFilename());
        }
    }
    $base = getenv('BASE');
    if ($base == null) {
        $base = "";
    }
    $props['base'] = $base;
    if (isset($_SESSION['lang'])) {
        $props['strings'] = json_decode(file_get_contents(__DIR__ . '/../lang/' . $_SESSION['lang'] . '.json'));
    } else {
        $props['strings'] = json_decode(file_get_contents(__DIR__ . '/../lang/en.json'));
    }
    $template = file_get_contents(__DIR__ . '/../templates/' . $name . '.mustache');
    $m = new \Mustache_Engine(array('partials' => $partials));
    $m->addHelper('json', function ($v) {
        return json_encode($v);
    });
    $m->addHelper('geojson', function ($v) {
        if ($v != null) {
            return json_encode($v);
        } else {
            return '{"features":[],"type":"FeatureCollection"}';
        }
    });
    $content = $m->render($template, $props);
    return $content;
}
 /**
  * Get a Mustache object.
  *
  * @param   Container  $c  A DI container.
  *
  * @return  \Mustache_Engine
  *
  * @since   2.0
  */
 public function getMustache(Container $c)
 {
     $config = $c->get('config');
     $mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader($config->get('mustache.views', __DIR__ . '/../templates'), array('extension' => $config->get('mustache.ext', '.md')))));
     $mustache->addHelper('number', array('1f' => function ($value) {
         return sprintf('%.1f', $value);
     }));
     return $mustache;
 }
 protected function ensureLoaded()
 {
     if ($this->mustache === null) {
         $dirs = $this->pieCrust->getTemplatesDirs();
         $loaders = array();
         foreach ($dirs as $dir) {
             $loaders[] = new \Mustache_Loader_FilesystemLoader(realpath($dir));
         }
         $loaders[] = new \Mustache_Loader_StringLoader();
         $loader = new \Mustache_Loader_CascadingLoader($loaders);
         $options = array('loader' => $loader, 'partials_loader' => $loader, 'debug' => false, 'cache' => null);
         if ($this->pieCrust->isDebuggingEnabled() or $this->pieCrust->getConfig()->getValue('mustache/debug') === true) {
             $options['debug'] = true;
         }
         if ($this->pieCrust->isCachingEnabled()) {
             $options['cache'] = $this->pieCrust->getCacheDir() . 'mustache_c';
         }
         $this->mustache = new \Mustache_Engine($options);
         $pieCrustExtension = new PieCrustExtension($this->pieCrust);
         foreach ($pieCrustExtension->getFunctions() as $name => $function) {
             $this->mustache->addHelper($name, $function);
         }
     }
 }
 public function testHelpers()
 {
     $foo = array($this, 'getFoo');
     $bar = 'BAR';
     $mustache = new Mustache_Engine(array('helpers' => array('foo' => $foo, 'bar' => $bar)));
     $helpers = $mustache->getHelpers();
     $this->assertTrue($mustache->hasHelper('foo'));
     $this->assertTrue($mustache->hasHelper('bar'));
     $this->assertTrue($helpers->has('foo'));
     $this->assertTrue($helpers->has('bar'));
     $this->assertSame($foo, $mustache->getHelper('foo'));
     $this->assertSame($bar, $mustache->getHelper('bar'));
     $mustache->removeHelper('bar');
     $this->assertFalse($mustache->hasHelper('bar'));
     $mustache->addHelper('bar', $bar);
     $this->assertSame($bar, $mustache->getHelper('bar'));
     $baz = array($this, 'wrapWithUnderscores');
     $this->assertFalse($mustache->hasHelper('baz'));
     $this->assertFalse($helpers->has('baz'));
     $mustache->addHelper('baz', $baz);
     $this->assertTrue($mustache->hasHelper('baz'));
     $this->assertTrue($helpers->has('baz'));
     // ... and a functional test
     $tpl = $mustache->loadTemplate('{{foo}} - {{bar}} - {{#baz}}qux{{/baz}}');
     $this->assertEquals('foo - BAR - __qux__', $tpl->render());
     $this->assertEquals('foo - BAR - __qux__', $tpl->render(array('qux' => "won't mess things up")));
 }
Exemple #6
0
require_once "../vendor/autoload.php";
$template = __DIR__ . "/../templates/index.html";
$inputOrdem = filter_input(INPUT_GET, 'ordem', FILTER_SANITIZE_STRING);
$ordem = ['next' => '?ordem=asc'];
if (strtolower($inputOrdem) == 'asc') {
    $ordem = ['next' => '?ordem=desc', 'icon' => 'fa fa-caret-up'];
}
if (strtolower($inputOrdem) == 'desc') {
    $ordem = ['icon' => 'fa fa-caret-down'];
}
$dadosClientes = (include '../data/clientes.data.php');
if ($inputOrdem != '') {
    $valorOrdem = 1;
    if ($inputOrdem == 'desc') {
        $valorOrdem = -1;
    }
    usort($dadosClientes, function ($a, $b) use($valorOrdem) {
        return $valorOrdem * ($a['id'] <=> $b['id']);
    });
}
$clientes = [];
foreach ($dadosClientes as $cliente) {
    $clientes[] = new Poo\Cliente($cliente);
}
$m = new Mustache_Engine();
$m->addHelper("dateBR", function (DateTime $date) {
    return $date->format('d/m/Y');
});
$tpl = $m->loadTemplate(file_get_contents($template));
echo $tpl->render(['clientes' => $clientes, 'ordem' => $ordem]);