Joomla 3 load template in external file - php

I have created an external file. I want to load joomla default template in that file. I used below code, and it caused an error:
Fatal error: Call to protected method JApplicationSite::render() from
context ' '
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', realpath(dirname(__FILE__) ));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe = JFactory::getApplication('site');
JPluginHelper::importPlugin('system');
$mainframe->initialise();
$myContent = "Hello World!!";
$document = JFactory::getDocument();
$document->setBuffer($myContent, 'component');
$mainframe->render();
echo $mainframe;

At the least you need to let the Joomla app know which template you'd like to render.
//normal external script initialisation
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath('/home/somepath/public_html/somedirectory/'));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');
require_once( JPATH_LIBRARIES .DS.'joomla'.DS.'factory.php');
$app = JFactory::getApplication('site');
$app->initialise();
//jimport( 'joomla.document.html.html' ); required in certain cases
//initialise the doc to use the Html renderer
$doc = new JDocumentHtml();
//minimal info required
$options['directory'] = "/my/template/directory";
$options["template"] = $app->getTemplate();//get the template name
$options["file"] = "index.php";//usually
//Optional: Manually set params
//$params = new JRegistry();
//$params->colorVariation = "blue";
//$params->widthStyle = "fluid";
//$params->showComponent = "1";
//$options["params"] = $params;
//you may initialise the document object here and set the buffers
//$document = JFactory::getDocument();
//$document->setBuffer($contents, 'module', $name);
// $document->setHeadData($data['head']);etc
echo $doc->render(false, $options);
$app->close();
Of course dont expect it to display the same way esp if you use relative urls. Can be used for other purposes though like examining the scripts in a document template. tested on Joomla 3.3 PHP 5.5 . It will not work on lower versions like 1.5.

Related

Load Joomla K2 module in an external php

Hi I'm trying to load a K2 content module in an external php file. I found some info and examples about this but the problem it's that it is only working with some Joomla modules (custom html and others) but not with the k2 modules. This is mi code:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__) .'' ) );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS. 'includes' .DS. 'defines.php' );
require_once ( JPATH_BASE .DS. 'includes' .DS. 'framework.php' );
$app = JFactory::getApplication('site');
$app->initialise();
$modules = JModuleHelper::getModules("contenedor-eventos");
$document = JFactory::getDocument();
$attribs = array();
$attribs['style'] = 'xhtml';
foreach ($modules as $mod)
{
echo JModuleHelper::renderModule($mod, $attribs);
}
I have this error message: Fatal error: Class 'K2Model' not found in /home/my_user/my_domain/modules/mod_k2_content/helper.php on line 289
The code in helper.php line 289 is:
$model = K2Model::getInstance('Item', 'K2Model');
Hope someone could help me.
Thanks in advance.

Weird behaviour from joomla and virtuemart regarding cart data

