Sizzling Code
Sizzling Code

Reputation: 6070

Smarty Templates with CodeIgniter, can't load template when too many directories in hierarchy

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 is how I call it.

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

Simply, Smarty only allows 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

Here is my project:

https://github.com/pakistanihaider/HouseRentSystem

Database regarding this project:

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


Somehow found the main problem thanks to @Sauryabhatt. I think problem exists in {{extends file='adminLayout.tpl'}}. How does it know 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 to define the path to the layout, but seems that it also didn't work 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.

Upvotes: 0

Views: 2242

Answers (5)

SMHasnain
SMHasnain

Reputation: 706

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.

Upvotes: 1

Sizzling Code
Sizzling Code

Reputation: 6070

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..

Upvotes: 1

Sauryabhatt
Sauryabhatt

Reputation: 269

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

Upvotes: 0

Federico J.
Federico J.

Reputation: 15892

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

Upvotes: 0

Bogdan
Bogdan

Reputation: 44526

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.

Upvotes: 0

Related Questions