title: How to Load Twig Template Outside of the Theme Folder?
body: <p>I just learning Twig templating and I am having trouble figuring out how to load a template part that is in a directory outside of the current themes' directory so they can be reused by other themes.</p>
  
  <p>I am starting with just a simple loading of a head.html template part located in the core/tpls/ directory. With the variouse methodes I have tried, I get the following error:</p>
  
  <blockquote>
    <p>PHP Fatal error:  Uncaught exception 'Twig_Error_Loader' with message
    'Unable to find template "/home/ubuntu/workspace/core/tpls/head.html"
    (looked into: /home/ubuntu/workspace/themes/core) in "base.html" at
    line 5.</p>
  </blockquote>
  
  <p>The file is in the directory and the path is correct despite that the log error says otherwise.</p>
  
  <p>My base.html located in the themes/core/ directory contains the custom Twig function to include the head.html template located outside of the theme directory:</p>
  
  <pre><code>&lt;html lang="en" class="no-js"&gt;
  &lt;head&gt;
  
      {% 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 %} &lt;-- 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 //
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;header id="header"&gt;
  
      {% include tpl.header %} // This works via the get_tpl() function
  
  &lt;/header&gt;
  
      {% include tpl.breadcrumbs %} // This works too
  
  [. . .]
  </code></pre>
  
  <p>I hate to clutter this with an entire class but it may be helpful for someone to see what I have wrong.  The get_core_tpl() function is what I cannot get working where my other get_*() functions work as they should.  See my comments there and also, take note to my comments in the befor_rendor() function:</p>
  
  <pre><code>&lt;?php
  
  class Get_TPLs {
  
      private $tpl_name,
              $theme = 'core',
              $tpl_array = array(
                  'breadcrumbs',
                  'content',
                  'cover',
                  'footer',
                  'head',
                  'header',
                  'sidebar'
      );
  
      public function before_load_content(&amp;$file) {
          $this-&gt;tpl_name = basename($file, '.md');
      }
  
      public function config_loaded(&amp;$settings) {
  
          if (isset($settings['core_tpl']))
              $this-&gt;core_tpl = $settings['core_tpl'];
  
          if (isset($settings['tpl_array']))
              $this-&gt;tpl_array = $settings['tpl_array'];
  
          if (isset($settings['theme']))
              $this-&gt;theme = THEMES_DIR . $settings['theme'];
      }
  
      public function file_meta(&amp;$meta) {
  
          $config = $meta;
          if (isset($config['slug'])):
              $this-&gt;tpl_name = strtolower($config['slug']);
          endif;
      }
  
      public function before_render(&amp;$twig_vars, &amp;$twig) {
  
   //// ==== THIS IS CALLED WITH {% include core.[TEMPLATE_NAME] %} SEE Base.html ==== ////
  
          // core/tpl/file
          $twig_vars['core'] = $this-&gt;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-&gt;get_tpl();
  
          // theme/tpl/views/file
          $views = $this-&gt;get_views();
          $twig_vars['views'] = $views;
  
          // theme/content/file
          $theme = $this-&gt;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-&gt;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-&gt;tpl_array as $value) {
  
              $pattern = array('/ /', '/_/');
              $name = preg_replace($pattern, '_', $value);
              $tpl[$name] = 'tpl/' . $value . '.html';
              $page_tpl = $this-&gt;theme . '/tpl/' . $this-&gt;tpl_name . '-' . $value . '.html';
  
              if (file_exists($page_tpl))
                  $tpl[$name] = '/tpl/' . $this-&gt;tpl_name . '-' . $value . '.html';
          }
          return $tpl;
      }
  
      private function get_theme_content() {
          $content = $this-&gt;get_files($this-&gt;theme . '/content', '.md');
          $content_data = array();
  
          if (empty($content))
              return;
  
          foreach ($content as $key) {
              $file = $this-&gt;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-&gt;theme . '/tpl/views';
          $views = $this-&gt;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-&gt;theme . '/tpl/' . $this-&gt;tpl_name . '-' . $key . '.html'))
                  $view[$name] = 'tpl/views/' . $this-&gt;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;
      }
  } ?&gt;
  </code></pre>
  
  <p><strong>Any help will be greatly appreciated!</strong> Thnx</p>