View previous topic :: View next topic |
Author |
Message |
ricklach Smarty Regular
Joined: 27 Aug 2004 Posts: 39
|
Posted: Wed Nov 17, 2004 6:58 pm Post subject: $compile_dir ... does not exist |
|
|
I am getting the following error for the first time: Smarty error: the $compile_dir 'templates_c' does not exist, or is not a directory.
Of course it is a directory because I have many compiled templates in it already. The only thing that is new is the template. Here is the code I am trying to put together for testing (only partially completed):
<?php
require('gpbc/setup.php'); //setup file with DB and Smarty setup info
require_once "globals.php";
$post_vars = StripSlashArray ($_POST);
if (!is_array($post_vars)) {
$post_vars = StripSlashArray ($_GET);
}
class donation {
function action ($post_vars) {
$return_str = "";
switch ($post_vars['action']) {
default:
case "Form":
$return_str .= $this->Form ($post_vars);
break;
case "Add":
$return_str .= $this->Add ($post_vars);
$return_str .= $this->ViewOne ($post_vars);
break;
case "Edit":
$return_str .= $this->Update ($post_vars);
$return_str .= $this->ViewOne ($post_vars);
break;
case "View":
$return_str .= $this->View ($post_vars);
break;
}
return $return_str;
}
//-----------------------------------------
function Form ($post_vars, $error=NULL) {
$smarty = new Smarty ();
$smarty->assign ($post_vars);
if ($error != NULL)
$smarty->assign ("error", $error);
return $smarty->fetch ("form.tpl");
}
}
$donation = new donation ();
$body = $donation->action ($post_vars);
$smarty = new smarty ();
$smarty->assign ("body", $body);
$smarty->display ("donation.tpl");
?>
And here is the template:
{if $error != ""}
<p><span style="color:red"> {$error} </span></p>
{/if}
<form method = "POST" action = "">
<br />First Name: <input type="text" name="first" value="{$first}">
<br />Last Name: <input type="text" name="last" value="{$last}"><br />
<input type="Hidden" name="name_id" value="{$name_id}">
{if $action == "Form" && $name_id != ""}
<input type="Submit" name="action" value="Duplicate">
<input type="Submit" name="action" value="Update">
{else}
<input type="Submit" name="action" value="Add">
{/if}
</form>
My guess is that the template has not been compiled yet but I am a smarty neophyte and learn with each passing error. Any idea what is wrong?
Rick |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Nov 17, 2004 7:01 pm Post subject: |
|
|
If the value of $compile_dir is just 'templates_c', my guess is that the path to the directory is not working. Try using an absolute value such as '/path/to/templates_c' |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 17, 2004 7:32 pm Post subject: |
|
|
Here's a tip: only create one Smarty object instance and setup that instance according to your environment/paths. You would then reference that one and only instance whenever you need to access Smarty and thereby ensure that you are always going to use a properly setup instance.
Here is an example using global var access. I actually suggest using a singleton, but this is fine to give you the idea.
[php:1:f7e28da11b]<?php
// ensure a global
$GLOBALS['smarty'] =& new MyExtendedSmarty();
// example access
function foo() {
global $smarty;
$smarty->display('foo.tpl');
}
?>[/php:1:f7e28da11b] |
|
Back to top |
|
ricklach Smarty Regular
Joined: 27 Aug 2004 Posts: 39
|
Posted: Wed Nov 17, 2004 8:02 pm Post subject: |
|
|
Monte,
I am using a full path name in my script. Been burned by that in the past and learned my lesson.
Boots,
What do you mean by "singleton" in your comment? Why not use a GLOBAL var if it is going to be used in virtually every page?
Rick |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 17, 2004 9:03 pm Post subject: |
|
|
ricklach wrote: | Boots,
What do you mean by "singleton" in your comment? Why not use a GLOBAL var if it is going to be used in virtually every page? |
Sure, you can use globals but it means a few dependancies that are hard to resolve. For example, your functions will have to assume that the global variable has a specific name and that it is properly initialized prior to being accessed via the function. It also increases the risk that 3rd party code (which may also use globals) will collide with your private code. There is also some snobbery about proper coding styles which suggest not using globals at all (for example, Java where they are not even possible) for the reasons above and also due to some compiler considerations in some languages. In interpreted PHP it is not too much of a big deal if you are willing to live with the coding style issues.
A singleton is a slighlty more advanced concept and relates to an object pattern that enforces a single instance of a class. ie: it gaurantees that whenever you request an instance of a specific class that only one instance is ever created; once created, all other requests for an instance of the class return a reference to the existing instance.
The following demonstrates the above example but with a singlton derived from a class that I use for such purposes.
[php:1:b5f39b4000]<?php
function foo() {
$smarty =& LAF_RT::Singleton('Render::LocalSmarty');
$smarty->display('foo.tpl');
}
?>[/php:1:b5f39b4000]
It is a subtle difference. I have to include my "LAF_RT" class (which is very small) before this function is defined -- but I do that in my prepend.php so it is gauranteed to be available. The LAF_RT::Singleton() method call returns an object instance for the class "LocalSmarty" which is my custom extended Smarty class and it also ensures that once an instance is created, only references to that existing instance are returned. Note, though, that I don't have to pre-initialize the Smarty var or even load the Smarty class -- that only happens if it is actually used by code that is executed.
To summarize: globals are easy to implement, singletons offer better overall control. |
|
Back to top |
|
ricklach Smarty Regular
Joined: 27 Aug 2004 Posts: 39
|
Posted: Thu Nov 18, 2004 4:50 pm Post subject: |
|
|
Gentlemen,
As always, you advice has proved invaluable. My homework now is to read-up on singletons and try to implement that feature next. I solved all my other "file" problems with your suggestions and as always close attention to detail.
Rick |
|
Back to top |
|
damon1977@hotmail.com Smarty n00b
Joined: 16 Nov 2009 Posts: 1
|
Posted: Mon Nov 16, 2009 3:15 pm Post subject: |
|
|
I doubt that you get this ricklach since your post was from 2004 but on the slight chance you might I have the following question. I have very similar code to what you had but at the opening of the page and anytime when moving through the form I get a the following error "Undefined variable: new_array"
from my main php
$post_vars = StripSlashArray ($_POST);
if (!is_array($post_vars)) {
$post_vars = StripSlashArray ($_GET);
}
class savelinks extends db_module {
function savelinks () {
}
function action ($post_vars) {
$return_str = "";
switch ($post_vars['action']) {
default:
case "Form":
$return_str .= $this->Form ($post_vars);
break;
case "Duplicate":
case "Add":
$return_str .= $this->Add ($post_vars);
$return_str .= $this->ViewOne ($post_vars);
break;
etc...etc.
the StripSlashArray sits in a file globals.php with the following code
function StripSlashArray ($array)
{
array ($new_array);
foreach ($array as $key => $value) {
$new_array [$key] = stripslashes ($value);
}
return $new_array;
}
The form works as it lets me add links, udate, delete them etc but I'm not sure why I'm getting the undefined variable. Any help is appreciated. Thanks |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Nov 17, 2009 8:51 pm Post subject: |
|
|
function StripSlashArray ($array)
{
$new_array = array();
foreach ($array as $key => $value) {
$new_array [$key] = stripslashes ($value);
}
return $new_array;
} |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Nov 17, 2009 10:54 pm Post subject: |
|
|
U.Tews wrote: | function StripSlashArray ($array)
{
$new_array = array();
foreach ($array as $key => $value) {
$new_array [$key] = stripslashes ($value);
}
return $new_array;
} |
Or just:
Code: | array_walk($myarray,'stripslashes'); |
|
|
Back to top |
|
|