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
maxime.thomas@wascou.org | 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
maxime.thomas@wascou.org | 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

eZ debug

Timing: Jan 18 2025 05:20:49
Script start
Timing: Jan 18 2025 05:20:49
Module start 'content'
Timing: Jan 18 2025 05:20:49
Module end 'content'
Timing: Jan 18 2025 05:20:49
Script end

Main resources:

Total runtime0.1834 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0069 587.8594180.8125
Module start 'content' 0.00690.0064 768.6719114.0547
Module end 'content' 0.01330.1700 882.7266543.8750
Script end 0.1833  1,426.6016 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00392.1331200.0002
Check MTime0.00150.8340200.0001
Mysql Total
Database connection0.00060.341910.0006
Mysqli_queries0.134373.21921410.0010
Looping result0.00150.82461390.0000
Template Total0.169692.510.1696
Template load0.00090.475410.0009
Template processing0.168791.983910.1687
Override
Cache load0.00060.320610.0006
Sytem overhead
Fetch class attribute can translate value0.00080.453210.0008
XML
Image XML parsing0.00030.164110.0003
General
dbfile0.00291.6031200.0001
String conversion0.00000.004930.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs