Wiki : ModuleProgrammingPart3
Documentation Home :: Categories :: Index :: Recent Changes :: Comments :: Search :: Help :: Login/RegisterDisplay with pnRender
In this section we will begin to explore how to use pnRender templates to display your content. pnRender is an extension of SMARTY∞ which allows the developer to separate the display of data from the code that processes or generates the data.
This is an extremely important step forward with web applications. It is similar to the idea of database reports that display data from a table or query.
Templates are stored in the modules own pntemplates subdirectory. The naming scheme is
{$module}_{$type}_{$function}.htm
Templates can be named using extensions other than .htm--many people, for example, use .tpl
So function helloworld_user_greetings() would typically use the template helloworld_user_greetings.htm
Let's explore an example (building upon our previous "hello world" pnuser.php code snippet):
<?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');
// Create a new pnRender object.
$pnRender = new pnRender('HelloWorld');
// Assign our variables to the template - this create a variable called 'myname'
// and assigns the value in $myname so it can be used in our template.
// Please note that keeping variables sensibly named and consistent is very helpful.
$pnRender->assign('myname', $myname);
// Assign our variables to the template - this create a variable called 'myage'
// and assigns the value in $myageso it can be used in our template.
// Note again our keeping variables sensibly named and consistent.
$pnRender->assign('myage', $myage);
// Fetch the tempate named, process it and return the result.
return $pnRender->fetch('helloworld_user_greetings.htm');
}
?>
// 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');
// Create a new pnRender object.
$pnRender = new pnRender('HelloWorld');
// Assign our variables to the template - this create a variable called 'myname'
// and assigns the value in $myname so it can be used in our template.
// Please note that keeping variables sensibly named and consistent is very helpful.
$pnRender->assign('myname', $myname);
// Assign our variables to the template - this create a variable called 'myage'
// and assigns the value in $myageso it can be used in our template.
// Note again our keeping variables sensibly named and consistent.
$pnRender->assign('myage', $myage);
// Fetch the tempate named, process it and return the result.
return $pnRender->fetch('helloworld_user_greetings.htm');
}
?>
Our template pntemplates/helloworld_user_greetings.htm would look like this:
<div>
Hello World! My name is <!--[ $myname ]--> and I am <!--[ $myage ]-->.
</div>pnRender (aka Smarty) tags in PostNuke are started by <!--[ and ended by ]-->
The example above <!--[ $myname ]--> simply means insert the variable called $myname - this was the variable we assigned in our code before 'fetching' the template.
Beginning with .762 you can also use this short form:
<?php
// pnuser.php
function helloworld_user_greetings()
{
// create a new pnRender object.
$pnRender = new pnRender('HelloWorld');
// assign all module vars for this module in one step. Doing this we do not need to bother
// with variables added later on
$pnRender->add_core_data();
// fetch the template named, process it and return the result.
return $pnRender->fetch('helloworld_user_greetings.htm');
}
?>
// pnuser.php
function helloworld_user_greetings()
{
// create a new pnRender object.
$pnRender = new pnRender('HelloWorld');
// assign all module vars for this module in one step. Doing this we do not need to bother
// with variables added later on
$pnRender->add_core_data();
// fetch the template named, process it and return the result.
return $pnRender->fetch('helloworld_user_greetings.htm');
}
?>
with this template
<div>
Hello World! My name is <!--[ $pncore.HelloWorld.myname ]--> and I am <!--[ $pncore.HelloWorld.myage ]-->.
</div>You see, all variables are assigned to $pncore.<modulename>.
Plugins
Plugins are functions we can call while inside the display template. SMARTY, which pnRender is based on has several built in 'plugins' or functions. We can also use PostNuke system wide plugins, or define our own for our specific module. Plugins can be passed arguements like a normal function.
PostNuke system wide plugins can be found in [root]/system/pnRender/plugins and module specific plugins in our module folder in pntemplates/plugins
Plugin files are named function.pluginname.php and contain *one* function inside called
smarty_function_{$pluginname}
A plugin is called within a template like this:
<!--[ myplugin ]-->
The example above will include the file in pntemplates/plugins/function.myplugin.php and call the function
smarty_function_myplugin()
<!--[ agecheck myage=$myage ]-->
The example above will include the file in pntemplates/plugins/function.agecheck.php and call the function
smarty_function_agecheck() and parse the variable 'myage' to the function.
Variables are passed in an associative array of (name => value).
<?php
//example plugin agecheck
function smarty_function_agecheck($params, &$smarty)
{
// pull the agecheck variable out of the $params
$agecheck = (int)$params['agecheck'];
if($agecheck < 18) {
$result = "You are under 18 years old";
}else{
$result = "You are 18 years or over";
}
return $result;
}
?>
//example plugin agecheck
function smarty_function_agecheck($params, &$smarty)
{
// pull the agecheck variable out of the $params
$agecheck = (int)$params['agecheck'];
if($agecheck < 18) {
$result = "You are under 18 years old";
}else{
$result = "You are 18 years or over";
}
return $result;
}
?>
The $smarty object will be the same pnRender instance that called the plugin. This means the plugin can also modify the pnRender object. Our function could do something like
$smarty->assign('foo', 'bar');
Which would mean the variable $foo would now be available to the template.
Modifiers
A modifier is a different kind of plugin. They are found system wide in [root]/system/pnRender/plugins or in a module's own pntemplates/plugins fodler.
Files named modifier.{$name}.php and contain one function called smarty_modifier_{$name}
will be passed at least one string.
<!--[ $myname|pnvarprepdisplay ]-->
<?php
function smarty_modifier_pnvarprepdisplay($string)
{
return pnVarPrepDisplay($string)
}
?>
function smarty_modifier_pnvarprepdisplay($string)
{
return pnVarPrepDisplay($string)
}
?>
Output Filters
Next step: Module Programming Part 4
DeveloperDocs
CategoryDeveloperDocs
1 Comments so far
1. vcaravantes wrote on Jun 17, 2007 at 05:04 AM
Hi there.Seems like the code of example plugin agecheck is not right because it use dots instead of under score.
This tutorial is cool.
