Forums / Developer / AJAX to return an eZ Publish template? eg new comment

AJAX to return an eZ Publish template? eg new comment

Author Message

Paul Wilson

Thursday 29 May 2008 1:38:51 am

I'm thinking that it might be possible and useful to have AJAX to load eZ Publish Templates.

For example, when looking at an article and clicking the button for a new comment, an AJAX call returns the edit (new) comment template into the current article page, without a page reload.

Has anyone done this type of thing? Any good ideas on how or if it might be possible?

It's simple enough to do this type of thing with AJAX, but the big difference I'm thinking of is the <b>re-use of the eZ Publish templates</b>. Without this, we all wind up re-writing custom (template-like) code to edit/view these objects just because they're being accessed with AJAX.

So the AJAX transaction might be:
<b>Request:</b> <send me html for an edit-comment template>
<b>Response:</b> <html code generated using eZ & eZ Templates>

Then this code could be placed in a DIV via Javascript, and Javascript then does any validation and creates the comment via another AJAX transaction.

The main challenges I see are that:
- generating pure HTML code without eZ wanting to do its normal page redirects/validation
- security - returning HTML code, perhaps use some md5 checking/encoding

Thanks

Kristof Coomans

Thursday 29 May 2008 3:01:16 am

Hi Paul

The ezxajax_classattributes extension uses the result of some templates: http://projects.ez.no/ezxajax_classattributes

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Paul Wilson

Thursday 29 May 2008 8:01:38 am

Excellent, thanks Kristoff. That gives me the clues I need.

Maxime Thomas

Thursday 29 May 2008 10:57:20 pm

You may use your custom library (e.g. if you would like to you scriptacoulus, extjs or jQuery) and access your custom eZPublish templates via the "layout/set" functionnality.

So, you design your own functionnality in a custom module, like fetching meteo on yahoo.com, and to call it, you may use this one to change the layout :

http://www.mydomain.com/mysiteacess/layout/set/ajax/mymodule/myfunction/parameters..

All you have to do is to declare a new pagelayout template for ajax (pagelayout_ajaxa.tpl), set up your layout.ini file and finally edit the pagelayout file as :

{$module_result.content}

Or maybe there's something eZier but I haven't found.

Maxime Thomas
[email protected] | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

Patrick Renaud

Thursday 24 July 2008 6:09:01 am

Hi everybody,

I'm trying to use Ajax to load some templates. Kristof's answer was very promising, but I couldn't make it work ( see http://ez.no/developer/forum/setup_design/how_to_ez_ajax_my_site/re_how_to_ez_ajax_my_site ), so that I tried Maxime's.

It works, or almost...
The template I need to fetch displays a different information according to the current user.
The problem is, when you call this template using ajax, the user is always the anonymous one.

Does anybody have an idea on how to solve this ?

Ca, c'est fait !

Paul Wilson

Thursday 24 July 2008 5:05:23 pm

Hi Patrick,

Here's a cut-down version of where I got to. I did get it to work. Please note that for simplicity I have stripped out some of the logic in this code (some elements to check permissions), so there may be some extra pieces here that can be safely removed / ignored. Some of the include statements are in peculiar places through the code for similar reasons. I'm heading away for a few days, so apologies for the rushed code & reply.

hth

- Paul

function EditBox($ContentObjID){
	// Add Includes required for function, others loaded IFF logic shows they're needed.
	include_once( 'kernel/classes/ezcontentobject.php' );
	include_once( 'kernel/classes/ezcontentclass.php' );
	include_once( 'kernel/classes/ezcontentobjectattribute.php' );
	include_once( 'kernel/classes/ezcontentobjectversion.php' );
	include_once( 'kernel/classes/ezcontentfunctions.php' );
	include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );

	$obj = $ContentObjID ? eZContentObject::fetch( $ContentObjID ) : false; 
	$classID = $obj->attribute( 'contentclass_id' );
	$class = eZContentClass::fetch( $classID );
	$EditLanguage="eng-AU";
	$FromLanguage="eng-AU";
	$ContentObjectLanguageCode=="eng-AU";
	$CurrentVersion = $obj->attribute('current_version');

	// Set the new version as the published version
    include_once( 'lib/ezutils/classes/ezoperationhandler.php' ); 
    $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $obj->attribute( 'id' ), 'version' => $version->attribute( 'version' ) ) );
	$CurrentVersion = $obj->attribute('current_version');

    require_once( 'kernel/common/template.php' );
            $tpl = templateInit();

            $res = eZTemplateDesignResource::instance();
            $res->setKeys( array( array( 'object', $obj->attribute( 'id' ) ),
                                array( 'class', $class->attribute( 'id' ) ),
                                array( 'class_identifier', $class->attribute( 'identifier' ) ),
                                array( 'class_group', $class->attribute( 'match_ingroup_id_list' ) ) ) );
			$originalContentAttributes =& $obj->contentObjectAttributes();
			$tpl->setVariable( 'view_parameters', $viewParameters ); 
            $tpl->setVariable( 'edit_language', $EditLanguage );
            $tpl->setVariable( 'from_language', $FromLanguage );
            $tpl->setVariable( 'object', $obj );
            $tpl->setVariable( 'class', $class );
			$tpl->setVariable( 'current_version', $CurrentVersion );
			$tpl->setVariableRef( 'content_attributes', $originalContentAttributes );
            $Result = array();
            $Result['content'] = $tpl->fetch( 'design:content/edit.tpl' );

	// Create New Object Response
	$objResponse = new xajaxResponse();
	$returnthismessage=$Result['content'];
	$objResponse->setReturnValue($returnthismessage);

    // Send Response
	return $objResponse;

Patrick Renaud

Friday 25 July 2008 7:01:49 am

Hi Paul,

Thanks a lot for your quick answer. :)
I'll be away for some weeks too, but no doubt I'll find it useful.

Ca, c'est fait !

Norman Leutner

Wednesday 30 July 2008 1:49:58 am

@Maxime
Just a small hint: You should always write own modules for such kind of functionality.
This is much faster than using the /layout/set functionality. Especially for AJAX.
Within you module you can just fetch the content you need and output it directly without using the template engine. (For example as an xml document)

Mit freundlichen Grüßen
Best regards

Norman Leutner

____________________________________________________________
eZ Publish Platinum Partner - http://www.all2e.com
http://ez.no/partners/worldwide_partners/all2e_gmbh

Maxime Thomas

Monday 04 August 2008 10:37:03 pm

I'm not sure to understand what you have said, just after some weeks of holidays...
When you enter the module mecanism, you have to deal with the pagelayout template.
So, I suppose you have to disable the pagelayout in the Result variable to do this way.

Maxime Thomas
[email protected] | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

André R.

Wednesday 13 August 2008 1:43:17 am

Ajax views should have something like this in it's end:

eZDB::checkTransactionCounter();
eZExecution::cleanExit();

1. Avoids possessing of pagelayout
2. stops the execution, so the sessions variables for remembering last accessed page is not stored for this ajax url.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Norman Leutner

Wednesday 13 August 2008 1:49:39 am

Just a small example:

<?php

$Module =& $Params['Module'];

if ( $Params[ 'NodeID' ] )
{
	echo "Received NodeID:".$Params[ 'NodeID' ];
}

eZDB::checkTransactionCounter();
eZExecution::cleanExit();

?>

Mit freundlichen Grüßen
Best regards

Norman Leutner

____________________________________________________________
eZ Publish Platinum Partner - http://www.all2e.com
http://ez.no/partners/worldwide_partners/all2e_gmbh