Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Warning: Smarty error: unable to read resource: 'foo.tpl'

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> FAQ (Frequently Asked Questions)
View previous topic :: View next topic  
Author Message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7267
Location: Lincoln Nebraska, USA

PostPosted: Thu Feb 03, 2005 3:55 pm    Post subject: Warning: Smarty error: unable to read resource: 'foo.tpl' Reply with quote

Smarty is unable to read the template file for one reason or another. The following instructions assume your smarty object is named $smarty.



    Troubleshooting Steps

    1) $smarty->template_dir

    By default, the value of $smarty->template_dir is templates, meaning that smarty will look for your template directory relative to your php_include path. It is highly recommended to set this to an absolute system filepath such as $smarty->template_dir = "/my/system/path/to/templates";. Relying on php_include path is error-prone and resource intensive. It is also common to use a path relative to the initially executed PHP script, such as $smarty->template_dir = "./templates"; or relative to the currently executing PHP script such as $smarty->template_dir = dirname(__FILE__) . "/templates";

    2) the template file

    Be sure the template file exists under the template directory. For example, if you are calling $smarty->display("foo.tpl"); then Smarty will look for the template file in $smarty->template_dir . "/foo.tpl" (Smarty will automatically place a directory separator between the template directory and the filename if one is necessary.) Likewise, if you call $smarty->display("path/to/foo.tpl"); Smarty will expect that file to be in $smarty->template_dir . "/path/to/foo.tpl"

    3) file/directory existence and permissions

    OK, now for the test that should tell us everything. Try this script and see if the contents of your template file gets displayed:

    Code:
    <?php
    // setup smarty here
    $smarty = new Smarty();
    $smarty->template_dir = "/path/to/my/templates";

    // use your template filename, and be sure to put the "/" in there
    // if your template_dir does not have a trailing "/"
    $myfile = $smarty->template_dir . "/foo.tpl";
    echo "attempting to read $myfile\n";
    readfile($myfile);
    ?>


    If the above script fails with an error, this is certainly the problem. Double check that the file exists, then check the file permissions. For unix systems, you have to check both the file permissions and all directory permissions leading up to it. Files commonly have a chmod of 644 (-rw-r--r--) and directories have a chmod of 755 (drwxr-xr-x). It is important that the user executing the script (either you from the command line or the php user via apache) have access to the template files. This is the most common problem, so check all those file permissions.

    Once you get that working, Smarty should be able to read the template file.


Last edited by mohrt on Fri Nov 07, 2008 9:34 pm; edited 3 times in total
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7267
Location: Lincoln Nebraska, USA

PostPosted: Tue Jul 05, 2005 6:58 pm    Post subject: Reply with quote

Here is a class extension that may help determine your problem (setup or otherwise)

usage:

Code:


error_reporting(E_ALL);

require('Smarty.class.php');

// include test extension
require('Smarty_Test.class.php');

// do this instead of $smarty = new Smarty();
// if you have your own extension, then have it
// extend Smarty_Test() instead of Smarty()
$smarty = new Smarty_Test();

// setup your smarty directories, then execute:
$smarty->test();
exit();


expected output (perms may vary):

Code:
testing start:
template_dir is 'templates'
real system path: /system/path/to/templates
file perms: 0755
OK.
config_dir is 'configs'
real system path: /system/path/to/configs
file perms: 0755
OK.
plugins_dir (0) is 'plugins'
real system path: /system/path/to/plugins
file perms: 0755
OK.
compile_dir is 'templates_c'
real system path: /system/path/to/templates_c
file perms: 0775
OK.
cache_dir is 'cache'
real system path: /system/path/to/cache
file perms: 0775
OK.
testing complete.


Smarty_Test.class.php:

Code:

<?php

class Smarty_Test extends Smarty {

    function Smarty_Test() {
        $this->Smarty();
    }
   
