0

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


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 didn't 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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sizzling Code
  • 5,932
  • 18
  • 81
  • 138

5 Answers5

1

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

Sizzling Code
  • 5,932
  • 18
  • 81
  • 138
1

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.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
SMHasnain
  • 696
  • 4
  • 13
0

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.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • 1
    Sir, yes there is a typo in my question, which i will fix now in question. but problem do exist, i have tried like tons of time, i even tried it again, just now after you posted about my typo, in hope that all those times i tried i might be wrong.. I would be glad, if you or anyone can solve this issue. Please can you upload the project on github, i want to see why it works for you but not for me.. This is the Project i am working on, and problem exist in this project. https://github.com/pakistanihaider/HouseRentSystem – Sizzling Code Aug 30 '14 at 14:18
  • 1
    i also want to mention, All my template files reside in themes folder not in codeigniter main view folder. i follwed the pattern from this guy who made CI-smarty. he made the project in CI-smarty and did put themes option in his project. https://github.com/Vheissu/Zebra – Sizzling Code Aug 30 '14 at 14:23
0

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

Community
  • 1
  • 1
Federico J.
  • 15,388
  • 6
  • 32
  • 51
  • Sorry for late reply, yes i have tried that already in MY_Parser file before posting the question here. seems like it do not have any effect :( – Sizzling Code Sep 05 '14 at 09:48
0

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);
Sauryabhatt
  • 269
  • 2
  • 14
  • Should i replace my function code with the above code you gave? – Sizzling Code Sep 05 '14 at 09:41
  • no add it before $this->_add_paths(); but you have to code config_item('is_admin') to true or false yourself – Sauryabhatt Sep 05 '14 at 09:45
  • still not working, my directory structure starts from `views` not from `admin` and `front`. `admin` directory resides inside of of `views` directory. so i tried you code and also did tried against my directory structure, didn't work :( i added my link to my project on github, please if you have some time to check it out. – Sizzling Code Sep 05 '14 at 10:00
  • 1
    Can you check $this->parser->parse('site-settings.tpl',$this->data); Should work without modifying, and site-settings.tpl can be at any depth in directory as smarty scans complete directory. – Sauryabhatt Sep 05 '14 at 10:06
  • problem is same that if the file is in the inner directory Smarty dont find it, but if file is out in open just in admin directory it works. But yes its good to know that i don't have to write the whole path now, only writing by filename can do the trick. xD. But still the main problem remaining... – Sizzling Code Sep 05 '14 at 10:14
  • you need database wait i give database file to you too. – Sizzling Code Sep 05 '14 at 10:59
  • 1
    I modified your project main.php and used $this->parser->parse('test/test1/testsaurabh.tpl', $data); and it worked for me. I have put test in view folder. I also copied the code of your My_controller.php to constructor of Frontend_controller and extended ci_controller dirctly from frontend _controller – Sauryabhatt Sep 05 '14 at 11:16
  • can i see the updated code please. so i can do the changes accordingly. – Sizzling Code Sep 05 '14 at 11:28
  • yes it worked for me too. but see, if you add 1 more directory there inside `test1`, name it `test2` so the link would be `$this->parser->parse('test/test1/test2/testsaurabh.tpl', $data);` the page wont gonna work. – Sizzling Code Sep 05 '14 at 11:34
  • $this->parser->parse('test/test1/test2/testsaurabh.tpl', $data); also worked. – Sauryabhatt Sep 05 '14 at 11:42
  • Yes my mistake that worked, i didnt moved the file inside test2 before..Again very sorry for trouble. :( please can you check this path also. as you have downloaded the code.. and i already have tried this path and it didnt worked out for me.. view is inside `admin/system/systemConfigurationSitePreferences.tpl`. i am trying to view that file from this controller `admin/configurations/SitePreferences()` using this method `$this->parser->parse('admin/system/systemConfigurationSitePreferences',$this->data);`. Please see if it works for you. – Sizzling Code Sep 05 '14 at 11:55
  • it worked just did the same thing to Admin Controller as in Front Controller, copy paste the my controller code – Sauryabhatt Sep 05 '14 at 12:05
  • finally figured out the problem.. The hirarichy works but only if the file is not extended with adminlayout. If i extend the file `{{extends file='adminLayout.tpl'}}` then cant put the file inse. Sir have you tried testing with my code, i mean dont delete the code in my file. if you keep that file in `system` directory as it is and access it via method then you will see it wont gonna work. but if you move that file from system to the upper directory and change the path accordingly, it will start working. hirarichy problem exist cuz of layout. – Sizzling Code Sep 05 '14 at 12:17
  • can you check it again for `{{extends file='adminLayout.tpl'}}`. may be need to define a path for the layout in every file, but i dont know how to define a path to layout to fix this problem. – Sizzling Code Sep 05 '14 at 12:17
  • I checked systemConfigurationSitePreferences and {{extends file='adminLayout.tpl'}}. is already present and worked for me. – Sauryabhatt Sep 05 '14 at 13:02
  • wow, if it really works for you then please help me solve it. cuz its not working here. can you come over team viewer.. i know you must be a busy person, but i will really appreaciate if you help me out. if you use skype, my skype id is `pakistanihaider`, i am online.. or if you have any other messenger i will send you its id.. but really want to solve this issue. im not gay but if you solve this issue i will love you for that and will add more bounty points to give you... Just please solve it... im going mad over this problem.. – Sizzling Code Sep 05 '14 at 13:13
  • Im in chat room that you invited in, you there.? – Sizzling Code Sep 05 '14 at 13:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60736/discussion-between-sizzling-code-and-sauryabhatt). – Sizzling Code Sep 06 '14 at 06:14