Page 1 of 1

how to use smarty variable inside {php} tags

Posted: Thu Jan 22, 2009 2:46 pm
by equilerex
there is most likely some thread with the solution but ive been searching for it for the last 2h... starting to tear my hair already...


i want to do something really simple... use smarty variable inside {php} tags... atm it will only enter empty space... while out of the php tags, it works as a charm.

the code in simplified format::

Code: Select all

{php}
    if (curPageURL()=="http://site.com/page")
       {echo "<img src=\"images/{$lang}/navig1_05.gif\" />";} 
    else
       {echo "<img src=\"images/{$lang}/navig2_05.gif\" />";}
{/php}
with the

Code: Select all

{$lang}
being the smarty variable (i know, its actually from cmsms mle but the theory is the same)

anyone got a solution to make it work?
its most likely a rather simple thing but im a novice at php and smarty...

Re: how to use smarty variable inside {php} tags

Posted: Thu Jan 22, 2009 9:15 pm
by steph_30
Hello,

When you are inside {php} ... {/php}, you are in the smarty environment. It means that you can access the smarty object with $this.

If the lang parameter has been declared in smarty, you should access it with $this->lang.

Try and teel us

Good luck

Re: how to use smarty variable inside {php} tags

Posted: Thu Jan 22, 2009 10:51 pm
by equilerex
sorry,  i am unfamiliar with "$this"
i am guessing its some special smarty variable that i am supposed to extract "lang" from... the best i could come up with was
{$new == $this['lang'];}

unfortunately, whenever i use $this in any form it gives me an error: "Fatal error: Cannot use object of type Smarty_CMS as array in....." im guessing what i tried was completely wrong...

as, i said, i am very new to php.. i mostly know what i want it to do and then find the specific code through google,  i would really appreciate some help... perhaps a bit more specific.... i know ive run across $this variable in the morning when i was trying on my own but it impossible to find it anymore with this common word.... its also the last thing i need to get working with this damn site... after thats its just editing the content a bit ^^

and thanks for the reply steph!

Re: how to use smarty variable inside {php} tags

Posted: Fri Jan 23, 2009 8:09 am
by steph_30
Hi equilerex,

$this is not a smarty variable but a php one.

It's an object's notation : you are inside an object and $this is the object.
For your need, $this is the smarty object. It is not an array.

To access the smarty object attribute, you need to do $this->lang and not $this['lang']


And at least, I'm not sure of your need, but if you want to create a code that dynamically print the country flag of the user, you should probably use the user tag instead of {php} ... {/php}. So, in your pages, you will only have to insert your tag like {user_country_flag}.
The code in a user tag is the same as the one between {php} ... {/php}, but if you missed a flag, or a country, you will have only one place to modify (see the custom_copyright for example).

Hope it will help

Re: how to use smarty variable inside {php} tags

Posted: Mon Oct 05, 2009 6:52 am
by sivanantham
Hi
i think this will help you

{php} global $lang,$objSmarty;
$objSmarty->assign("lang",$lang);
{/php}