Timber\Timber::compile PHP Method

compile() public static method

Compile function.
public static compile ( array $filenames, array $data = [], boolean | integer $expires = false, string $cache_mode = Loader::CACHE_USE_DEFAULT, boolean $via_render = false ) : boolean | string
$filenames array
$data array
$expires boolean | integer
$cache_mode string
$via_render boolean
return boolean | 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;
    }

Usage Example

Esempio n. 1
0
 /**
  * Logic for rendering this shortcode
  *
  * Note that we return the output of this shortcode. This is a requirement
  * by WordPress based on how do_shortcode works.
  * @return string
  *  Compiled shortcode
  */
 public function render($atts = array(), $content = null)
 {
     $template = $this->shortcode . '.twig';
     if ($this->template !== null) {
         $template = $this->template;
     }
     $template = \apply_filters('shortcode_template', $template, $this->shortcode);
     if (class_exists('\\Timber\\Timber')) {
         return Timber::compile($template, $this->data);
     } else {
         include_once $template;
     }
 }