我正在使用 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 个额外的目录,我想知道是否有任何解决方案,以便我可以在层次结构中拥有无限或至少更多的目录,这样我就不会搞砸文件系统。
这是我的项目:
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);
如果我将失败放在管理目录中,它会工作,但如果我将文件移到管理目录的另一个目录中,它就会停止工作。
OMG,经过这么长时间,我几乎尝试了所有方法,我什至下载了 zebra CMS 来看看为什么那里的代码可以工作而不是我的,xD 我什至尝试用我的代码更改那里的代码,即使知道它不会解决任何问题,但只是希望。 xD
但最终我终于看到了真正的问题, 当我尝试将模板文件放入目录的另一个目录中时,管理布局中的相对路径受到干扰。
我有一些文件组件,例如单独文件中的菜单选项卡,我将其包含在主布局文件中。 例如:
{{include file="../admin/ui_components/user-media.tpl" name="user media"}}
如果模板位于 1 个目录内,则模板可以正常工作,但是当我尝试添加 1 个目录并将文件放入其中时,路径出现错误。
CI Smarty 最大的问题是,它从不给出错误,仅在 tpl 文件出现任何错误时给出白色空白页。
如果错误系统很好,我可能会更快找到解决方案。
无论如何,现在只是添加了基本路径,所以无论我进入多少个目录,都不会出现任何问题..
感谢大家的支持。看来我的赏金被浪费了 xD..
改变
{{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';
然后你就完成了。尝试并运行该应用程序。希望它现在对你有用。
答案似乎很简单:拼写错误。你把模板文件的路径写错了。根据您提供的目录结构,您应该有这个:
$this->parser->parse('admin/sitesettings/site-settings.tpl',$this->data);
而不是这个:
$this->parser->parse('admin/sitesitesettings/site-settings.tpl',$this->data);
我还使用 CI-Smarty 进行了快速 CI 设置,以确保它可以在多个子目录下正常工作。
检查库,我认为你应该修改文件
libraries/MY_Parser
(https://github.com/Vheissu/Ci-Smarty/blob/master/libraries/MY_Parser.php)。在那里,您可能会找到以下方法:
当您调用
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 检索完整树并将其添加到_update_theme_paths()
,它会自动执行此操作,XD
如果您的 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');
}
}
main.php 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);