So I wrote up this code to display the cart total outside of joomla framework:
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
$total += $product[quantity];
}
echo $total;
?>
which works fine (by displaying the total items in cart) in a top level directory (/public_html/test.php)
but if I move it to a second-level directory, like (/public_html/includes/test.php), I get this error:
first, the code (notice the /../store because we're in a second-level now):
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
$total += $product[quantity];
}
echo $total;
?>
then the error:
Fatal error: Uncaught exception 'Exception' with message 'XML file did not load' in /home/me/public_html/store/libraries/joomla/form/form.php:2020 Stack trace: #0 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmplugin.php(201): JForm::getInstance('weight_countrie...', '/home/me/publ...', Array, false, '//vmconfig | //...') #1 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmpsplugin.php(45): vmPlugin::getVarsToPushByXML('/home/me/publ...', 'weight_countrie...') #2 /home/me/public_html/store/plugins/vmshipment/weight_countries/weight_countries.php(44): vmPSPlugin->getVarsToPush() #3 /home/me/public_html/store/libraries/joomla/plugin/helper.php(194): plgVmShipmentWeight_countries->__construct(Object(JDispatcher), Array) #4 /home/me/public_html/store/libraries/joomla/plugin/helper.php(125): JPluginHelper::_import(Object(stdClass), true, NULL) #5 /home/me/public_html/store/administrator/components/com_virtuemart/helpe in /home/me/public_html/store/libraries/joomla/form/form.php on line 2020
I have no clue why it works fine in the top level but then not in subdirectories. Any ideas?
When you are going to subdirectory the path changes. I got these paths for the defined JPATH_BASE.
For top level
string '/Applications/MAMP/htdocs/store';
And for subdirectory
string '/Applications/MAMP/htdocs/test/../store';
As dirname(realpath(FILE) will give the location of current directory it is in.
So there are two methods to get correct path
JPATH_BASE can be given the exact location like
/var/www/store for linux system
/Applications/MAMP/htdocs/store for MAC
C:/xampp/htdocs/www/store for windows
example
define('JPATH_BASE', '/var/www/store' );
These are just examples.
It can also be achieved this way by defining JPATH_BASE this way
Replace
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
BY
$path=getcwd();
$parts = explode(DIRECTORY_SEPARATOR, $path);
$pop = array_pop($parts);
$path = implode(DIRECTORY_SEPARATOR, $parts);
define('JPATH_BASE', $path.'/store');

Accessing Joomla Enviroment script fails if not in root

I'm a newbie but trying to learn.
I have a script to read the joomla user details. It's a simple script just to display logged in user name. The script works if I put it in the root directory of my website.
What I want to do is run the script from a folder off the root, /ssscart, for example.
When I put the script in the sub-directory it does not execute.
Here is my script:
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
$user = JFactory::getUser();
$joomla_name = $user->name;
$joomla_email = $user->email;
$joomla_password = $user->password;
$joomla_username = $user->username;
echo 'hello';
echo " - ".$joomla_username;
?>
I believe my problem is in the JPATH_BASE entry but haven't been able to figure it out yet.
Thanks
G
Try this,
The is issue is due to JPATH_BASE path getting wrongly.
when you are in root it will look like.
define('JPATH_BASE', dirname(__FILE__) );
So in your case you are in sub folder so it will be look like.
define('JPATH_BASE', dirname(dirname(__FILE__)) );
so your final code will be look like.
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
Hope it works..

Loading module external page Joomla

I am trying to load a module on an external page from the Joomla core.
I am using the following:
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__).'/../../..' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('xxxx', 'xxxxxxx');
$memberships = JModuleHelper::renderModule($module);
But get the following error
JPath::check Use of relative paths not permitted
Any ideas

Using jimport in my own script

When i try to use jimport('joomla.user.helper') it gives me this error.
Fatal error: Call to undefined function jimport() in /home/joomclan/public_html/quiz/pop_fetching.php on line 223
This is my code where i use this:
function addJoomlaUser($name, $username, $password, $email) {
jimport('joomla.user.helper');
$salt = JUserHelper::genRandomPassword(32);
$crypted = JUserHelper::getCryptedPassword($password, $salt);
$cpassword = $crypted.':'.$salt;
$data = array(
"name"=>$name,
"username"=>$username,
"password"=>$password,
"password2"=>$password,
"email"=>$email,
"block"=>0,
"groups"=>array("1","2")
);
add at the top
include(JPATH_BASE.'libraries/loader.php')
but, afraid, something wrong with your code
http://www.webdeveloper.com/forum/showthread.php?226904-jimport-in-Joomla-how-does-it-work
It's defined in "libraries/loader.php", which is included by "libraries/joomla/import.php", which is included by "includes/framework.php", which is included by "index.php" (all paths relative to the base Joomla directory).
Probably this will be helpful as well
jimport not working in Joomla 1.5
in your script, at the top, initialise joomla framework as below.
`define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$app =& JFactory::getApplication('site');
$app->initialise();
jimport( 'joomla.user.user' );`
I always use that.

Categories