1
mboyden
HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2007/12/24 6:22

  • mboyden

  • Moderator

  • Posts: 484

  • Since: 2005/3/9 1


From my searches on the forums, sometimes we all want to access some XOOPS variable(s) that aren't available in our templates and themes. The biggest one I run up against is wanting to use the XOOPS Real Name (I usually call it the Display Name in my implementations) instead of the login (this is actually somewhat good, from a security standpoint). But there may be others, too, that you need, like the ID/Name/Title of the current module and such. After some hacking, I came up with the methodology below. The concept should work in themes as well as templates. I've also included a small example for an enhanced search bar that I've been playing with. Would certainly love to hear any additional suggestions and epiphanies this one generates.

Proof of Concept Code
<{php}>
// to access Xoops, Smarty, Module variables, must define global
global $xoopsUser$xoopsModule$xoopsModuleConfig$smarty;
// to add a Smarty Constant accessible via
define("XOOPS_MODULE_ID",$xoopsModule->getVar('id'));
define("XOOPS_MODULE_NAME",$xoopsModule->getVar('name'));
// to add a Smarty Variable to be accessible later in template
$xoopsTpl->assign('module_name',XOOPS_MODULE_NAME);
<{/
php}>
<{* if global var 
doesn't exist (say $xoopsUser for Anonymous User) then php will fail and page will be blank, so you have to not access unless able to, like this example *}>
<{if $xoops_isuser}>
<{php}>
// Add a Smarty Constant from $xoopsUser (but fails if Anonymous User and $xoopsUser doesn'
t exist)
define("XOOPS_REALNAME",$xoopsUser->getVar('name'));
<{/
php}>
<{/if}>
<
p>Welcome, <{if $smarty.const.XOOPS_REALNAME}><{$smarty.const.XOOPS_REALNAME}><{else}>Guest<{/if}>! The currently displayed module mame is: <strong><{$module_name}></strong> (ID: <{$smarty.const.XOOPS_MODULE_ID}>).</p>


Note: this solution works only for the later releases of XOOPS with the updated Smarty engine. Also for the example above, you must be logged in and on a module page (or the code fails and gives a blank page, see the search example below for more information). The global definition allows access to the existing variables in XOOPS or the active module code. The $smarty variable must be in the globals definition to allow a define to show up in the $smart.const.CONSTANTS. And if you try to access the non-existent $xoopsUser variable as an anonymous user, you'll get a blank page, thus the second set of code for that even though the global is defined above that (I couldn't quickly find a way to test it in PHP without getting a blank page, thus this hack).

Maybe I should look at making some of this stuff available in the core if there is enough desire/need. I somewhat expected to find this information available.

Search Bar Example
I wanted a search bar that allowed me to search either the current module or the entire site. I had found code to accomplish this using the core Search block (system module), but like many other themes I had seen, I wanted it in a Search Bar in the header, thus that solution didn't work.

I placed this code near the top of theme.html:
<{php}>
global 
$xoopsUser$xoopsModule$smarty;
<{/
php}>
<{if 
$xoops_dirname != 'system'}>
<{
php}>
define("MODULE_ID",  $xoopsModule->getVar('mid'));
define("MODULE_NAME",$xoopsModule->getVar('name'));
if (
$xoopsModule->getVar('hassearch')) {
    
define("MODULE_HASSEARCH",true);
} else {
    
define("MODULE_HASSEARCH",false);
}
<{/
php}>
<{/if}>
<{if 
$xoops_isuser}>
<{
php}>
define("XOOPS_REALNAME",$xoopsUser->getVar('name'));
<{/
php}>
<{/if}>

Then, in my searchbar theme code, I used this (all display via CSS):
<div id="xo-searchbar">
<
form name="searchbar" method="post" action="<{xoAppUrl /search.php}>" title="<{$smarty.const.THEME_DESC_SEARCH}>"><input type="text" id="query" name="query" class="keyword" value="<{$smarty.const.THEME_KEYWORDS}>" onfocus="this.value=(this.value=='<{$smarty.const.THEME_KEYWORDS}>')? '' : this.value ;" maxlength="255" /><input type="hidden" name="action" id="action" value="results" />
<
input type="image" title="Search Entire Site" src="<{xoImgUrl /img/nav/search-global.gif}>" onclick="javascript:for(i=0;t=document.searchbar.elements[i++];t.checked=false);" /><{if $xoops_dirname != 'system' && $smarty.const.MODULE_HASSEARCH == true}><input type="checkbox" name="mids[]" value="<{$smarty.const.MODULE_ID}>" style="display:none;" /><input type="image" title="Search <{$smarty.const.MODULE_NAME}> ONLY" src="<{xoImgUrl /img/nav/search-module.gif}>" onclick="javascript:for(i=0;t=document.searchbar.elements[i++];t.checked=true);" /><{/if}></form>
</
div>


