LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2006, 02:47 PM   #1
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
Globals in PHP and Smarty ($_GET)


Hi,

I decided to learn how to use smarty. I like it so far as the code is so much cleaner. I am creating my personal website as a test.
It is gonna use ADODB+MySQL to store the data. My problem is that I want to use $_GET to get the calue of site variable. Eg.: index.php?site=home
I realized that I can use the $smarty.get.site thing to get that but only in the tpl file and not in the index.php file that displays the tpl.
How would you solve a problem like that to get that data from mysql? I could create functions in the index.php file and then calling them from the tpl file but I am not sure that this is the best solution.
And there is one more problem. The {assign var='site' value=$smarty.get.site} works only for the tpl file and cannot be accessed from other files.
Pls help me out.

PS: by the way.. is there a way to call functions from the template file that is in the index.php file or in another one?

Last edited by vargadanis; 11-29-2006 at 03:07 PM.
 
Old 11-29-2006, 05:41 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
I think you are looking at the problem from the wrong angle. When you use a template, you dont wanna know where your data comes from. You just want to take a template, set it the values it depends on (which can be strings, numbers or an object) and run it. It doesnt matter if it comes from a _GET array or from a database query. Because thats the kind of separation you want.
So if you have a $_GET["site"] value you want to access, you can just set it to your template like

Code:
$myTemplate->->assign('site', $_GET['site']);
What I am not very clear about is the function thing. I know you can build your own smarty tags or smarty functions but I am not sure if I would want to call a function from MY application. Of course it is valid and it can be good I think that I would want to call a function only if it has to do with presentation and no with the logic of my domain. Perhaps if you give me an example, I could say more (of course, its just my subjective opinion hehehehe)

As regard of assigning variables in a template and the accessing them from PHP, I think its a bad idea (perhaps you are mixing the ideas). I first think, why would he want to assing a variable in a template and then accessing from a PHP file? My answer is: he worked some logic in the template and needs it back in his PHP script. If thats the case, then you are not using it properly. The idea of a template is to separate the presentation logic, of the bussiness logic. Perhaps my assumptions were wrong, in that case, my idea is still valid but doesnt apply to your case hahaha
But if I got it wrong, perhaps you can explain me a bit more what you want to do (and why) with your smarty variables and I might be able to help you out

Cheers!

PS: I also like Smarty!
 
Old 11-29-2006, 08:58 PM   #3
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
No, you did an excellent job! I had a mess in my mind. I first taught that this template thing is the same as divide the PHP file into smaller parts. Eg:
There is the huge index.php file with 100 functions and 20 classes and 250 variables. Divide it to classes and functions etc so the index.php file would include these files

Now I think of this a little bit differently. I think I will create a template for each site as it is not that big work esp that I need to modify only 3 lines (about). That is gonna be so simple. I

Let me explain the thing I was struggling with. I wanted to create a smarty version of the following concept:
I have one index.php file that contains everything for all the sites. (Somehow I liked doing the things like that) If you visit another site everything you needed to do is enter this: index.php?site=home

the code looked like this:
Code:
$res = $db->Execute("SELECT * FROM sites WHERE name = '".$_GET['site']."'");
I fetched the result of this ADODB request and I was happy that I do not need to create maybe 10 sites that does the same but I did solve it in 1 file. (Only for small CMS sites)
 
Old 11-30-2006, 02:07 PM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi again,
first, I know but I cant help it, just because you want to use a different configuration by doing index.php?site=home doesnt mean you have to get all your code in just one file. Of course you can do it, but I think it will eventually lead you to madness (which can be a good thing or bad thing I am not sure hehehe).
As regards of writing one template for each site, it could be a good idea. But, since it is a template it could work for ALL the sites because its generic. It depends on how much you want to make each site different. You could parametriced your template so you can use it with all the specific configuration which you picked from the database (so you will still only need the site=home and nothing else).

I think that since it is just a couple of lines you want to differ from one template to another, I would create my own smarty tag (yes you can do it!) which can solve the slighty difference between them (which will be marked as parameter brought from the database).

Of course, its just an idea based on something very generic, but it makes senses to me

Hope this is useful.
Cheers!
 
Old 12-01-2006, 10:33 AM   #5
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
Maybe that is what I need!
I mean the smarty tags. It can be sweet. About the templates. I did completely ignore what are templates good for. This is just a simple CMS so I will not create a template for each site as the look the same, the only difference is the content.
Those smarty tags interest me. How can I create smarty tags? I did not find a proper description on the internet.

I just want to use Smarty as it is intended to be used not in a different way and as I do not know almost anything about it and I am pretty sure I have got all the bad habits in connection with programming I am asking other ppl how would they do it. This the whole point of this topic. (^_^)
 
Old 12-01-2006, 11:09 AM   #6
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Smarty has a plugin architecture, so you might want to start reading that part on the official documentation:

http://smarty.php.net/manual/en/plugins.php

I would recommend you to read it all, but I think you will be needing a "Template Function" or a "Block Function". There are examples in that tutorial so you can start with that to get the grip of it.

In another topic, what is it specificaly that you want to change between sites? (the things that you said it was 3 or 4 lines) Perhaps if you tell us about it we could guide you more specificaly

Cheers!
 
Old 12-01-2006, 07:21 PM   #7
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
thx...

This is what I did not find on my own. (^_^)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Register Globals problem vnb400 Linux - Newbie 1 04-28-2006 09:51 AM
LXer: Smarty templating and presentation library for PHP LXer Syndicated Linux News 0 12-09-2005 05:53 PM
LXer: Smarty templating and presentation library for PHP LXer Syndicated Linux News 0 12-09-2005 02:36 PM
PHP $_GET is getting weird info ghight Programming 2 08-11-2004 12:32 PM
PHP 5.0 Register Globals and Apache 2.0.50 latino Linux - Software 1 07-21-2004 06:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration