This guide walks you through several examples on how to use the PHP assignment type in the Pro version of Content Templater.
PHP assignments (or conditions) give you the opportunity to assign to just about anything you can think of. You just need to know what PHP code you should use.
With PHP you can call on all data/information available in the variables, URL, database, etc. And then do your checks on this data. You can tell the module whether it should be displayed by ending the PHP code with a return true or false based on your checks, like:
if( $some_variable == 'some value' ) { return true; } else { return false; }
Or written in a more compact way:
return $some_variable == 'some value';
Ready-to-use PHP Variables & Objects
Below are a collection of ready-to-use PHP assignment scripts examples. You can also use these as examples and starting-point to create your own custom scripts.
Content Templater checks your PHP code to see if you are referencing any commonly used Joomla objects / variables, and then creates them for you.
This means you don't have to create these variables yourself every time.
Currently Content Templater creates these variables ready for use:
$app
The Joomla application framework$document
or$doc
The html document$database
or$db
The database$user
The user object containing the details of the guest or current logged in user$Itemid
The menu id of the page$article
The article object (only available when using the code inside articles)
Assign to article authors
// comma separated list of author ids $authors = '62,63,65';
// DO NOT EDIT BELOW return isset( $article ) && isset( $article->created ) && in_array( $article->created, explode( ',', $authors ) );
Assign to certain (parts of) email addresses
// comma separated list of (parts of) email addresses $emails = '@yahoo.com,@hotmail.com';
// DO NOT EDIT BELOW $pass = 0; foreach( explode( ',', $emails ) as $email ) { if ( strpos( $user->email, $email ) !== false ) { $pass = 1; break; } } return $pass;
Assign to ip addresses
// comma separated list of ip addresses $ips = '111.111.111.111,222.222.222.222';
// DO NOT EDIT BELOW return in_array( $_SERVER['REMOTE_ADDR'], explode( ',', $ips ) );
Assign to ip ranges
// comma separated list of ip ranges $ipranges = '123.255,234.255.123';
// DO NOT EDIT BELOW $ip = $_SERVER['REMOTE_ADDR']; $pass = 0; foreach( explode( ',', $ipranges ) as $iprange ) { if ( substr( $ip.'.', 0, strlen( $iprange.'.' ) ) == $iprange.'.' ) { $pass = 1; break; } } return $pass;
Assign to keyword (string) in page text
This will search for your search word in the output of the content area.
// string to be present in content area $search = 'my search string';
// DO NOT EDIT BELOW $buffer = JFactory::getDocument()->getBuffer('component'); return !is_array($buffer) && strpos($buffer, $search) !== false;
Assign to certain menu levels
// comma separated list menu item levels $showonlevel = '2,3';
// DO NOT EDIT BELOW $getMenuParent = create_function( '$id', '$db = JFactory::getDBO(); $query = "SELECT parent FROM #__menu WHERE id = ".(int) $id." LIMIT 1"; $db->setQuery( $query ); return $db->loadResult(); '); $level = 0; $menuid = JRequest::getInt( 'Itemid' ); while ( $menuid ) { $level++; $menuid = $getMenuParent( $menuid ); } return in_array( $level, explode( ',', $showonlevel ) );
You can set the first variable to what you want. level 1 would be the top level. level 2 would be a sub level, level 3 a sub-sublevel, etc.
So in above code, the module would show on level 2 and 3 (sub and sub-sub menu items).
Assign to number of visits after new session
// the maximum number of pageloads after a new session $max = 10;
// DO NOT EDIT BELOW $session = JFactory::getSession(); return $session->get( 'session.counter', 0 ) <= $max;
This will make your item pass the check for every new session and the 10 pageloads following it.
You could use this - for instance - for showing a teaser ad or something to new visitors.
Assign to template parameter values
// name of the template parameter to check $param = 'someparam'; // comma separated list of possible values $values = 'this,that';
// DO NOT EDIT BELOW $pass = 1; $document = JFactory::getDocument(); if ( !( isset( $document->params ) && isset( $document->params->_registry ) && isset( $document->params->_registry['_default'] ) && isset( $document->params->_registry['_default']['data'] ) ) ) { $pass = 0; } if ( $pass ) { $pass = 0; $params = $document->params->_registry['_default']['data']; if ( isset( $params->$param ) ) { if ( isset( $params->$param ) ) { $pass = in_array( $params->$param, explode( ',', $values ) ); } } } return $pass;
So - for instance - if you want to assign to all pages where the template parameter 'colorVariation' is set to 'blue' or 'green', then the first lines of the code should look like:
// name of the template parameter to check $param = 'colorVariation'; // comma separated list of possible values $values = 'blue,green';