Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

using get vars in include statements

 
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 -> General
View previous topic :: View next topic  
Author Message
jet-jock
Smarty Regular


Joined: 11 Sep 2003
Posts: 48
Location: Connecticut, USA

PostPosted: Tue Sep 16, 2003 4:07 am    Post subject: using get vars in include statements Reply with quote

I did try to research this in the docs and faq, but couldn't find an answer...

I have a PHP-based (Linux) website now that is basically one index.php page that pulls in include files (PHP or HTML) depending on the value of a get variable, 'page'. I'd like to use this same style in a Smarty website, if it makes sense to do so.

I have set my test site up using the 'Basic Installation' procedure from the docs...the example works fine 'Hello, Ned'.

My index.php file is unchanged from the 'Hello, Ned' example (modified for my file system).

My index.tpl is as follows:

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
    <head>
        <title>{$pgTitle}</title>
    </head>
    <body bgcolor="{$bgColor}">
        {include file="$smarty.get.page.tpl"}
    </body>
</html>


The page loads fine if I delete the {include} line (the other 2 vars function properly). Smarty is choking on that file reference, though:

"syntax error: $smarty is an unknown reference"

I've tried all variations of comma placement (including none at all). If I change that line to:

{$smarty.get.page}

I get the value of the variable printed on the page, as expected.

What am I missing here?


Last edited by jet-jock on Sun Sep 28, 2003 11:39 am; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Sep 16, 2003 4:33 am    Post subject: Reply with quote

There is an ambiguity with the '.tpl' portion of the variable. This will work:

{include file="`$smarty.get.page`.tpl"}

That's an ugly syntax to get that working... but Smarty will let you do it. A better way to approach this is to assign the value to the template first:

$smarty->assign('page_tpl', $_GET['page'] . '.tpl');

Then use it in the template like so:

{include file=$page_tpl}

This accomplishes two things. First, cleaner syntax in the template. Second, the application is dealing with super globals, not the template. This is a better application/presentation separation, IMHO.
Back to top
View user's profile Send private message Visit poster's website
jet-jock
Smarty Regular


Joined: 11 Sep 2003
Posts: 48
Location: Connecticut, USA

PostPosted: Sun Sep 28, 2003 11:38 am    Post subject: Reply with quote

Thanks, morht!

I like that second syntax, too. It has the added benefit of allowing authorization checks prior to displaying the requested page.
Back to top
View user's profile Send private message
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 -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP