title: Smarty PHP - how to build include modules
body: <p>I would like to write an efficient mechanism to include files in Smarty PHP. This is to be a kind of "modules" which I can use inside my website. I imagine this model:</p>
  
  <pre><code>application
  cache
  configs
  libs
  modules
  | search
    | templates
    | - search.tpl
    - search.php
  | slider
    | templates
    | - slider.tpl
    - slider.php
  | weather
    | templates
    | - weather.tpl
    - weather.php
  plugins
  templates
  templates_c
  </code></pre>
  
  <p>Then my <code>index.php</code>:</p>
  
  <pre><code>$modules = array(
      'search',
      'weather',
      'slider'
  );
  
  if(isset($modules)) {
  
      $module = array();
  
      foreach($modules AS $m) {
  
          require_once('modules/' . $m . '/' . $m . '.php');
  
          $module[$m] = 'modules/' . $m . '/templates/' . $m . '.tpl';
  
      }
  
      $smarty-&gt;assign("module", $module);
  
  }
  </code></pre>
  
  <p>And finally <code>index.tpl</code> in the part where I want to add a module:</p>
  
  <pre><code>{include file=$module.weather}
  </code></pre>
  
  <p>In theory, everything works but I know that this is not a professional way.
  It also restricts me when I want to display module with a different HTML code.</p>
  
  <p>I don't have ideas, can you give me advise please?
  Thanks!</p>
  
  <p><strong>_ EDIT _</strong></p>
  
  <p>I found a certain way please check whether it is the correct way. I created in <code>libs/plugins/</code> smarty pligin file <code>funcion.module.php</code>:</p>
  
  <pre><code>/*
   * Smarty plugin
   * -------------------------------------------------------------
   * File:     function.module.php
   * Type:     function
   * Name:     module
   * Purpose:  include php and tpl files from modules folder
   * -------------------------------------------------------------
   */
  function smarty_function_module($params, &amp;$smarty)
  {
      // uri to your php file module
      $module_file = 'modules/';
  
      // check if name of module exist
      if(empty($params['name'])) {
          trigger_error("error: missing 'name' parameter");
          return;
      }
  
      // create TPL 
      $tpl = $smarty-&gt;createTemplate($module_file . $params['name'] . '/templates/' . $params['name'] . '.tpl');
  
      // Default URL
      $tpl-&gt;assign('URL', URL);
  
      // Include PHP Module
      include($module_file . $params['name'] . '/' . $params['name'] . '.php');
  
      // Display tpl
      $tpl-&gt;display();
  
  }
  </code></pre>
  
  <p>Now i can write code in my php file of module, for ex.:</p>
  
  <pre><code>// SQL
  $db = new MysqliDb (HOSTNAME, USERNAME, PASSWORD, DATABASE);
  
  // GET movies
  $db-&gt;join('users u', 'c.author = u.id');
  $db-&gt;join('components_movies_details y', 'y.component = c.value');
  $db-&gt;where('c.type', 'movie');
  $db-&gt;where('c.status != "0"');
  $db-&gt;orderBy('c.id', 'desc');
  $result = $db-&gt;get('components c', 15, 'c.id AS cid, c.image, c.title, c.description, c.value, u.login, y.views, y.comments, y.duration');
  
  $movies = array();
  
  $x = 0;
  
  foreach($result AS $id) {
  
      $movies[] = array(
          'lp' =&gt; $x++,
          'id' =&gt; $id['cid'],
          'views' =&gt; number_format($id['views'], 0, '', ' '),
          'comments' =&gt; $id['comments'],
          'duration' =&gt; str_replace('00:', '', $id['duration']),
          'login' =&gt; $id['login'],
          'value' =&gt; $id['value'],
          'name' =&gt; $id['title'],
          'desc' =&gt; $id['description']
      );
  
  }
  
  $tpl-&gt;assign('movie',  $movies[0]);
  $tpl-&gt;assign('movies',  array_slice($movies, 1));
  </code></pre>
  
  <p>And in my TPL file:</p>
  
  <pre><code>{module name=movies}
  </code></pre>
  
  <p>Is that good way?</p>