带有CodeIgniter的Smarty模板,当层次结构中的目录太多时无法加载模板

2cmtqfgy  于 2022-12-16  发布在  其他
关注(0)|答案(5)|浏览(121)

我在用CI Smarty

https://github.com/Vheissu/Ci-Smarty

据我所知,这有两个问题。但我打开这个问题的问题是,如果.tpl文件在另一个目录的目录中,我就不能加载.tpl文件。
例如,这是SmartyTemplate的当前目录结构

--Themes
  --SomeOtherThemeName
  --Default //Default Theme Directory I am currently using
    --css
    --js
    --images
    --views
      --admin (directory)
          --sitesettings (directory)
            --site-settings.tpl (template file) //This Template file will Not Work

如果我将此模板文件移动到父目录admin,如果我调用它,它将工作,但如果我从sitesettings目录内调用它,它将不工作。
我是这么说的。

function functionName(){
    $data['title']="Some Title";
    $this->parser->parse('admin/sitesettings/site-settings.tpl',$this->data);
}

简单地说,Smarty只允许我有1视图文件夹下的层次结构额外的目录,我想知道是否有任何解决这个问题,这样我就可以在层次结构中有无限或至少更多的目录,所以我没有搞砸了文件系统。
更新:如果有人想看我的项目代码,请到这个GitHub项目。
https://github.com/pakistanihaider/HouseRentSystem
有关此项目的数据库。
http://www.mediafire.com/view/1jp9u906r8i10u0/houserentsystem.sql
不知何故发现了主要问题感谢@Sauryabhatt。我认为问题存在于{{extends file='adminLayout.tpl'}}如何知道文件存在的地方,我的意思是,如果我把文件移动到最内部的目录,它将如何知道主布局文件退出到哪里,它将是一个孩子?我需要定义一个路径时,扩展一个文件?

**更新:**还尝试定义布局的路径,但似乎它也没有为我工作。

$this->parser->parse('extends:layouts/adminLayout.tpl|file:systemConfigurationSitePreferences.tpl',$this->data);

它的工作,如果我把失败在管理目录,但停止工作,如果我把文件内的另一个目录的管理目录。

s5a0g9ez

s5a0g9ez1#

天啊,经过这么长时间,我尝试了几乎所有的东西,我甚至下载了斑马CMS,看看为什么有代码的工作,而不是我的,我甚至尝试改变有代码与我的,即使知道它不会解决任何问题,但只是希望. XD
但最终我终于看到了真实的的问题,当我试图把模板文件放在一个目录的另一个目录中时,相对路径在管理布局中受到干扰。
我有一些文件组件,如菜单选项卡在单独的文件中,我包括在主布局文件。例如:

{{include file="../admin/ui_components/user-media.tpl" name="user media"}}

因此,模板是工作,如果它是在1个目录,但当我试图添加1个目录,并把它里面的文件,路径给错误。
CI Smarty最大的问题是,如果有任何关于tpl文件的错误,它从来没有给出错误,只给白色空白页。
如果错误系统是好的,我可能会更快地找到解决方案。
无论如何,现在刚刚添加了基本路径,所以无论我进入多少目录,它都不会给予任何问题。
谢谢你的支持.看来我的赏金被浪费了.

svdrlsy4

svdrlsy42#

变更

{{include file="../admin/ui_components/user-media.tpl" name="user media"}}
<!-- #menu -->
{{include file="../admin/ui_components/left_menu.tpl" name="left menus"}}

到这个

{{include file="{{getBasePath()}}/themes/{{$themeName}}/views/admin/ui_components/user-media.tpl" name="user media"}}
<!-- #menu -->
{{include file="{{getBasePath()}}themes/{{$themeName}}/views/admin/ui_components/left_menu.tpl" name="left menus"}}

以及

{{include file="../admin/ui_components/top_navigation.tpl" name="top navigation"}}

变成这样

