I need to include some php code that must be repeated in many tmpls. How can I do this, may be as class including? And how can I write php file with my class in a right way? In other words I need something like
views/category/tpml/default.php
JLoader::register('MyClass', '/administrator/components/com_mycom/helpers/myclass.php');
$repeatedcode = new MyClass();
echo $resultstr;
views/article/tpml/default.php
JLoader::register('MyClass', '/administrator/components/com_mycom/helpers/myclass.php');
$repeatedcode = new MyClass();
echo $resultstr;
myclass.php
class MyClass {
// some code with string for echo in the end
$resultstr = ...
}
...
UPDATE: @Guilherme thank you! So now it's looking as
The file /mytemplate/html/com_content/article/default.php:
require_once '/administrator/components/com_mycom/helpers/myclass.php';
MyComHelper::myFunction($param);
$newstring = str_replace($find, $replace, $this->item->text);
echo $newstring;
The file administrator/components/com_mycom/helpers/myclass.php:
defined('_JEXEC') or die;
abstract class MyComHelper
{
public static function myFunction($param)
{
$db = &JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('ua', 'ru')))
->from($db->quoteName('#__words'));
$db->setQuery($query);
$results = $db->loadAssocList();
$find = array();
$replace = array();
foreach ($results as $row) {
$find[] = $row['ua'];
$replace[] = $row['ru'];
}
return $find;
return $replace;
}
}
This script replaces every ua words with matched ru words that are stored in my database and it works if I add the script to tmpl directly. But in the case with including when I open a page with an article I see the blank page which contains only a heading and nothing else i.e. content isn't displayed. Maybe a problem with array?