CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • New feature: template client side
Pages: [1]

Author Topic: New feature: template client side  (Read 528 times)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
New feature: template client side
April 19, 2012, 05:25:48 am
We are using mustache to do client side templating. It works fine but we identified 2 missing features:

1) being able to alter the data just before render it (mustache doesn't do any calculation/logic, it has to be done before)
2) being able to apply some jquery to the generated html

Each of these actions generates a custom event (assign & render). You can write code that "catch" then and do something extra.
I'm using "on" that replaces the "live", ie. that will work even if you add more data using ajax later on and render it with this template. For a simple example like below, a bit overkill

Code: [Select]
<script>
var somedata = {crmAPI entity="contact" action="get"}; // this generates a json formated of 25 contacts
{literal}
cj (function ($) {

   $('#tpl-contacts').on (''assign",function (event, data) { // this is called everytime the template is assigned data
      //do a complex calculation, like the average age, and adds it to the data
      data['average_age'] = "42";
   });
 
   $('#contacts').on ("render", function (event) { //this is called when the list has been generated
      $(this).find('crm-editable').crmEditable(); // and now the first & last name are edit in place
   });


   $('#contacts').crmTemplate ('#tpl-contact',somedata); // generate the list of contacts and displays it
});
</script>

<script type="text/x-templates" id="tpl-contacts">
got {{count}} contact and average age of {{average_age}}
{{#values}}
<li class="crm-entity" id="contact-{{id}}"> <span class="crmf-first_name crm-editable">{{first_name}}</span> <span class="crmf-last_name crm-editable">{{last_name}}</span></li>
{{/values}}
{/literal}

<p>under there I generate (client size) a list of contacts</p>
<div id="#contacts">Will be replaced using the javascript templating</div>



If you want to see a more complex template

http://svn.civicrm.org/civicrm/branches/trunk.profile/templates/CRM/UF/Page/Inline/EditFields.tpl

and the crmTemplate jquery plugin (thin wrapper around mustache)
http://svn.civicrm.org/civicrm/branches/trunk.profile/js/jquery/jquery.crmtemplate.js
« Last Edit: April 19, 2012, 05:27:26 am by xavier »
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • New feature: template client side

This forum was archived on 2017-11-26.