(first posted: Jun 20, 2006)
(1313 Reads)
keywords: articles cache xarpages
Permalink
Testing which page you're on in a template
What URL am I on?
Just to start this disucssion, you can get the current URL you're on with $url = xarServerGetCurrentURL()
For more on this, and accessing URL parameters, see Vaporbase article "Accessing URL details".
What module and function am I in?
Sometimes I just want to know which module you're in. You can use the xarModGetName() function.
For example, when base is the default module
<xar:if condition="xarModGetName() eq 'base'"> ... </xar:if>
This especially works if you only have user-main.xt in base. (which i always do because if i need other standalone pages i use xarpages and alias them).
More generally, you can get the URL info with $info = xarRequestGetInfo() which returns the current module, fn type, and function
[0 ] = module name ('articles')
[1] = type ('user', 'admin')
[2] = function ('view')
<xar:if condition="$info[0] eq 'articles' and $info[1] eq 'view'"> ... </xar:if>
Which Xarpage am I on?
Of course, if you're in the Xarpages module space, you already have the $current_page variable, so you can just do
<xar:if condition="$current_page.name eq 'about'"> ... </xar:if>
But this won't help if you're in pages/default.xt or some other template that does not have $current_page.
Some modules will cache details of the current page, for use by blocks. But we can use the cached values too in any template.
To know what values a given function caches (if any), you'll need to inspect the php code for that page function. Displaying an xarpage calls the function xarpages_user_display() in display.php; look for calls to xarVarSetCached(), such as:
- xarVarSetCached('Blocks.xarpages', 'pagedata', $data);
- xarVarSetCached('Blocks.xarpages', 'current_pid', $pid);
<xar:if condition="$info[0] eq 'xarpages'" />
<xar:set name="pid">xarVarGetCached('Blocks.xarpages','current_pid')</xar:set>
<xar:if condition="$pid eq 8"> ... </xar:if>
</xar:if>
Personally I dont like hardcoding pid's this way (the '8'), and prefer using a page name. That makes my templates more readable, maintainable, and portable. You can use xarpages_userapi_getpagebypath() like this
<xar:set name="aboutpage">xarModApiFunc('xarpages','user','getpagebypath',array('path'=>'about'))</xar:set>
<xar:if condition="!empty($aboutpage) and ($pid eq $aboutpage['pid'])"> ...
Then again, I'm not sure the processing overhead is worth it, to get what amounts to a static value on a given site.
Which Pubtype am I on?
This is also useful in other situations where i need to know the specific module page or in the case of articles, specific pubtypes, e.g.
<xar:if condition="$info[0] eq 'articles'">
<xar:set name="ptid">xarVarGetCached('Blocks.articles','ptid')</xar:set>
<xar:if condition="$ptid eq 5 or $ptid eq 7 or $ptid eq 13"> etc.
Telling yourself where you are
Another technique is similar to using the Block cached vars, but you do it yourself. In your module space template you cache a variable, then in the template outside this space (e.g. pages/default.xt) you test that.
For example, if base is the default template, and thus the home page template is mytheme/modules/base/user-main.xt, add this
<xar:set name="dummy">xarVarSetCached('base.mysite','home',1)</xar:set>
Then in pages/default.xt, you can get it
<xar:if condition="xarVarIsCached('base.mysite','home')">
...
</xar:if>
The only time that var will be cached is when your home page is loaded.
(ref: http://www.xaraya.com/index.php/xarbb/topic/2056 )
There are no comments attached to this item.



