SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smarty template engine and file includes

    I've had to integrate a set of mortgage calculators into a site I'm working on. The app uses the Smarty template engine and I'm having some problens trying to figure out how to add in some includes (and or variables) into the template.

    On the site I have two specials side bar text areas that are nothing more than includes, work fine. But now after integrating the mortgage calc into the app (and using one of its templates for the layout) I can get these two text files back into the pages.

    I have this in the main calculators.php:

    PHP Code:
    include('includes/overall.inc.php');
    $smarty->assign('contents',$smarty->fetch('calculators.tpl'));
    $smarty->display('main.tpl'); 
    so I tried:

    PHP Code:
    $smarty->assign('specials1',smarty->fetch('/pages/special-news-1.txt'); 
    and then in the template (main.tpl) I used this:

    PHP Code:
    {$specials1
    where the text is to be placed. Got an error it couldnt find the file so I tried the old ../ to get me back up to the root of the site, and then noticed the smarty.class.php was four levels down so I tired: ../../../../ Still can't find the file. So I tried a full url and path to the file, still NFG.

    OK, scratch that ... went to the smarty site and did some quick reading and found this built in function:

    PHP Code:
    {include file="footer.tpl"
    I dropped that into the main.tpl file and tried all the variations of the path mentioned above .... still cant find the file.

    So now I'm really lost. The functions seem to be working but nothing I try seems to allow smarty (is it really?) to find this file..... any clues here?

    BTW, I dont want to move these two text files into the calculator folder as I have the rest of the site's content in the same folder as these ... makes it easier for the customer.



    thanks

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello DC

    This is very strange problem you have, as absolute path to the file should definitely do the trick..
    The only thing I have noticed with code you provided is on line
    PHP Code:
    $smarty->assign('specials1',smarty->fetch('/pages/special-news-1.txt'); 
    where you forgot $ sign before smarty and the closing bracket. But I guess that was the typo.

    How did you set up template dir and compile dir for smarty? It is higly recommended that you provide absolute paths, to avoid that smarty gets lost with files you want it to read.
    $smarty->template_dir = 'full path to the template dir';
    $smarty->compile_dir = 'full path to the compile dir';

    You could try to debug with smarty's debugging console, but I think it won't work with fetch() API.

    Maybe you should try to execute the code you are using for those calculations on mail.php (if that is the filename) and try using (include file="your_template.tpl"); within main.tpl. The relative paths should work! Try dumping $template_dir.

    Sorry I couldn't help you more. I'm out of ideas.
    -- Jelena --

  3. #3
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah the missing $ was a typo in my post. You may have a point with the path though, I moved the two files I wanted to read into the templates directory and changed the read to this:

    PHP Code:
    $smarty->assign('specials1'$smarty->fetch('special-news-1.txt')); 
    It found them fine then .... it's only when I take them out of the folder and put them up higher in the directory (with the site's pages) it can't find them..

    This is what I have in the overall.inc.php file (the default set up):

    PHP Code:
        /* no cache */
        
    header("Cache-Control: no-store, no-cache, must-revalidate");
        
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        
    header("Pragma: no-cache");

        
    /* turning off NOTICES */
        
    error_reporting(E_ALL E_NOTICE E_USER_NOTICE);
        
        
    /* full path to calculator dir */
        
    $calcdir realpath(dirname(__FILE__).'/../');

        
    /* including components */
        
    include($calcdir.'/includes/config.inc.php');
        include(
    $calcdir.'/includes/functions.inc.php');
        include(
    $calcdir.'/includes/smarty/Smarty.class.php');

        
    /* initialising Smarty engine */
        
    $smarty = new Smarty;
        
    $smarty->template_dir $calcdir.'/templates';
        
    $smarty->compile_dir  $calcdir.'/includes/smarty/templates_c';
        
    $smarty->assign('config'$config);
        
        
    /* GZIP compression */
        
    @ob_start("ob_gzhandler"); 
    So are you saying to set up the full path to those two directories from the site's root folder?

  4. #4
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes. I usually do that this way.

    PHP Code:
    define("ROOT"str_replace("\\""/"realpath(dirname(__FILE__)."/../")."/"));

    define("SMARTY_DIR"ROOT."_smarty/libs/");
    define("TEMPLATE_DIR"ROOT."_templates/");
    define("TEMPLATEC_DIR"ROOT."_templates_c/");

    require(
    SMARTY_DIR.'Smarty.class.php');

    $smarty = new Smarty;
    $smarty->template_dir TEMPLATE_DIR;
    $smarty->compile_dir TEMPLATEC_DIR
    I prefer using constants, as this is something that shouldn't be changed during the script.
    -- Jelena --


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •