1
nachenko
Any Smarty way to check we are in front page of a site?
  • 2007/10/26 9:50

  • nachenko

  • Quite a regular

  • Posts: 356

  • Since: 2005/1/18


Hi!

SOmetime ago I hacked a site to generate a Smarty variable when we were in front page. Now I'd like to do the same thing but without HACKING THE CORE.

Is there a Smarty variable or set of IFs I can use to evaluate we are in front page?

Thank you.

2
script_fu
Re: Any Smarty way to check we are in front page of a site?

I am not sure myself but maybe your answer is here.

If you solve this question could you post the answer?

3
kaotik
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/26 10:22

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


<{ if $xoops_dirname=="system" }>
some html code for home page
<{/if}>


this is one way of doing it

4
rplima2004
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/26 12:32

  • rplima2004

  • Just popping in

  • Posts: 70

  • Since: 2004/10/8


Hi,

With no hack on core files i don't know, but in my sites i made something like this:

in header.php
Quote:

if ($_SERVER['PHP_SELF'] == '/index.php'){
$xoopsTpl->assign( 'isIndex', true );
}else{
$xoopsTpl->assign( 'isIndex', false );
}


in theme.html
Quote:

<{if $isIndex}>
.... some code here .....
<{/if}>


If anyone have a best way to do this, please let us know.

Regards

5
zyspec
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/27 3:02

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


I've done something very similar to rplima2004 but I just added it all to the theme...

Towards the top of the theme add the following:
<{php}>
$myurl strtolower($_SERVER['PHP_SELF']);
if (
$myurl == (DIRECTORY_SEPARATOR.'index.php')) {
  
$this->assign(var='ishome' value=true);
}else{
  
$this->assign(var='ishome' value=false);
}
<{/
php}>


And then when you want to use the code later in the theme you can just do something like:
<{if $ishome}>
  
We are on the home page
<{else}>
  
We are NOT on the home page
<{/if}>


Of course you could put the php code listed above in a Smarty include if you'd like...
EDIT: Modified the code above slightly to allow it to work on either a windows or *nix server...

6
skenow
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/27 3:05

  • skenow

  • Home away from home

  • Posts: 993

  • Since: 2004/11/17


I've used the same method as kaotik with good results - much simpler, too.

7
zyspec
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/27 3:15

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


skenow / kaotik,

I think the code you have listed will give a false positive for any file you execute in the root directory. So for example if you try this on /user.php it will test true even though you're not on /index.php.

8
nachenko
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/27 10:46

  • nachenko

  • Quite a regular

  • Posts: 356

  • Since: 2005/1/18


Sorry but I'm not sure these methods are "all terrain, any weather".

Using PHP_SELF does not consider the script receiving variables, so it would give a false positive in News module when browsing latest news in a category.

XOOPS core itself also has this problem, see this bug:

http://sourceforge.net/tracker/index.php?func=detail&aid=1811479&group_id=41586&atid=430840

The other method, detecting the system module... is it supposed to work when the start module is any other module?

Drupal has a global variable named $is_front. I think we should copy that.

9
nachenko
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/27 11:02

  • nachenko

  • Quite a regular

  • Posts: 356

  • Since: 2005/1/18


Although the problem is fixed for me, I'd check the bug report above and add a line after the IF statement, like this:

define ("IS_FRONT", true);

So we can check it in the theme, like this:

{$smarty.const.IS_FRONT}

10
zyspec
Re: Any Smarty way to check we are in front page of a site?
  • 2007/10/27 17:32

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


Okay... So I thought about it a little more and here's some code that will work - most of the time. Just place this code at the top of your theme (or include it) and then you can reference the <{$ishome}> smarty variable to figure out if you're on the home page.

<{php}>
global 
$xoopsConfig;
$xbase basename(XOOPS_URL);
$myloc $_SERVER['PHP_SELF'];

$xdir strtolower(DIRECTORY_SEPARATOR $xbase);
if (
$xdir != DIRECTORY_SEPARATOR) {
  
$pattern "'/".$xbase."/'";
  
$myloc preg_replace($pattern,'',$myloc);
}
$startloc= (!empty($xoopsConfig ['startpage'])) ? 'modules'.DIRECTORY_SEPARATOR.$xoopsConfig['startpage'].DIRECTORY_SEPARATOR.'index.php' 'index.php' ;
if ( (
$myloc == $startloc) && (empty($_SERVER['argv'])) ) {
 
$this->assign('ishome',true);
}else{
  
$this->assign('ishome',false);
}
<{/
php}>


The code at the beginning, while strange, checks to see if XOOPS is installed in a subdirectory off the site's root. So this will work even if XOOPS is installed in http://yoursite.com/xoops instead of in http://yoursite.com

There's an instance where if you post variables to the module's index page then it will still tell you it's a home page, even though technically it's not the "home" page. The code in the bug report that nachenko referenced above suffers from the same limitation.

EDIT:Fixed DIRECTORY_SEPARATOR for both WIN and *nix - again

Login

Who's Online

135 user(s) are online (57 user(s) are browsing Support Forums)


Members: 0


Guests: 135


more...

Donat-O-Meter

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

Latest GitHub Commits