Пример #1
0
 public function readFile($file)
 {
     if (strpos($file, "://") !== false) {
         $r = new \Clips\Resource($file);
         return $r->contents();
     }
     // Try the absolute path first
     if (file_exists($file)) {
         return file_get_contents($file);
     }
     foreach (\Clips\clips_config('sass_folders', array()) as $folder) {
         $r = \Clips\try_path(\Clips\path_join($folder, $file));
         if ($r) {
             return file_get_contents($r);
         }
     }
     foreach ($this->includePathes as $path) {
         $filepath = $path . '/' . $file;
         if (file_exists($filepath) && is_file($filepath) && is_readable($filepath)) {
             return file_get_contents($filepath);
         }
     }
     return '';
 }
Пример #2
0
 /**
  * Render the template using the mustache engine
  *
  * @args
  * 		template: The template uri for render
  * 		args: The args that used for render
  */
 public function render($template, $args = array())
 {
     if (func_num_args() > 2) {
         // They must using the variable args method
         $args = func_get_args();
         $template = array_shift($args);
         return $this->render($template, $args);
     }
     // Get the cache file name by md5 it
     $hash = md5($template);
     $cacheDir = $this->filecache->cacheDir();
     if (!file_exists($cacheDir)) {
         // Create the directory if not exists
         mkdir($cacheDir, 0755, true);
     }
     $phpname = \Clips\path_join($cacheDir, 'tpl_' . $hash . '.php');
     $debug = \Clips\config('debug_template');
     // Check if we can found the compiled php
     if (!file_exists($phpname) || $debug) {
         // Can't found this template in cache, read it from resource
         $resource = new \Clips\Resource($template);
         $str = $resource->contents();
         $flags = \Clips\context('template_flags');
         if (!$flags) {
             $flags = \Clips\config('template_flags');
             if ($flags) {
                 $flags = $flags[0];
             } else {
                 $flags = LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_HANDLEBARS | LightnCandy::FLAG_HANDLEBARSJS;
             }
         }
         $opts = array('flags' => $flags);
         $partials = \Clips\context('template_partials');
         if ($partials) {
             $opts['partials'] = $partials;
         }
         $default_helpers = array('site_url' => '\\Clips\\Libraries\\site_url', 'static_url' => '\\Clips\\Libraries\\static_url', 'php' => '\\Clips\\Libraries\\php_call');
         $default_block_helpers = array('times' => '\\Clips\\Libraries\\times', 'randtimes' => '\\Clips\\Libraries\\randtimes');
         $helpers = \Clips\context('template_helpers');
         if ($helpers) {
             $opts['helpers'] = array_merge($default_helpers, $helpers);
         } else {
             $opts['helpers'] = $default_helpers;
         }
         $block_helpers = \Clips\context('template_block_helpers');
         if ($block_helpers) {
             $opts['hbhelpers'] = array_merge($default_block_helpers, $block_helpers);
         } else {
             $opts['hbhelpers'] = $default_block_helpers;
         }
         if ($str) {
             $php = "<?php " . PHP_EOL . LightnCandy::compile($str, $opts);
             // Save it to php file
             file_put_contents($phpname, $php);
         }
     }
     if (file_exists($phpname)) {
         $renderer = (include $phpname);
         return $renderer((array) $args);
     }
     return '';
 }