如何在主题文件夹外加载Twig模板?

时间:2014-10-22 22:06:28

标签: php templates twig

我只是学习Twig模板,我无法弄清楚如何加载当前主题目录之外的目录中的模板部分,以便其他主题可以重用它们。

我首先只是简单地加载位于core / tpls /目录中的head.html模板部分。使用我尝试的variouse方法,我得到以下错误:

  

PHP致命错误:带有消息的未捕获异常'Twig_Error_Loader'   '无法找到模板“/home/ubuntu/workspace/core/tpls/head.html”   (查看:/ home / ubuntu / workspace / themes / core)在“base.html”中   第5行。

文件位于目录中,路径正确,尽管日志错误另有说明。

位于themes / core /目录中的我的base.html包含自定义Twig函数,以包含位于主题目录之外的head.html模板:

<html lang="en" class="no-js">
<head>

    {% include core.head %} // This is line 5 and throws the error and need to work correctly
    // {% include core_head %} // This throws the error and should function the same as {% include core_head %}
    // {% include tpl.head %} <-- If I use this, my function for grabbing templates from the theme works since I have a copy of the head.html in the theme dir too //

</head>
<body>

<header id="header">

    {% include tpl.header %} // This works via the get_tpl() function

</header>

    {% include tpl.breadcrumbs %} // This works too

[. . .]

我讨厌整个班级混乱,但有人看到我错了可能会有所帮助。 get_core_tpl()函数是我无法工作的地方,我的其他get _ *()函数可以正常工作。在那里查看我的评论,并注意我在befor_rendor()函数中的注释:

<?php

class Get_TPLs {

    private $tpl_name,
            $theme = 'core',
            $tpl_array = array(
                'breadcrumbs',
                'content',
                'cover',
                'footer',
                'head',
                'header',
                'sidebar'
    );

    public function before_load_content(&$file) {
        $this->tpl_name = basename($file, '.md');
    }

    public function config_loaded(&$settings) {

        if (isset($settings['core_tpl']))
            $this->core_tpl = $settings['core_tpl'];

        if (isset($settings['tpl_array']))
            $this->tpl_array = $settings['tpl_array'];

        if (isset($settings['theme']))
            $this->theme = THEMES_DIR . $settings['theme'];
    }

    public function file_meta(&$meta) {

        $config = $meta;
        if (isset($config['slug'])):
            $this->tpl_name = strtolower($config['slug']);
        endif;
    }

    public function before_render(&$twig_vars, &$twig) {

 //// ==== THIS IS CALLED WITH {% include core.[TEMPLATE_NAME] %} SEE Base.html ==== ////

        // core/tpl/file
        $twig_vars['core'] = $this->get_core_tpl();

//// === THIS IS A SIMPLIFIED VERSION OF THE ABOVE VAR - IT'S CALLED WITH {% include core_head %} but It throws the same error ==== ////

        $twig_vars['core_head'] = CORE_TPL . '/head.html';

        // theme/tpl/file
        $twig_vars['tpl'] = $this->get_tpl();

        // theme/tpl/views/file
        $views = $this->get_views();
        $twig_vars['views'] = $views;

        // theme/content/file
        $theme = $this->get_theme_content();
        $twig_vars['theme'] = $theme;
        //var_dump($twig_vars['theme']);
    }

//// ==== THIS IS THE FUNCTION IN QUESTION ==== ////

    private function get_core_tpl() {

        foreach ($this->tpl_array as $value) {

            $pattern = array('/ /', '/_/');
            $name = preg_replace($pattern, '_', $value);
            $core[$name] = CORE_TPL . $value . '.html';
        }
        return $core;
    }

    private function get_tpl() {

        foreach ($this->tpl_array as $value) {

            $pattern = array('/ /', '/_/');
            $name = preg_replace($pattern, '_', $value);
            $tpl[$name] = 'tpl/' . $value . '.html';
            $page_tpl = $this->theme . '/tpl/' . $this->tpl_name . '-' . $value . '.html';

            if (file_exists($page_tpl))
                $tpl[$name] = '/tpl/' . $this->tpl_name . '-' . $value . '.html';
        }
        return $tpl;
    }

    private function get_theme_content() {
        $content = $this->get_files($this->theme . '/content', '.md');
        $content_data = array();

        if (empty($content))
            return;

        foreach ($content as $key) {
            $file = $this->theme . '/content/' . $key . '.md';
            $pattern = array('/ /', '/-/');
            $title = preg_replace($pattern, '_', strtolower($key));
            $data = file_get_contents($file);
            $content_data[$title] = \Michelf\MarkdownExtra::defaultTransform($data);
        }
        return $content_data;
    }

    private function get_views() {

        $view_dir = $this->theme . '/tpl/views';
        $views = $this->get_files($view_dir);

        if (empty($views))
            return;

        $pattern = array('/ /', '/-/');

        foreach ($views as $key) {
            $name = preg_replace($pattern, '_', $key);
            $view[$name] = 'tpl/views/' . $key . '.html';

            if (file_exists($this->theme . '/tpl/' . $this->tpl_name . '-' . $key . '.html'))
                $view[$name] = 'tpl/views/' . $this->tpl_name . '-' . $key . '.html';
        }

        if (!isset($view))
            return array(0);

        return $view;
    }

    // scrive.php lib
    private function get_files($directory, $ext = '.html') {

        if (!is_dir($directory))
            return false;

        $array_items = array();

        if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {

                $file = $directory . "/" . $file;
                if (!$ext || strstr($file, $ext))
                $array_items[] = basename($file, $ext);
            }
            closedir($handle);
        }
        return $array_items;
    }
} ?>

非常感谢任何帮助! Thnx

0 个答案:

没有答案
相关问题