5

Very simple question, is it possible to use a smarty var inside the {php}{/php} tags. I know it's deprecated, pointless, not recommended, etc., but please, I am looking for a simple patch !

Something like

{php} 

 $result = mysql_query("SELECT value FROM table WHERE fieldid = 9 AND relid = {MYSMARTYVAR}");

{/php}

Thank you!

Update: I`ve tried the following methods.

1-$var = $this->get_template_vars('smarty_var');
2-$var = $smarty->getTemplateVars('smarty_var');
3-$var = $this->_tpl_vars['smarty_var'];

All give me Fatal errors, $this when not in object context etc. I`m in the WHMCS environment, if this explains why perhaps certain things are blocked or disabled?

Update 2; found a solution more simple than it seemed, I feel silly: $var= "{$mysmartyvar}";

Thanks everyone!

1
  • Why do you need this? I think it'll be better if you call mysql from your php (before calling smarty). Or do you have any special reason? Commented Jan 12, 2012 at 14:08

7 Answers 7

7

Use $this->get_template_vars('smarty_var') to get a Smarty variable.

 $result = mysql_query("SELECT value FROM table WHERE fieldid = '9' AND relid = '" . $this->get_template_vars('smarty_var') . "'");
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Preston, I`ve tried the get_template_vars method, as well as getTemplateVars but in both cases I get Fatal Errors such as using this on a non-object;
Have you tried replacing "$this" with the name you gave your Smarty object in your PHP file?
Cant say that I have, and cant say that Id know whta that even is..how can I tel what the Smarty object is, I dont even know where the PHP file is in relation to the .tpl as I haven't built any of this and would certainly not use smarty if I could avoid it haha
The name would be wherever the smarty view instance is created, using something like $smarty = new Smarty; where $smarty is the object name. If you have no idea where this is, you could try searching (or find/replace) for "new Smarty" through the PHP files if you are using an IDE that allows you to.
I used {debug} and found some references to objects. The smarty variable I'm trying to get is serverDetails.0 , so I changed $this to $serverDetails. The error goes away, but nothing shows up now..am i on the right track with this?
|
2
 {php}
 $var = $template->get_template_vars('...')
 {/php}

Comments

0

Yes, all variables are stored in $this->_tpl_vars, so it should look like this:

{php} 

 $result = mysql_query("SELECT value FROM table WHERE fieldid = 9 AND relid = {$this->_tpl_vars['MYSMARTYVAR']}");

{/php}

1 Comment

Thanks, I get this error however; Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given i
0
$myValue = $template->getVariable('myVariable')->value;

1 Comment

You should explain why.
0

Answer from the OP. It may be helpful to others.

It is very simple.

$var= "{$mysmartyvar}";

This is working for me.

Comments

0

This works for me:

{php}
$variable= $GLOBALS['smarty']->getTemplateVars('variable');
{/php}

Comments

-1

Try using "global"

{php}
global $smarty_object;
$var = $smarty_object->get_template_vars('whatever');
{/php}

1 Comment

Thanks, I get this Fatal error: Call to a member function get_template_vars() on a non-object :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.