Tip: Javascript and eZ publish communication using user preferences

Thursday 18 August 2011 3:33:28 pm

By : Thiago Campos Viana

In this post I will show an easy way to make eZ publish communicate with javascript.

 

{set-block scope=global variable=cache_ttl}0{/set-block}

<input type="checkbox" id="var_test" name="var_test"
{if eq(ezpreference('var_test'),'true')} checked="checked"{/if}> Test


<script type="text/javascript">
var prefs_url="{"/user/preferences/set/var_test"|ezurl(no)}";
{literal}
$("#var_test").change(function() {
  $.ajax({
      url: (prefs_url+"/"+$("#var_test").is(":checked")),
      success: function(){}
    });
});
{/literal}

</script>

So, basically in this sample every time the user sets the checkbox value, he sets his user preference using an ajax call to the user/preferences view telling it to set the var value, so when the user reloads the page the checkbox will remain with its last state by getting the preference value using the ezpreference template operator. Simple right?

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 17 2025 23:52:48
Script start
Timing: Jan 17 2025 23:52:48
Module start 'layout'
Timing: Jan 17 2025 23:52:48
Module start 'content'
Timing: Jan 17 2025 23:52:48
Module end 'content'
Timing: Jan 17 2025 23:52:48
Script end

Main resources:

Total runtime0.0700 sec
Peak memory usage6,144.0000 KB
Database Queries42

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 589.5313152.6875
Module start 'layout' 0.00550.0026 742.218839.5313
Module start 'content' 0.00810.0605 781.7500350.3438
Module end 'content' 0.06860.0014 1,132.093815.8594
Script end 0.0699  1,147.9531 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00304.3547150.0002
Check MTime0.00131.8991150.0001
Mysql Total
Database connection0.00081.181010.0008
Mysqli_queries0.028340.4436420.0007
Looping result0.00030.3841400.0000
Template Total0.038254.720.0191
Template load0.00192.723220.0010
Template processing0.036351.923620.0182
Template load and register function0.00010.101610.0001
states
state_id_array0.00050.742010.0005
state_identifier_array0.00081.153720.0004
Override
Cache load0.00152.123490.0002
Sytem overhead
Fetch class attribute name0.00142.068530.0005
class_abstraction
Instantiating content class attribute0.00000.010630.0000
General
dbfile0.00070.9679100.0001
String conversion0.00000.007240.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplblog_entry/full.tplextension/community_design/design/suncana/override/templates/blog_entry/full.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
3content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezkeyword.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezkeyword.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 9
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs