Timber\Loader::render PHP Метод

render() публичный Метод

public render ( string $file, array $data = null, array | boolean $expires = false, string $cache_mode = self::CACHE_USE_DEFAULT ) : boolean | string
$file string
$data array
$expires array | boolean (array for options, false for none, integer for # of seconds)
$cache_mode string
Результат boolean | string
    public function render($file, $data = null, $expires = false, $cache_mode = self::CACHE_USE_DEFAULT)
    {
        // Different $expires if user is anonymous or logged in
        if (is_array($expires)) {
            /** @var array $expires */
            if (is_user_logged_in() && isset($expires[1])) {
                $expires = $expires[1];
            } else {
                $expires = $expires[0];
            }
        }
        $key = null;
        $output = false;
        if (false !== $expires) {
            ksort($data);
            $key = md5($file . json_encode($data));
            $output = $this->get_cache($key, self::CACHEGROUP, $cache_mode);
        }
        if (false === $output || null === $output) {
            $twig = $this->get_twig();
            if (strlen($file)) {
                $loader = $this->get_loader();
                $result = $loader->getCacheKey($file);
                do_action('timber_loader_render_file', $result);
            }
            $data = apply_filters('timber_loader_render_data', $data);
            $data = apply_filters('timber/loader/render_data', $data, $file);
            $output = $twig->render($file, $data);
        }
        if (false !== $output && false !== $expires && null !== $key) {
            $this->delete_cache();
            $this->set_cache($key, $output, self::CACHEGROUP, $expires, $cache_mode);
        }
        $output = apply_filters('timber_output', $output);
        return apply_filters('timber/output', $output, $data, $file);
    }

Usage Example

Пример #1
0
 /**
  * Compile function.
  * @api
  * @param array   $filenames
  * @param array   $data
  * @param bool    $expires
  * @param string  $cache_mode
  * @param bool    $via_render
  * @return bool|string
  */
 public static function compile($filenames, $data = array(), $expires = false, $cache_mode = Loader::CACHE_USE_DEFAULT, $via_render = false)
 {
     if (!defined('TIMBER_LOADED')) {
         self::init();
     }
     $caller = LocationManager::get_calling_script_dir(1);
     $loader = new Loader($caller);
     $file = $loader->choose_template($filenames);
     $caller_file = LocationManager::get_calling_script_file(1);
     apply_filters('timber/calling_php_file', $caller_file);
     $output = '';
     if (is_null($data)) {
         $data = array();
     }
     if (strlen($file)) {
         if ($via_render) {
             $file = apply_filters('timber_render_file', $file);
             $data = apply_filters('timber_render_data', $data);
         } else {
             $file = apply_filters('timber_compile_file', $file);
             $data = apply_filters('timber_compile_data', $data);
         }
         $output = $loader->render($file, $data, $expires, $cache_mode);
     }
     do_action('timber_compile_done');
     return $output;
 }