
For our final sGen tutorial, we'll walk through how to use the powerful sgen.parse plugin system to handle any advanced logic or needs that might arise in your application.
As of this writing ( v2008.Summer.3 ), Serenity ships with two built in plugins, parse_url and parse_member.
To understand how these work, let's open our HTML wrapper back up ( tpl-global.php ) and add the following in the body somewhere:
<a href='<sgen.parse url="link_to_me.html" />'>Here is a link</a>
It isn't necessary to add any code to your action handler to use the plugins. The code we inserted above maps to the following file:
/core_layer/classes/output/sgen.parse/parse_url.php
When encountering a sgen.parse tag, sGen will locate the appropriate driver under the sgen.parse folder shown above, and evaluate the PHP snippet it contains. The return result of the URL plugin is a properly formatted full URL, including any session information.
Likewise with the Member plugin, if we insert the following:
<sgen.parse member="user_email" />
into our HTML template, sGen will load the parse_member.php driver from the sgen.parse folder and execute the code. The Member plugin basically acts as a shortcut to the Serenity::user()→get() function, and allows dynamic access to the member array.
Additional plugins are encouraged and welcome. The structure of a sGen plugin looks like this:
$PARSE['string'] = Serenity::user()->get($PARSE['args'][0]);
It's important to note that while this is PHP code, you cannot wrap the statements in <?php & ?> tags due to the way it is parsed.
When executing a plugin, sGen expects the final formatted string to be available in $PARSE['string'], and sends any arguments in the sgen.parse tag to the driver as a numerical array in $PARSE['args'].
For example:
<sgen.parse url="some_url.html" />
will be available as:
$PARSE['args'][0]; // Contains some_url.html
Multiple arguments are supported, and would look something like this:
Given: <sgen.parse arg1="heythere" arg2="serenity" /> Access: $PARSE['args'][0]; // heythere $PARSE['args'][1]; // serenity
All Serenity:: functions are available in drivers, however for obvious reasons you can't access members from config.php, user passwords, or any other potentially sensitive information.