Smarty Templates with Codeigniter, Cant Load Templ

2019-08-20 07:01发布

I'm using CI Smarty

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

There are Two issues with this as far as i have noticed. but the issue for which I opened this question is that I cant load a .tpl file if that .tpl file is inside the directory of another directory.

e-g this is my current directory structure for 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

If I move this Template file to parent directory which is admin, it will work if i call it, but if i call it from inside sitesettings directory it will not work.

Here how i call it.

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

Simply, Smarty Only Allow me to have 1 extra Directory in hierarchy under views folder, I want to know if there is any fix for this so that i can have unlimited or at least more directories in hierarchy so I don't have messed up file system.


update: if anyone wanna see my project coding please go to this github project.

https://github.com/pakistanihaider/HouseRentSystem

Database regarding this project.

http://www.mediafire.com/view/1jp9u906r8i10u0/houserentsystem.sql

NOTE: cant attach files here so uploaded the .sql file on mediafire and added the link. If there is any rules against it, Admins sure can remove the link but please let me know the proper way of giving the file link.


Somehow found the main problem thanks to @Sauryabhatt. I think problem exists in {{extends file='adminLayout.tpl'}} how do it knows where the file exist, i mean if i move the file inside most inner directory how it will know where the main layout file exit to which it will be a child.? do i need to define a path when extending a file?


Update: Also Tried like to define the path to the layout, but seems that it also didnt worked out for me.

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

it works if i put the fail in admin directory, but stops working if i move the file inside in another directory of admin directory.

5条回答
冷血范
2楼-- · 2019-08-20 07:26

Change

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

into this

{{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"}}

and

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

into this

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

then in your site_helper.php file define this code

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

and finally define your theme in the MY_Controller

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

Then you are all done. Try and run the application. Hope it works for you now.

查看更多
我想做一个坏孩纸
3楼-- · 2019-08-20 07:26

Checking the library, I think you should modify the file libraries/MY_Parser (https://github.com/Vheissu/Ci-Smarty/blob/master/libraries/MY_Parser.php) . There, you may find the following methods:

  • parse: The method you invoke
  • _find_view: The method will check whether the file exists or not

When you invoke parse, it'll call _find_view which load the paths from two places: In line 307, loading

$locations = $this->_template_locations;

where _template_locations is an array loaded in the method _update_theme_paths() in the line 375:

$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'
);

and in line 314, with $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/'
);

I haven't tested it, sorry, but I think that if you add the path to your folders there, it'll find your files and you'll be able to increase your directory path. An improvement it'll be to add automatically your folder structure to the array in the method _update_theme_paths(), check: PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree and add it to _update_theme_paths(), and it'll do it automatically, XD

查看更多
何必那么认真
4楼-- · 2019-08-20 07:30

If your smarty version is 2.1 or later there is no need to write directory name just file name is enough.

Use This

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

Instead of

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

Code of Frontend Controller

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');
   }
}

code of main.php index.php function

$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);
查看更多
霸刀☆藐视天下
5楼-- · 2019-08-20 07:43

The answer seems to be very simple: a typo. You've written the path to the template file wrong. According to the directory structure you've presented you should have this:

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

Instead of this:

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

I've also done a quick CI setup with CI-Smarty to make sure and it works just fine with multiple subdirectories.

查看更多
闹够了就滚
6楼-- · 2019-08-20 07:48

OMG, Aftr this long time, i tried almost everything, i even downloaded the zebra CMS to see why there code is working and not mine, xD i even tried changing there code with mine even knowing that it wont gonna solve anything but just was hoping. xD

But in the end finally i got my eyes on the Real Problem, When i tried to put the template file inside another directory of a directory the relative path got disturbed in admin layout.

I had some File Components like menus tabs in separate files which i was including in the Master Layout File. e-g:

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

so template was working if it was inside 1 directory but when i tried to add 1 more directory and put file inside it, the path gave error.

The Biggest Problem with CI Smarty is, it never gives error only give White Blank Page if there is any error regarding the tpl files.

If the error system would have been good i might would have found the solution faster.

Anyhow, now just Added the Base Path, so no matter how many directories i go in, it will not gonna give any problem..

Thanks for All your Support. Looks like my Bounty Got Wasted xD..

查看更多
登录 后发表回答