Re: [SMARTY] global vars in a php block
| From: | Monte Ohrt | Date: | Tue, 16 Dec 2003 20:34:27 +0000 |
| Subject: | Re: [SMARTY] global vars in a php block | ||
| References: | 1 2 3 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-10708@lists.php.net to get a copy of this message | ||
Another option for you, the $_GET vars are already available from within
Smarty like so:
{$smarty.get.FOO}
So your example might be something along these lines:
<html header>
{include file=$smarty.get.f}
<html footer>
Here is a fairly clean way to handle it... don't pass GET vars directly
to include for security, thus making GET vars a shorthand name to the
actual template path.
So, the URL looks like:
http://www.foobar.com/index.php
http://www.foobar.com/index.php?page=foobar1
http://www.foobar.com/index.php?page=foobar2
template looks like:
<html header>
{if $smarty.get.page eq "foobar1"}
{include file="pages/foobar1.tpl"}
{elseif $smarty.get.page eq "foobar2"}
{include file="pages/foobar2.tpl"}
{else}
{* default page *}
{include file="main.tpl"}
{/if}
<html footer>
On Tue, 2003-12-16 at 12:39, Luca Borrione wrote:
> Jason Hubble ha scritto:
>
> > I assume you have a good reason for doing this and the more traditional way of
> > doing this wouldn't work. If that is the case, here is something to try:
> >
> > {php}
> > global $MYVAR;
> > echo "Printing from php<br>";
> > echo "\$MYVAR: $MYVAR";
> > {/php}
> >
> > The way of doing this that would make more since to me would be like this:
> >
> > $MYVAR = "test";
> > $smarty = new Smarty;
> > $smarty->assign('MYVAR',$MYVAR);
> > $smarty->display('index.tpl');
> >
> >
> > Having a index.tpl like this:
> > Print from php<br>
> > $MYVAR: {$MYVAR}
>
> Hi Jason,
>
> thank you for your suggestion, which is perfect but I cannot use it for
> my purposes.
> I just simplified too much, asking how to reach global vars from a php
> block.
>
> My prob is that I use only one php *index.php* which display a smarty
> template
> require_once "common.inc";
> $smarty = new Smarty;
> $smarty->display('index.tpl');
>
> This one will build the main part of every pages, which will be
> different only for an include. e.g.
> <html header>
> {include file="$myFile"}
> <html footer>
>
> The value of $myFile will be different compared to params coming from
> the query part used why flashing index.php
>
> e.g. index.php?f=first will include first.inc
> index.php?f=second will include second.inc
>
>
> Now...
> I use a conf file [e.g. common.inc] in which I set all the general
> global php vars I need, for example db params.
>
> I already include this file in *index.php* because I need it just to
> assign some vars to smarty.
>
>
>
> The point is that...
> the content of every inclusion will be different obviously, and
> sometimes I need to use a {php} block, for example to read a db table
> or to include a whole php file which could use any of the global vars
> contained in common.inc
>
> As far as I can see I have only 2 ways:
> 1. require common.inc anytime I need in a php block or php file to be
> included in the main template.
>
> 2. use extract($GLOBALS) in place of the previous require to make any
> global vars reachable.
>
>
> I was just wondering if there's something wiser, something like a
> boolean smarty param similar to $request_use_auto_globals.
>
>
>
> Hope it makes sense
> Luca