Page 1 of 2
Using the Global variables from CMS
Posted: Wed Mar 22, 2006 4:18 pm
by Glenn
I have a question, but first a little help for anyone who needs this type of thing:
I've learned to use the variables defined by cmsms in my user tags rather than using my own select queries which is really efficient. For those who don't know what they are, here's a little user tag I use to quickly pull them out and identify them:
user tag name: show_variables
Use the tag in a template like this: {show_variables}
Here's the code for the tag:
Code: Select all
global $gCms;
$pageinfo = &$gCms->variables['pageinfo'];
$page_title = $pageinfo->content_title;
$template = $pageinfo->template_id;
$menu = $pageinfo->content_menutext;
$alias = $gCms->variables['page'];
$content_id = $gCms->variables['content_id'];
$curhierarchy = $gCms->variables['position'];
$page_name = $gCms->variables['page_name'];
echo "menu text: " . $menu."<br>";
echo "template: " . $template."<br>";
echo "page title: " . $page_title."<br>";
echo "alias: " . $alias."<br>";
echo "hierarchy: " . $curhierarchy."<br>";
echo "id: " . $content_id."<br>";
echo "page name: " . $page_name."<br>";
Okay, now my question...are there other variables that i could be using but don't know about? Specifically, it would be nice to know the variable for parent_id and parent title or parent menu text. Now I am using my own select statement to get that when i need it, by first getting the parent id:
Code: Select all
$query = "SELECT parent_id FROM cms_content WHERE content_id = '$content_id'";
$result = mysql_query($query,$dblink);
$content = mysql_fetch_assoc($result);
$parent_id = $content['parent_id'];
Then getting the title, menu text, whatever else I need with another query:
Code: Select all
$query = "SELECT menu_text FROM cms_content WHERE content_id = '$parent_id'";
$result = mysql_query($query,$dblink);
$content = mysql_fetch_assoc($result);
$menu_text = $content['menu_text'];
Obviously this isn't very efficient. I know breadcrumbs already does this, but I have poured through that code and with my (rudimentary) php/mysql skills can't figure it out.
Any help is appreciated.
Re: Using the Global variables from CMS
Posted: Wed Mar 22, 2006 5:32 pm
by calguy1000
I wrote this little user defined tag to see all of the variables that smarty knows about..... it may be helpfull to alot of people:
Code: Select all
// get a list of all of the smarty assigned variables
// user defined tag named "get_template_vars"
global $gCms;
$tpl_vars = $gCms->smarty->get_template_vars();
print_r( $tpl_vars );
Then place your {get_template_vars} tag somewhere in your content for debugging
Re: Using the Global variables from CMS
Posted: Wed Mar 22, 2006 5:40 pm
by Ted
Ok, time for an intro to hierarchy manager, which is new in 0.12.
Basically, hierarchy manager helps us keep the structure of the content in between page requests. It's a cache of sorts, but it's also more of a structure to make sure we're not querying the database more than necessary.
We store all of the content in nodes in a tree. The content has no properties loaded, so it doesn't use a lot of memory. However, if a parameter does get loaded, it only has to be loaded once, even if it's used again on the same page.
Ok, so what does this mean? Basically, you can utilize the hierarchy manager to look up stuff for you without writing sql queries. Since it's all the same interface, you don't have to worry about slowing down your site too much.
So, in a plugin, you can do:
Code: Select all
global $gCms;
$hm =& $gCms->GetHierarchyManager();
This will return the same global instance. The & is very important. Anyway... then you can grab a node from it.
Code: Select all
$node =& $hm->getNodeById($some_id);
or:
Code: Select all
$node =& $hm->getNodeByAlias($some_page_alias);
or even:
Code: Select all
$node =& $hm->getNodeByHierarchy('1.3.1');
Ok, so now you have a node object. Keep in mind that this isn't content. It's a thin wrapper around it. But it does keep all kinds of details about the structure. So, for instance, to get the parent's title...
Code: Select all
if ($node != null)
{
//Grab the parent node
$parentnode =& $node->getParentNode();
//Make sure something came back
if ($parentnode != null)
{
//Get the actual content object
$content =& $node->getContent();
if ($content != null) //Just in case
{
//Display it's title
echo $content->Title();
}
}
}
Or to find out if it has any children...
Code: Select all
if ($node != null)
{
if ($node->hasChildren())
{
//Do something...
}
}
Anyway, you should get the idea. This is a very powerful tool, but of course it's undocumented and well under the hood, so it's not easy to realize that it's there. Enjoy!
Re: Using the Global variables from CMS
Posted: Thu Mar 23, 2006 5:35 pm
by Glenn
Thanks so much Ted and Calguy, I'll still have some questions as I get into this more, but it looks like this is exactly the kind of thing I've been looking for!
However, this line is causing a problem:
I've used Ted's code in several different ways in a user tag called {parent_title} (used in the template in the content area) and after experiencing blank pages commented each section of the code out until I arrived at that line causing a problem. If I leave everything as Ted wrote it, I get blank pages. IF I comment that line out the page works (but obviously then the tag does nothing).
Here's the full tag with everything in it:
Code: Select all
global $gCms;
$content_id = $gCms->variables['content_id'];
$hm =& $gCms->GetHierarchyManager();
$node =& $hm->getNodeById($content_id);
if ($node != null)
{
if ($node->hasChildren())
{
echo "This page has children<br />";
}
else
{
echo "This page doesn't have children<br />";
}
//Grab the parent node
$parentnode =& $node->getParentNode();
//Make sure something came back
if ($parentnode != null)
{
//Get the actual content object
$content =& $node->getContent();
if ($content != null) //Just in case
{
//Display it's title
echo $content->Title();
}
}
}
Re: Using the Global variables from CMS
Posted: Fri Mar 24, 2006 12:13 am
by Ted
Sorry, that's me being dumb. Try:
Can you tell that I didn't test the code?

