PageRender2 works with a few straightforward commands, but you can pass options to alter the behavior of each of these commands.
If you are not using sherpa, create a PR2 instance as follows:
SAutoLoad::addModule('pr2');
Next, create an instance of PR2 or of a local PR2:
$prm = new PageRender2(); /* or $prm = new LocalPageRender2() */
If you want to make sure that your PR2 instance is functional, you should be able to test it by calling:
$prm->renderPrint();
It can already build you a page! But that is a pretty boring page. So the next (and most important) subject is...
For the Page Head
For Page Navigation and Naming
For Tabs Bars:
For Feature Wells:
For the Bottom Bar
For Metrics Sites
Reserved
Look at the documentation for the SPageTemplate you are using to find out what slots to load your content into.
In the simplest case, if
then you can run the following:
$prm->load($input);
If your input is HTML instead of StdXML3, you can use the options array to specify that:
$prm->load($input, array('inFormat' => 'html'));
The translator that PR2 will use is determined by the options you pass to load().
You can indicate that your input is something other than the default type by passing an option to the load function, as we saw above. This code snippet is simply telling PR2 that the input string is HTML rather than the default, XML.
$prm->load($input, array('inFormat' => 'html'));
So, when you specify inFormat to be html, PR2 simply chooses the STransHTML translator and passes your input through that.
You can pick which translator is used by changing the option specifying which translator handles your type of input. You simply change the value for the option translator_type
(where type is your input type) to the translation class you want to use. The translator can either be a default translation class (in common/pr2/translate), or a custom translation class (in project/app/translate).
Below is an example of picking the STransBookXML class to translate your input XML. Note that specifying inFormat to be xml is unnecessary as xml is the default inFormat.
/* ex. choosing a non-standard translator for XML */ $prm->load($input, array('inFormat' => 'xml', 'translator_xml'=>'STransBookXML')); /* ex. to load your navigation XML */ $prm->load($navXML, array('inFormat' => 'xml', 'translator_xml' => 'STransNavXML'), 'Nav');
The option for the translation target is trans_target
. Simply specify a slot name for this option, and the content will be added to that slot rather than to the Main (default) slot.
For instance, the following code would use the default translator for HTML (STransHTML) and would direct its output to the SideBar slot in the page buffer.
$prm->load($input, array('inFormat' => 'html', 'trans_target' => 'SideBar'));
$prm->load($input, array('inFormat' => 'html', 'trans_target' => 'SideBar', 'trans_mode' => 'set'));
$prm->add(new TKLabel('Hello, World!'));
Of course, you can add any TK2 view to your PR2 page, no matter how complex. You can also pass the standard translator options to the add function. For example, if we wanted to load the TK2 item into the SideBar:
$prm->add(new TKLabel('Hello, World!'), array('trans_target' => 'SideBar'));
$s = Snap2::lookup('//test/sherpa'); $prm->load($s);
To make your page be a typical Shodor-style page, set your page layout to SShodorLayout. This layout class knows how to write out the HTML for all possible parts of a Shodor-style page. (You don't need to do this in your code, since it is the default layout.)
$prm->setOption('layout', 'SShodorLayout');
Then, if you want to re-order, hide, or change the style of the meta-slots, you can use a page template. For instance, to choose a template that hides the PageTitle and PathBar, shows the UpperFeatureWell, sets the UpperFeatureWell to display as two columns, and sets the ContentWell to display with a right side bar, you could use the template called SplashPageTwoColsRightSideBar:
$prm->setOption('template', 'SplashPageTwoColsRightSideBar');
Layouts and templates are documented on a class-by-class basis. Some templates may only look good for certain layouts. For instance, the PrintTemplate looks good under SShodorLayout, but not HPCULayout.
In addition to using globally available layouts and templates, you can extend them to create local layouts and templates in your own project. You can extend SShodorLayout (or SLayout) and SPageTemplate and store your classes in app/layout and app/template. If you are doing this, be sure to add those folders to the search path using SAutoLoad::addSearchPath().
This can all be done using the renderPrint() command:
$prm->renderPrint();
In these cases, it is useful to use the flashAndExit() function to render and print a pretty error page and then exit the script immediately. Here's an example:
if($error) { $prm->flashAndExit('You made my program angry! Goodbye.'); }
include_once("../../config/standardIncludes.php5"); /* create your page */ $prm = new PageRender2(); /* load in your static XML content */ $xml = file_get_contents("$PROJECT_ROOT/content/sampleList.xml"); $prm->load($xml); /* load in your SNAP2 content */ $rsrc = Snap2::lookup('//test/sherpa'); $prm->load($rsrc); /* render your page to the screen */ $prm->renderPrint();
Here is the default LocalPageRender2 provided by the sherpa site template. In this class, the local PR2 overrides the constructor to set the collection name slot to the name of the project at the time the PR2 instance is created. This value could be changed in the code as the controller runs.
It also overrides renderPrint() so that it can generate and add the navigation XML to the page at the last minute. This allows controllers to keep modifying/adding items to their NavXMLModule until the moment the page is rendered to the screen. This allows pages to add tabs, etc. more easily.
class LocalPageRender2 extends PageRender2 { public function __construct() { global $SITE_PATH, $PATH, $STRING; parent::__construct(); // get the page of this PR2 and set the default Collection Name // to be the full name of this site. $this->getPage()->setSlot('CollName', $STRING['siteName']); } public function renderPrint() { global $snm; // load the site nav before rendering and printing any page if (isset($snm) and ($snm instanceOf NavXMLModule)){ $nav = $snm->generateXML(); $this->load($nav, array('translator_xml'=>'STransNavXML'), 'Nav'); } parent::renderPrint(); } }
These are the most frequent reasons for extending PageRender2, but there are many other possibilities!