Wiki : ModuleProgrammingPart2
Documentation Home :: Categories :: Index :: Recent Changes :: Comments :: Search :: Help :: Login/RegisterModule Variables
PostNuke can store variable for a module in it's registry. We dont need to concern ourselves with any data access as we have three standardised calls which will get, set and delete values.
pnModSetVar()
pnModSetVar($module, $name, $value)
One would normally initialise module variables in the modules initialisation script pninit.php. This is done at module installation time only and registers the variables with the PostNuke registry.
// pninit.php
function helloworld_init()
{
pnModSetVar('helloworld', 'myname', 'Fred Bloggs');
pnModSetVar('helloworld', 'myage', 32);
return true;
}
function helloworld_init()
{
pnModSetVar('helloworld', 'myname', 'Fred Bloggs');
pnModSetVar('helloworld', 'myage', 32);
return true;
}
pnModGetVar()
pnModGetVar($module, $name)
Back to our 'hello world' module lets add a function greetings to pnuser.php
// pnuser.php
function helloworld_user_greetings()
{
// get the variable 'myname' which belongs to the module 'helloworld' from the registry
$myname = pnModGetVar('helloworld', 'myname');
// get the variable 'myage' which belongs to the module 'helloworld' from the registry
$myage= pnModGetVar('helloworld', 'myage');
$greeting = "Hello World. My name is $myname and I am $myage";
// function returns to PostNuke Application Framework.
return $greeting;
}
function helloworld_user_greetings()
{
// get the variable 'myname' which belongs to the module 'helloworld' from the registry
$myname = pnModGetVar('helloworld', 'myname');
// get the variable 'myage' which belongs to the module 'helloworld' from the registry
$myage= pnModGetVar('helloworld', 'myage');
$greeting = "Hello World. My name is $myname and I am $myage";
// function returns to PostNuke Application Framework.
return $greeting;
}
Do not rely on the return value! If the module variable does not exist, pnModGetVar() returns false. If your module var is likely to hold a boolean, you might get into trouble. Better store such data as 'on' for true and 'off' for false to avoid problems.
pnModDelVar()
pnModDelVar($module, $name)
This function would normally be used during the uninstall of a module in pninit.php It removes the variables from the registry.
// pninit.php
// function to uninstall module
function helloworld_remove()
{
pnModDelVar('helloworld', 'myname');
pnModDelVar('helloworld', 'myage');
// delete all module vars in one step
pnModDelVar('helloworld');
return true;
}
// function to uninstall module
function helloworld_remove()
{
pnModDelVar('helloworld', 'myname');
pnModDelVar('helloworld', 'myage');
// delete all module vars in one step
pnModDelVar('helloworld');
return true;
}
Next step: Module Programming Part 3
DeveloperDocs
CategoryDeveloperDocs