    function test() {
        echo "testing start:<br />\n";
        echo "template_dir is '" . $this->template_dir . "'<br />\n";
        echo "real system path: " . realpath($this->template_dir) . "<br />\n";
        echo "file perms: " . substr(sprintf('%o', fileperms($this->template_dir)), -4) . "<br />\n";
        if(!file_exists($this->template_dir)) {
            echo "error: template_dir '" . $this->template_dir . "' does not exist.<br />\n";
        } elseif (!is_dir($this->template_dir)) {
            echo "error: template dir '" . $this->template_dir . "' is not a directory.<br />\n";           
        } elseif (!is_readable($this->template_dir)) {
            echo "error: template dir '" . $this->template_dir . "' is not readable.<br />\n";           
        } else {
            echo "OK.<br />\n";
        }
        echo "config_dir is '" . $this->config_dir . "'<br />\n";
        echo "real system path: " . realpath($this->config_dir) . "<br />\n";
        echo "file perms: " . substr(sprintf('%o', fileperms($this->config_dir)), -4) . "<br />\n";
        if(!file_exists($this->config_dir)) {
            echo "error: config_dir '" . $this->config_dir . "' does not exist.<br />\n";
        } elseif (!is_dir($this->config_dir)) {
            echo "error: config_dir '" . $this->config_dir . "' is not a directory.<br />\n";           
        } elseif (!is_readable($this->config_dir)) {
            echo "error: config_dir '" . $this->config_dir . "' is not readable.<br />\n";           
        } else {
            echo "OK.<br />\n";
        }
        foreach($this->plugins_dir as $_key => $_plugin_dir) {
            echo "plugins_dir ($_key) is '" . $_plugin_dir . "'<br />\n";
            echo "real system path: " . realpath($_plugin_dir) . "<br />\n";
           echo "file perms: " . substr(sprintf('%o', fileperms($_plugin_dir)), -4) . "<br />\n";
            if(!file_exists($_plugin_dir)) {
                echo "error: plugins_dir '" . $_plugin_dir . "' does not exist.<br />\n";
            } elseif (!is_dir($_plugin_dir)) {
                echo "error: plugins_dir '" . $_plugin_dir . "' is not a directory.<br />\n";           
            } elseif (!is_readable($_plugin_dir)) {
                echo "error: plugins_dir '" . $_plugin_dir . "' is not readable.<br />\n"; 
            } else {
                echo "OK.<br />\n";
            }
        }
        echo "compile_dir is '" . $this->compile_dir . "'<br />\n";
        echo "real system path: " . realpath($this->compile_dir) . "<br />\n";
        echo "file perms: " . substr(sprintf('%o', fileperms($this->compile_dir)), -4) . "<br />\n";
        if(!file_exists($this->compile_dir)) {
            echo "error: compile_dir '" . $this->compile_dir . "' does not exist.<br />\n";
        } elseif (!is_dir($this->compile_dir)) {
            echo "error: compile_dir '" . $this->compile_dir . "' is not a directory.<br />\n";           
        } elseif (!is_readable($this->compile_dir)) {
            echo "error: compile_dir '" . $this->compile_dir . "' is not readable.<br />\n";           
        } elseif (!is_writable($this->compile_dir)) {
            echo "error: compile_dir '" . $this->compile_dir . "' is not writable.<br />\n";           
        } else {
            $_test_file = $this->compile_dir . '/test_file';
            if(($_fp = fopen($_test_file, 'w')) !== false) {
                if(!fwrite($_fp,'test')) {
                    echo "error: unable to write to $_test_file<br />\n";
                } else {
                    fclose($_fp);
                    unlink($_test_file);
                    echo "OK.<br />\n";
                }
            } else {
                echo "error: unable to open $_test_file<br />\n";   
            }
        }
        echo "cache_dir is '" . $this->cache_dir . "'<br />\n";
        echo "real system path: " . realpath($this->cache_dir) . "<br />\n";
        echo "file perms: " . substr(sprintf('%o', fileperms($this->cache_dir)), -4) . "<br />\n";
        if(!file_exists($this->cache_dir)) {
            echo "error: cache_dir '" . $this->cache_dir . "' does not exist.<br />\n";
        } elseif (!is_dir($this->cache_dir)) {
            echo "error: cache_dir '" . $this->cache_dir . "' is not a directory.<br />\n";           
        } elseif (!is_readable($this->cache_dir)) {
            echo "error: cache_dir '" . $this->cache_dir . "' is not readable.<br />\n";           
        } elseif (!is_writable($this->cache_dir)) {
            echo "error: cache_dir '" . $this->cache_dir . "' is not writable.<br />\n";           
        } else {
            $_test_file = $this->cache_dir . '/test_file';
            if(($_fp = fopen($_test_file, 'w')) !== false) {
                if(!fwrite($_fp,'test')) {
                    echo "error: unable to write to $_test_file<br />\n";
                } else {
                    fclose($_fp);
                    unlink($_test_file);
                    echo "OK.<br />\n";
                }
            } else {
                echo "error: unable to open $_test_file<br />\n";   
            }
        }
        echo "testing complete.<br />\n";
    }
}

?>


Last edited by mohrt on Fri May 13, 2011 2:32 pm; edited 4 times in total
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7267
Location: Lincoln Nebraska, USA

PostPosted: Mon Jul 11, 2005 2:21 pm    Post subject: Reply with quote

Other link(s) that may help with permission problems:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5575

[edit] also note, Smarty 3 has a built-in $smarty->testInstall() feature that basically does the above.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> FAQ (Frequently Asked Questions) All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP