|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
stevenv Smarty n00b
Joined: 29 May 2003 Posts: 2
|
Posted: Thu May 29, 2003 5:13 am Post subject: include_php & multiple includes -> undefined var |
|
|
Sorry, for the mostly meaningless subject, but I can't explain the problem in so little space. In fact, I can hardly explain it at all, so lets just take a look at some code. The application I found this in is actually much larger, but I managed to reduce it to what follows. I moved all the files into the same directory to make it a bit clearer:
Code: |
$ cat index.php
<?php
error_reporting(E_ALL);
require_once('config.php');
require_once('Smarty.class.php');
$page = new Smarty;
$page->template_dir = '.';
$page->compile_dir = '.';
$page->config_dir = '.';
$page->cache_dir = '.';
$page -> assign('test', 'test');
$page -> display('index.tpl');
?>
$ cat config.php
<?php
$var2 = "config\n";
?>
$ cat index.tpl
{$test}
{include file="footer.tpl"}
$ cat footer.tpl
{include_php file="random.php"}
{$var2}
$ cat random.php
<?php
error_reporting(E_ALL);
require_once('config.php'); /* same config.php as included in index.php */
$this -> assign('var2', $var2);
?>
|
Here is the output:
Code: |
$ php index.php
test
Notice: Undefined variable: var2 in /home/steven/public_html/tests/smarty_tests/random.php on line 4
|
But, when we remove the require_once call in index.php, it works as expected:
Code: |
$ php index.php
test
config
|
I think that well demonstrates the problem I am having, however, it probably seems way over complicated in its current form, so let me explain it in the context my application uses it:
/home/me/templates - my general templates
/home/me/templates/includes - common templates (header, footer)
/home/me/includes - my libraries/configuration files
/home/me/public_html/ - my files to be publicly accessed
Now, I want to be able to display a random quote on the bottom of every page. I was hoping to get away with *not* having to assign that template variable on every single page (this is exactly what the include_php manual page says it is useful for). So, I have something like this, which is more or less a replica of the above footer.tpl:
Code: |
/home/me/templates/includes/footer.tpl :
{include_php file="random_quote.php"}
<blockquote cite="{$quote.author}">
<p>{$quote.quote}</p>
</blockquote>
|
Now, random_quote.php needs to access the same library files which, say, index.php in /home/me/public_html needs to access, therefore, they both contain identical require_once() calls. This is pretty much exactly like the {include_php} example in the smarty documentation, so I don't understand why it is not working for me.
Also, removing the require_once() call in random.php, and relying on the one set in /home/me/public_html/index.php does not work either - same undefined variable notice.
Any help is greatly appreciated, I'm really lost here. If you need any further clarification - just ask. Also, I am not particularly set on this solution. If you know a better way to achieve my overall desire - having a random quote on every page without having to assign it on every single page - feel free to share that as well.
TIA
[edit: fixed formatting a little] |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu May 29, 2003 6:00 am Post subject: |
|
|
hmm, require_once() includes the file just *once* as the name may suggest. use require() if you want to include the same file multiple times. |
|
Back to top |
|
stevenv Smarty n00b
Joined: 29 May 2003 Posts: 2
|
Posted: Thu May 29, 2003 12:53 pm Post subject: |
|
|
D'oh.
I was under the impression that it would be pointless to require the same file multiple times. I suppose it has something to do with scoping issues. (I could have sworn I tried that though, but it's hard to keep track of these things.)
Thanks |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu May 29, 2003 3:33 pm Post subject: |
|
|
yes, this could also be seen as a scoping issue. instead of re-requiring the file, you could also say
"global $var2;" in your random.php. the index.php has global scope, where files included by include_php have the scope of a smarty-method. this means all global variables are not visible by default to the included php, but have to be declared "global" to be visible.
HTH
messju |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot 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
|