Smarty PHP-如何构建包含模块

Smarty PHP-如何构建包含模块,php,module,model,smarty,Php,Module,Model,Smarty,我想写一个有效的机制来在Smarty PHP中包含文件。这是一种“模块”,我可以在我的网站内使用。我想象这个模型: application cache configs libs modules | search | templates | - search.tpl - search.php | slider | templates | - slider.tpl - slider.php | weather | templates | - weather.tpl

我想写一个有效的机制来在Smarty PHP中包含文件。这是一种“模块”,我可以在我的网站内使用。我想象这个模型:

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
然后我的
index.php

$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->assign("module", $module);

}
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.module.php
 * Type:     function
 * Name:     module
 * Purpose:  include php and tpl files from modules folder
 * -------------------------------------------------------------
 */
function smarty_function_module($params, &$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->createTemplate($module_file . $params['name'] . '/templates/' . $params['name'] . '.tpl');

    // Default URL
    $tpl->assign('URL', URL);

    // Include PHP Module
    include($module_file . $params['name'] . '/' . $params['name'] . '.php');

    // Display tpl
    $tpl->display();

}
最后,
index.tpl
在我想添加模块的部分:

{include file=$module.weather}
理论上,一切都是可行的,但我知道这不是一种专业的方式。 当我想用不同的HTML代码显示模块时,它也会限制我

我没有主意,你能给我一些建议吗? 谢谢

编辑

我找到了一种方法,请检查它是否正确。我在
libs/plugins/
smarty pligin文件
function.module.php
中创建了:

$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->assign("module", $module);

}
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.module.php
 * Type:     function
 * Name:     module
 * Purpose:  include php and tpl files from modules folder
 * -------------------------------------------------------------
 */
function smarty_function_module($params, &$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->createTemplate($module_file . $params['name'] . '/templates/' . $params['name'] . '.tpl');

    // Default URL
    $tpl->assign('URL', URL);

    // Include PHP Module
    include($module_file . $params['name'] . '/' . $params['name'] . '.php');

    // Display tpl
    $tpl->display();

}
现在我可以在模块的php文件中编写代码,例如:

// SQL
$db = new MysqliDb (HOSTNAME, USERNAME, PASSWORD, DATABASE);

// GET movies
$db->join('users u', 'c.author = u.id');
$db->join('components_movies_details y', 'y.component = c.value');
$db->where('c.type', 'movie');
$db->where('c.status != "0"');
$db->orderBy('c.id', 'desc');
$result = $db->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' => $x++,
        'id' => $id['cid'],
        'views' => number_format($id['views'], 0, '', ' '),
        'comments' => $id['comments'],
        'duration' => str_replace('00:', '', $id['duration']),
        'login' => $id['login'],
        'value' => $id['value'],
        'name' => $id['title'],
        'desc' => $id['description']
    );

}

$tpl->assign('movie',  $movies[0]);
$tpl->assign('movies',  array_slice($movies, 1));
在我的TPL文件中:

{module name=movies}
这样好吗