Smarty::addTemplateDir PHP Method

addTemplateDir() public method

Add template directory(s)
public addTemplateDir ( string | array $template_dir, string $key = null ) : Smarty
$template_dir string | array directory(s) of template sources
$key string of the array element to assign the template dir to
return Smarty current Smarty instance for chaining
    public function addTemplateDir($template_dir, $key = null)
    {
        // make sure we're dealing with an array
        $this->template_dir = (array) $this->template_dir;
        if (is_array($template_dir)) {
            foreach ($template_dir as $k => $v) {
                $v = str_replace(array('//', '\\\\'), DS, rtrim($v, '/\\')) . DS;
                if (is_int($k)) {
                    // indexes are not merged but appended
                    $this->template_dir[] = $v;
                } else {
                    // string indexes are overridden
                    $this->template_dir[$k] = $v;
                }
            }
        } else {
            $v = str_replace(array('//', '\\\\'), DS, rtrim($template_dir, '/\\')) . DS;
            if ($key !== null) {
                // override directory at specified index
                $this->template_dir[$key] = $v;
            } else {
                // append new directory
                $this->template_dir[] = $v;
            }
        }
        $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
        return $this;
    }

Usage Example

Example #1
0
 /**
  * @param null $template
  * @param null $cache_id
  * @param null $compiled_id
  * @param null $parent
  * @return string
  *
  * @throws \SmartyException
  */
 public function get_template($template = null, $cache_id = null, $compiled_id = null, $parent = null)
 {
     $this->init_engine();
     $this->template_engine->setCompileDir($this->compile_dir);
     foreach ($this->template_dirs as $i => $dir) {
         $i == 0 && $this->template_engine->setTemplateDir($dir);
         $i > 0 && $this->template_engine->addTemplateDir($dir);
     }
     $this->load_lang_vars($this->get_lang_file());
     return $this->template_engine->getTemplate($template, $cache_id, $compiled_id, $parent);
 }
All Usage Examples Of Smarty::addTemplateDir