how to use smarty variable inside {php} tags

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
equilerex
New Member
New Member
Posts: 4
Joined: Sat Aug 23, 2008 11:51 am

how to use smarty variable inside {php} tags

Post 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...
Last edited by equilerex on Thu Jan 22, 2009 5:51 pm, edited 1 time in total.
steph_30
Forum Members
Forum Members
Posts: 22
Joined: Mon Apr 28, 2008 11:38 pm

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

Post 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
Last edited by steph_30 on Thu Jan 22, 2009 9:25 pm, edited 1 time in total.
equilerex
New Member
New Member
Posts: 4
Joined: Sat Aug 23, 2008 11:51 am

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

Post 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!
steph_30
Forum Members
Forum Members
Posts: 22
Joined: Mon Apr 28, 2008 11:38 pm

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

Post 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
sivanantham

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

Post by sivanantham »

Hi
i think this will help you

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

Return to “CMSMS Core”