[Note: this could also be extended fairly easily to allow a user to search a defined web search engine, and/or maybe even allow them to pick which search engine, done creatively. Also, this search capability requires Javascript; otherwise it won't function properly making both buttons search the entire site (does that count as failing gracefully?).]

I'll note that I looked around for how to do some of this and couldn't find it, although it could exist on the on-long-hiatus dev site that I used to contribute to (and instead now post on X.o and on my personal site to keep for future reference). Anyhoos, I hope it's helpful. Comments appreciated.
Pessimists see difficulty in opportunity; Optimists see opportunity in difficulty. --W Churchill

XOOPS: Latest | Debug | Hosting and Web Development

2
hackbrill
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2007/12/24 13:50

  • hackbrill

  • Friend of XOOPS

  • Posts: 283

  • Since: 2005/7/14


Thanks for taking the time to post this; your ideas are very enlightening.

3
maxxy
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2007/12/24 16:21

  • maxxy

  • Quite a regular

  • Posts: 286

  • Since: 2007/6/11


is there any available demo ?

4
trabis
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2007/12/24 18:51

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


It´s very good to know how to use php inside a smarty template but I miss the point.
You should do those DEFINE´s inside of common.php or something to get <{$smarty.const.DEFINES}> availiable in all templates.

Thanks!

5
script_fu
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates

Thats pretty neat! Thanks

6
mboyden
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2007/12/27 16:30

  • mboyden

  • Moderator

  • Posts: 484

  • Since: 2005/3/9 1


Quote:
by maxxy on 2007/12/24 10:21:30
is there any available demo?

Not yet, but I will release this soon on a site I'm building along with the theme itself so everyone may have it to use. I went ahead and published the hack in case I get sidetracked and can't provide it right away (as there is still a bit of clean-up and coding to complete for the theme part, but this part was finished).

Quote:
by trabis on 2007/12/24 12:51:49
You should do those DEFINE´s inside of common.php or something to get <{$smarty.const.DEFINES}> available in all templates.

Agreed that some of these common things should be available. However, I tend to avoid hacking the core because it makes upgrades and updates of XOOPS core much harder to deal with. My philosophy is KISS (keep it simple, stupid), so that's why it's in the theme for now. My thought about sharing was for those that had a similar philosophy, and also as a hack, I like to share things I've learned as I take other people's hacks and take them further as well and hope others will do the same with mine.
Pessimists see difficulty in opportunity; Optimists see opportunity in difficulty. --W Churchill

XOOPS: Latest | Debug | Hosting and Web Development

7
trabis
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2007/12/27 19:14

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


I tend to avoid hacking the core because it makes upgrades and updates of XOOPS core much harder to deal with.


I just thought you were a "core developer", that's why I said it. I did not mean 'hack it', I mean 'core it'.


8
mboyden
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2008/1/10 18:32

  • mboyden

  • Moderator

  • Posts: 484

  • Since: 2005/3/9 1


Good suggestions/feedback. I will look into the issue of extending some of this information into the core and into the default XOOPS variables passed into Smarty.

However, in the interim since we're working on the merge, here's another update. Based on some additional work, I found how to do this with less code making it more accessible and less prone to failure. And, even if we pass more to Smarty, there may still be some variables that you want access to (say in a module template) and you don't want to hack the core or the module to get to them, then this methodology should work for providing access to module data via your templates instead of hacking the module. Again, I hope it helps.

In this updated Proof-of-Concept example below, note the removal of the globals declaration and accessing via the $GLOBALS variable instead. In this manner, too, for whatever reason, you don't need the Smarty global declaration either. So, now the proof of concept code becomes:
<{php}>
// to make accessible via Smarty Constants
define("XOOPS_MODULE_ID",$GLOBALS['xoopsModule']->getVar('id'));
define("XOOPS_MODULE_NAME",$GLOBALS['xoopsModule']->getVar('name'));
// to add a Smarty Variable to be accessible later in template
$GLOBALS['xoopsTpl']->assign('module_name',XOOPS_MODULE_NAME);
<{/
php}>
<{* 
NoteYou will get a blank page if you try to access a variable that doesn't actually exist. In this example, we check first or the page would fail if $xoopsUser doesn't exist (Anonymous User), thus this example*}>
<{if 
$xoops_isuser}>
<{
php}>
// Add a Smarty Constant from $xoopsUser
define("XOOPS_REALNAME",$GLOBALS['xoopsUser']->getVar('name'));
<{/
php}>
<{/if}>
<
p>Welcome, <{if $smarty.const.XOOPS_REALNAME}><{$smarty.const.XOOPS_REALNAME}><{else}>Guest<{/if}>! The currently displayed module name is: <strong><{$module_name}></strong> (ID: <{$smarty.const.XOOPS_MODULE_ID}>).</p>

I'll leave it as an exercise for the reader to modify the Module Search Code previously published. Good luck and have fun with it.
Pessimists see difficulty in opportunity; Optimists see opportunity in difficulty. --W Churchill

XOOPS: Latest | Debug | Hosting and Web Development

9
Will_H
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2008/1/10 21:47

  • Will_H

  • Friend of XOOPS

  • Posts: 1786

  • Since: 2004/10/10


Seems like this could also be used to display breadcrumbs. Well, its a good start at it

Cool share.

10
etcetera
Re: HOWTO: Accessing (Missing) PHP Variables in Theme/Smarty Templates
  • 2009/6/30 18:56

  • etcetera

  • Just popping in

  • Posts: 23

  • Since: 2009/6/13


I have a simple question.

Could someone demonstrate for me how I could take the above code and use it towards custom user profile variables (/fields)?

For example, if I were to add the custom field "gender", how would I alter the above code to read "Welcome, (real name), you are a female."?


Edit: This was not quite clear. I would like to show the information for the user whose profile you are currently viewing, not for the user who is signed in. I.e., if I added custom fields for "gender" and "emotion", I would like to be able to display, for each user, "(Username) is a female. She is feeling happy."

How might I go about doing this?

Login

Who's Online

178 user(s) are online (99 user(s) are browsing Support Forums)


Members: 0


Guests: 178


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits