pea Posted December 10, 2008 Share Posted December 10, 2008 I'm installing the smarty templating system. I'm not familiar with classes and i'm stuck at something. The configuration file has a class with variables for you to assign directory paths to, the problem i have is that i need to assign a variable to one. I'm getting the current template with an sql query. Then i'm doing this: var $template_dir = "templates/$template"; I get the error: syntax error, unexpected '"' I've read and read but i still don't understand classes Pastebin: http://pastebin.com/m6060ab95 Edit: Sorry, this forum posted this twice for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/ Share on other sites More sharing options...
keiran420 Posted December 10, 2008 Share Posted December 10, 2008 I'm installing the smarty templating system. I'm not familiar with classes and i'm stuck at something. The configuration file has a class with variables for you to assign directory paths to, the problem i have is that i need to assign a variable to one. I'm getting the current template with an sql query. Then i'm doing this: var $template_dir = "templates/$template"; I get the error: syntax error, unexpected '"' I've read and read but i still don't understand classes Pastebin: http://pastebin.com/m6060ab95 Edit: Sorry, this forum posted this twice for some reason. I cant see a problem on that 1 line, and i am not reading through 2000 lines in your link... But have you tried doing it a differant way, at times i find things fail 1 way, yet work randomly another... try var $template_dir = "templates/".$template; Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711321 Share on other sites More sharing options...
pea Posted December 10, 2008 Author Share Posted December 10, 2008 I've tried that before - same result. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711332 Share on other sites More sharing options...
keiran420 Posted December 10, 2008 Share Posted December 10, 2008 I've tried that before - same result. That line is not the problem then... Dont listen to much to line error reports.... Depending on what the error is, it may get that totally wrong... I bet if you read every line of your code, you would find atleast 1 error... Thats your job to read it, not anyone elses, try adding some more error handling reports in, once you have identified the error, things will be hella easyer... Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711340 Share on other sites More sharing options...
pea Posted December 10, 2008 Author Share Posted December 10, 2008 The only part that's my code is the part that gets the template name from the database. I know this is working. The only line that i've changed is the line it's complaining about. If i don't change anything the script works fine. So i can assume it's that one line that's at fault. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711345 Share on other sites More sharing options...
keiran420 Posted December 10, 2008 Share Posted December 10, 2008 I've tried that before - same result. That line is not the problem then... Dont listen to much to line error reports.... Depending on what the error is, it may get that totally wrong... I bet if you read every line of your code, you would find atleast 1 error... Thats your job to read it, not anyone elses, try adding some more error handling reports in, once you have identified the error, things will be hella easyer... Ok 2 things, 1 i never ever used 'var', and 2, echo out the value of '$template' before using it, see if it is what you think it is... Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711346 Share on other sites More sharing options...
RichardRotterdam Posted December 10, 2008 Share Posted December 10, 2008 Youre trying to edit Smarty.class.php. The Smarty class is fine as it is you can change the value like so. //instantiate object $smarty=new Smarty(); //change the value $smarty->template_dir="what_ever_you_want"; I recommend not changing this class unless you know what youre doing. Instead read how to use the smarty template engine. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711363 Share on other sites More sharing options...
pea Posted December 10, 2008 Author Share Posted December 10, 2008 Edit: i know how to bloody change variables, and if you looked at what i'm trying to do you'd see what you are asking isn't possible. I think it's a class specific thing. Some people have told me i need to use something called a 'construct', not that i can get this construct to work. I added this just below class Smarty { : function __construct () { db_connect($dbhost, $dbuser, $dbpass, $dbname); $table = $db_label."_templates"; $result = db_query("SELECT * FROM $table WHERE active = '1'"); while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $template = $row['folder']; } } Still gives me the same result though. I think i understand how you're seeing it. $variable = "this".$that; works normally, but not in classes apparently. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711366 Share on other sites More sharing options...
RichardRotterdam Posted December 10, 2008 Share Posted December 10, 2008 smarty is written in php 4 syntax so that means function Smarty(){ } is the constructor. If a class is all php 4 syntax it's tidier to keep it php 4 and not put php 5 syntax in it you're putting in two constructors. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-711369 Share on other sites More sharing options...
pea Posted December 11, 2008 Author Share Posted December 11, 2008 So what do you advise i do to get the variable in that string? Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-712559 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 <?php include(smartyclasshere); $smarty = new Smarty; $smarty->template_dir = "templates/$template"; ?> That should get you the desired results. EDIT: decided to explain. Once a class has been instantiated and it has a var of $x then you can assign any variable to $x by calling instantiated class (in this case $smarty) using the ->x. Using that you can read the variable or set the variable. The variable is a "property" of the smarty class with read/write access to it. So if smarty had a var $x defined in the class, this would be the call to change it: <?php include(smartyclasshere); $smarty = new Smarty; $smarty->x = "bob"; echo $smarty->x; // should print bob (if $x was defined in the class chances are it wasnt but yea this is just demonstration purposes. ?> EDIT EDIT: Seeing that you are attempting to modify the class for no reason, I decided to help you understand something. I would not change the smarty class period, it works fine as it is as long as you invoke the right methods. I would revert back to the original version and use the above code to change the directory, as that is essentially what you want to do right? If you do not want to change the directories (which I gathered is all you wanted to do from the initial post) then please restate your question exactly and clearly. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-712565 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Alright instead of doing a third edit I decided to post a possible answer for the young tyke, since he does not understand that smarty offered a way to set the template_directory without having to modify the original code. # define('SMARTY_PHP_PASSTHRU', 0); # define('SMARTY_PHP_QUOTE', 1); # define('SMARTY_PHP_REMOVE', 2); # define('SMARTY_PHP_ALLOW', 3); define('SMARTY_MY_TEMPLATE', $template); # # /** # * @package Smarty # */ # class Smarty # { # /**#@+ # * Smarty Configuration Section # */ # # /** # * The name of the directory where templates are located. # * # * @var string # */ # var $template_dir = "templates/" . SMARTY_MY_TEMPLATE; # That would solve your problem. But take my advice above and use the proper methods, cause if smarty comes out with a huge vunerability, you have to modify all that code again and remember what you did. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-712581 Share on other sites More sharing options...
pea Posted December 11, 2008 Author Share Posted December 11, 2008 I see, but wouldn't i put that in the smarty class? Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-712767 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 The point I am trying to get acrossed is that you do not need to modify the smarty class. By using the example below you are setting the template_dir without changing the smarty class period. <?php include(smartyclasshere); $template = "my/template/dir"; $smarty = new Smarty; $smarty->template_dir = "templates/$template"; ?> Will set the template_dir to "templates/my/template/dir" without doing any modification to the class code of smarty. The code I posted above with smarty, would be to show you the way you "wanted" to do it. But as I stated before it is uneseccary to do that since smarty offers the solution I just posted above $smarty->template_dir to dynamically set the template_dir to what you want it to be. Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-712778 Share on other sites More sharing options...
pea Posted December 11, 2008 Author Share Posted December 11, 2008 Ok I'll give it a go. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-712783 Share on other sites More sharing options...
pea Posted December 12, 2008 Author Share Posted December 12, 2008 It's working perfectly. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/136345-solved-class-problem/#findComment-713507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.