{{include file="{{getBasePath()}}/themes/{{$themeName}}/views/admin/ui_components/top_navigation.tpl" name="top navigation"}}

然后在site_helper.php文件中定义以下代码

//Returns the BASEPATH for the Template
if (!function_exists('getBasePath')) {
    function getBasePath()
    {
        return FCPATH;
    }
}

最后在MY_Controller中定义主题

$this->data['themeName'] = 'default';

那么你就都完成了。试着运行这个应用程序。希望它现在对你有用。

lmvvr0a8

lmvvr0a83#

答案似乎很简单:一个拼写错误。您将模板文件的路径写错了。根据您提供的目录结构,您应该有以下内容:

$this->parser->parse('admin/sitesettings/site-settings.tpl',$this->data);

而不是这个:

$this->parser->parse('admin/sitesitesettings/site-settings.tpl',$this->data);

我还做了一个快速CI设置与CI-智能,以确保它的工作只是罚款与多个子目录。

bihw5rsg

bihw5rsg4#

检查库,我认为您应该修改文件libraries/MY_Parserhttps://github.com/Vheissu/Ci-Smarty/blob/master/libraries/MY_Parser.php)。在那里,您可能会发现以下方法:

  • parse:调用的方法
  • _find_view:该方法将检查文件是否存在

当你调用parse时,它会调用_find_view,从两个地方加载路径:第307行,加载

$locations = $this->_template_locations;

其中,_template_locations是在第375行中方法_update_theme_paths()中加载的数组:

$this->_template_locations = array(
    config_item('smarty.theme_path') . $this->_theme_name . '/views/modules/' . $this->_module .'/layouts/',
    config_item('smarty.theme_path') . $this->_theme_name . '/views/modules/' . $this->_module .'/',
    config_item('smarty.theme_path') . $this->_theme_name . '/views/layouts/',
    config_item('smarty.theme_path') . $this->_theme_name . '/views/',
    APPPATH . 'modules/' . $this->_module . '/views/layouts/',
    APPPATH . 'modules/' . $this->_module . '/views/',
    APPPATH . 'views/layouts/',
    APPPATH . 'views/'
    // Here, load your custom path:
    APPPATH . 'views/admin/sitesettings'
);

在行314中,用$new_locations:

$new_locations = array(
    config_item('smarty.theme_path') . $this->_theme_name . '/views/modules/' . $current_module .'/layouts/',
    config_item('smarty.theme_path') . $this->_theme_name . '/views/modules/' . $current_module .'/',
    APPPATH . 'modules/' . $current_module . '/views/layouts/',
    APPPATH . 'modules/' . $current_module . '/views/'
);

我还没有测试它,抱歉,但我认为,如果你在那里添加文件夹的路径,它会找到你的文件,你可以增加你的目录路径。一个改进,它将自动添加你的文件夹结构到方法_update_theme_paths()的数组,检查:PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree并将其添加到_update_theme_paths(),它会自动执行此操作,XD

5sxhfpxr

5sxhfpxr5#

如果您的smarty版本是2.1或更高版本,则无需写入目录名,只需写入文件名即可。
使用此

$this->parser->parse('site-settings.tpl',$this->data);

代替

$this->parser->parse('admin/sitesettings/site-settings.tpl',$this->data);

前端控制器代码

class   Frontend_Controller extends CI_Controller{

function __construct() {
    parent::__construct();
    $this->data['errors'] = array();
    $this->data['site_name'] = array();
    $this->load->library('Smarty.php');
    $this->load->library('parser');
    $this->load->model('Common_Model');
    $this->load->model('users_management/login_check');
   }
}

index.php函数的代码

$myArray = array('Controller' => $this->router->fetch_class(), 'Method' => $this->router->fetch_method());
    $this->smarty->assign('Fetch', $myArray);

    $this->smarty->assign("lang", "english");
    $data['template'] = "home.tpl";
    $data['templateName'] = "Home template";
    $this->parser->parse('test/test1/test2/testsaurabh.tpl', $data);

相关问题