今天研究了下includ函数在实际应用中动态调用的方法 像本站的文章就是通过动态调用includ实现加载不同的内容,这样的好处就是文档页直接存放在html里不是存放再数据库中方便管理。
在默认的方法里
{include file="模块@/(view)目录" /}
例如 index模块中view中的public中的footer文件 正确写法是
{include file="index@/index/public/footer" /}
我们需要加根据业务加载不同的文件 我们需要file=是我们的变量 比如 在index模块的index控制器中 我需要加载 footer 而我在home页面需要加载 footer2 该怎么办呢 查阅了相关资料后
最终的解决方法是 修改tp框架核心文件 thinkphp\library\think\Template.php文件下的parseInclude方法 找到
if(isset($array['append']) &&  $array['append']){
    if(0===strpos($array['append'],'$')){
        $array['append'] = $this->get(substr($array['append'],1));
    }
    $file = $array['append'].$array['file'];
    unset($array['append']);
}else{
    $file  = $array['file'];
}
然后再相应的地方输出变量 前端调用该变量即可
例:  {include file="$html.include" /}
{/literal}

本文地址: http://www.18ai.top/read_2.html

版权声明