The Pro version of ReReplacer also gives you the ability to replace with dynamic PHP code. By enabling the "Replace with PHP" setting, you can replace simple words or pieces of html with complete php code snippets.
Ready-to-use PHP Variables & Objects
ReReplacer 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.
ReReplacer 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)
Example: Inline PHP
Say you want to place a tag containing either a userid
, username
or name
, and replace it with a table containing some details of the user, like name, email address and registration date.
So for example you could use these tags: {usertable 123}
, {usertable peter}
or {usertable Peter van Westen}
.
Like explained in the Regular Expression example, you can create the dynamic search string like:
\{usertable (.*?)\}
In the replace field you can place the required php code inside <?php
an ?>
tags:
<?php $query = $db->getQuery(true)
->select('name, email, registerDate') ->from('#__users') ->where('id = ' . $db->q('\1') . ' OR username = ' . $db->q('\1') . ' OR name = ' . $db->q('\1')); $db->setQuery($query); $user = $db->loadObject(); if ($user) { echo ' <table> <tr> <td>Name:</td> <td>' . $user->name . '</td> </tr> <tr> <td>Email:</td> <td>' . $user->email . '</td> </tr> <tr> <td>Registered on:</td> <td>' . $user->registerDate . '</td> </tr> </table> '; } ?>
Example: External File
For long blocks of php/html you can also choose to place the code in an external file and simply include it.
So in the replace field you can place:
<?php $myvar = '\1'; include JPATH_SITE . '/myfiles/file.php'; ?>
With the above code, you can use the variable $myvar
in the external file. The variable will contain whatever you placed inside the {usertable}
tag in your article. Of course, you can name the variable however you want.