compile() public static method

@param string $tpl Template body
public static compile ( string $tpl, array $context = [] ) : callback($vars=array(),
$tpl string
$context array Context variables useful to generate efficient code (for array, objects and array) @return callback($vars=array(), $return=TRUE, $block=array())
return callback($vars=array(),
Beispiel #1
0
 /** 
  * @dataProvider tplProvider
  */
 public function testLambda($test_file, $data, $expected)
 {
     $this->init($test_file, $expected);
     $callback = Haanga::compile(file_get_contents($test_file), $data);
     $output = $callback($data);
     $this->assertEquals($output, $expected);
 }
 public function match($uri)
 {
     global $conf;
     global $localUri;
     global $uri;
     global $acceptContentType;
     global $endpoints;
     global $lodspk;
     global $results;
     global $firstResults;
     require_once $conf['home'] . 'classes/MetaDb.php';
     $metaDb = new MetaDb($conf['metadata']['db']['location']);
     $pair = Queries::getMetadata($localUri, $acceptContentType, $metaDb);
     if ($pair == NULL) {
         // Original URI is not in metadata
         if (Queries::uriExist($uri, $endpoints['local'])) {
             $page = Queries::createPage($uri, $localUri, $acceptContentType, $metaDb);
             if ($page == NULL) {
                 HTTPStatus::send500("Can't write sqlite database.");
             }
             HTTPStatus::send303($page, $acceptContentType);
             exit(0);
         } else {
             return false;
         }
     }
     list($res, $page, $format) = $pair;
     $uri = $res;
     $queries = $this->getQueries();
     $e = $endpoints['local'];
     require_once $conf['home'] . 'lib/Haanga/lib/Haanga.php';
     Haanga::configure(array('cache_dir' => $conf['home'] . 'cache/', 'autoescape' => FALSE));
     $vars = compact('uri', 'lodspk', 'models', 'first');
     foreach ($queries as $l => $v) {
         $q = Utils::addPrefixes(file_get_contents($v));
         $fnc = Haanga::compile($q);
         $query = $fnc($vars, TRUE);
         $aux = $e->query($query, Utils::getResultsType($query));
         if ($aux["boolean"] === true) {
             $pair[] = $l;
             return $pair;
         }
     }
     return false;
 }
Beispiel #3
0
 public static function showView($lodspkData, $data, $view)
 {
     global $conf;
     global $uri;
     global $lodspk;
     global $extension;
     //$lodspk = $conf['view']['standard'];
     $lodspk = $lodspkData;
     if (isset($lodspkData['params'])) {
         $lodspk['this']['params'] = $lodspkData['params'];
     }
     if (isset($lodspk['queryTimes'])) {
         $lodspk['queryTimes'] = Convert::array_to_object($lodspk['queryTimes']);
     }
     require_once $conf['home'] . 'lib/Haanga/lib/Haanga.php';
     $viewAux = explode("/", $view);
     $viewFile = array_pop($viewAux);
     //$viewFile = $view;
     $viewPath = join("/", $viewAux);
     Haanga::configure(array('template_dir' => $viewPath, 'cache_dir' => $conf['home'] . 'cache/'));
     $rdf = null;
     if (array_key_exists('rdf', $data)) {
         $rdf = $data['rdf'];
         unset($data['rdf']);
     } else {
     }
     $models = $data;
     Convert::getPaths($models, "");
     $first = $lodspk['firstResults'];
     unset($lodspk['firstResults']);
     //unset($lodspk);
     $vars = compact('uri', 'lodspk', 'conf', 'models', 'rdf', 'first');
     if ($conf['debug']) {
         //Logging::log(var_export($vars, true));
     }
     if (is_string($data)) {
         echo $data;
     } elseif (is_file($view)) {
         try {
             Haanga::Load($viewFile, $vars);
         } catch (Exception $e) {
             echo '<pre>';
             echo $e->getMessage() . "' in " . $e->getFile() . ":" . $e->getLine() . "\nStack trace:\n" . $e->getTraceAsString();
             echo '</pre>';
         }
     } elseif ($view == null) {
         $fnc = Haanga::compile('{{models|safe}}');
         $fnc($vars, TRUE);
     } else {
         echo $conf['home'] . $viewPath . " " . $viewFile;
         $fnc = Haanga::compile($view);
         $fnc($vars, TRUE);
     }
 }
Beispiel #4
0
<?php

require "../lib/Haanga.php";
$fnc = Haanga::compile(<<<EOT
    <h1>{{foobar}}{{    foobar  }}</h1>

    Este template será compilado a una función PHP ({{foo|default:foobar}})


EOT
);
$fnc(array("foobar" => 'hola', 'foo' => '.I.'), FALSE);
$fnc(array("foobar" => 'chau'), FALSE);
Beispiel #5
0
 public function generate($name, $data)
 {
     if (!class_exists("Haanga")) {
         throw new \RuntimeException("The library 'Haanga' is not loaded for UI Generator!");
     }
     if (($default = $this->getDefConf('default_block')) !== '') {
         $this->defaultBlock = $default;
     }
     $this->currentBlock = $this->getBlock($name);
     $this->currentBlockName = $name;
     $this->data = $data;
     $generated = array();
     foreach ($this->currentBlock as $varName => $template) {
         $fn = \Haanga::compile($template);
         try {
             $generatedContent = $fn($data);
             if ($varName == 'html') {
                 $generatedContent = $this->enableMinify ? self::minifyHtml($fn($data)) : $fn($data);
             }
             $generated[$varName] = $generatedContent;
         } catch (\Exception $e) {
             throw new \Exception('Alchemy\\Component\\UI\\Parse:: ' . $e->getMessage());
         }
     }
     return $generated;
 }