Re: Using the Global variables from CMS
Posted: Fri Mar 24, 2006 5:19 pm
by jeffunk
Hi, Thanks for all this work.
I tried out the code with the new Name() function in there and everything worked, except that it just displayed the page name of the current page. I was sort of hoping for the top level parent.
So I've be fiddling with it and have got something that will show you the title of the parent. Or the title of the current page if there is no parent. I don't know if we could use recursion to get up to the top level parent. (Is that allowed??!) Anyways, here's the function.
Code: Select all
// This is only going to step up the tree one level
global $gCms;
$content_id = $gCms->variables['content_id'];
$hm =& $gCms->GetHierarchyManager();
$node =& $hm->getNodeById($content_id);
if ($node != null)
{
//Grab the parent node
$parentnode =& $node->getParentNode();
$ncontent =& $node->getContent();
$pnode = $ncontent->ParentID();
//Make sure something came back
if ($pnode != -1)
{
//Get the actual content object
$content =& $parentnode->getContent();
}
else {
//Ok no parent lets get the title of this node
$content = $ncontent;
}
if ($content != null) //Just in case
{
//Display it's title
echo $content->Name();
}
}
Re: Using the Global variables from CMS
Posted: Tue Apr 11, 2006 1:26 pm
by jzip
Ted wrote:
Ok, time for an intro to hierarchy manager, which is new in 0.12.
...
Anyway, you should get the idea. This is a very powerful tool, but of course it's undocumented and well under the hood, so it's not easy to realize that it's there. Enjoy!
Even if the documentation is minimal, I think it would be very useful to have the ContentHierarchyManager class appear in the API documentation. That would allow one to find easily which methods it provides.
Re: Using the Global variables from CMS
Posted: Mon Apr 24, 2006 2:08 am
by skypanther
jzip wrote:I think it would be very useful to have the ContentHierarchyManager class appear in the API documentation.
Indeed! I know you're working on 0.13. But, could you please update the API docs to 0.12 for those of us trying to develop new code?
Thanks,
Tim
Re: Using the Global variables from CMS
Posted: Fri Jan 25, 2008 3:56 pm
by MarkFresh
// This is only going to step up the tree one level
How would I get it to show the highest level? I have content on e.g. level 2.1.3 and 3.2.2 but I would like to show the highest level title (2 and 3 in this case)
thanks
Mark
Re: Using the Global variables from CMS
Posted: Fri Jan 25, 2008 4:21 pm
by calguy1000
The CGSimpleSmarty module (what a bad name, who thought of that?) has smarty functions to
allow you to:
a) get the page alias of the parent of the current page (or any page)
b) get the page alias of the root parent of the current page (or any page)
c) get the page title of any page
Code: Select all
{$cgsimple->get_page_title($alias)}
d) get the content from any content block in any page
Code: Select all
{$cgsimple->get_page_content($alias)}
e) detect if the current page (or any page) has children
Just thought I'd let you know.... it's been tested, and it works well.
Re: Using the Global variables from CMS
Posted: Fri Jan 25, 2008 7:24 pm
by MarkFresh
Thanks, that works fine but it doesn't show the root title, the closest is the root alias
Re: Using the Global variables from CMS
Posted: Fri Jan 25, 2008 7:27 pm
by calguy1000
{$cgsimple->get_root_alias('','root_alias')}{$cgsimple->get_page_title($root_alias)}
Re: Using the Global variables from CMS
Posted: Sat Jan 26, 2008 11:43 am
by MarkFresh
great thank you.
so just to help me understand is that saying: "first get the root alias then, from that root alias, get the page title"? And it's done this way because it can't get the page title directly.
thanks again
Re: Using the Global variables from CMS
Posted: Sat Jan 26, 2008 2:54 pm
by calguy1000
I could have written a function to get the root page title directly, but this is more flexible and re-useable.
Re: Using the Global variables from CMS
Posted: Mon Nov 23, 2009 3:46 pm
by catfish
To pick up an old thread - is there a way to get a page URL based on the Alias? The page's URL can be constructed using the site's URL and the alias, but if the page in question is an external link, for example, that's no good...:/