SlideShare a Scribd company logo
1 of 24
Download to read offline
Wednesday, November 2, 2011
ADVANCED TEMPLATES
                              Colin Alworth, Sencha



             colin.alworth@sencha.com             @ambisinister




Wednesday, November 2, 2011
What are Templates?




Wednesday, November 2, 2011
Templates + Data =
                                  HTML




Wednesday, November 2, 2011
Features
       Control statements
       Conditions
       Loops
       String formatting
       Built-in and custom
       Simple expressions
       JavaScript-like scoping
       Bean path syntax




Wednesday, November 2, 2011
Easier to Write, Better
       Performance
       GWT: SafeHtmlTemplates
       2.0: Template and XTemplate
       3.0: XTemplates methods




Wednesday, November 2, 2011
XTemplates in 2.0
          private native String getTemplate() /*-{
         var html = [
         '<p>Name: {name}</p>',
         '<p>Company: {company}</p>',
         '<p>Location: {location}</p>',
         '<p>Salary: {income:currency}</p>',
         '<p>Kids:</p>',
         '<tpl for="kids" if="name=='Darrell Meyer'">',
         '<tpl if="age > 1"><p>{#}. {parent.name}'s kid - {name} - ',
         '{bday:date("M/d/yyyy")}</p></tpl>',
         '</tpl>'
         ];
         return html.join("");
       }-*/;

       //...
       XTemplate tpl = XTemplate.create(getTemplate());  
       panel.removeAll();  
       panel.addText(tpl.applyTemplate(Util.getJsObject(person, 3)));
       panel.layout();



Wednesday, November 2, 2011
XTemplates in 3.0
       <p>Name: {data.name}</p>
       <p>Company: {data.company}</p>
       <p>Location: {data.location}</p>
       <p>Salary: {data.income:currency}</p>
       <p>Kids:</p>
       <tpl for="data.kids">
           <tpl if="age < 100">
               <p>{#}. {parent.name}'s kid - {name} - {bday:date("M/d/yyyy")}</p>
           </tpl>
       </tpl>


       public interface DataRenderer extends XTemplates {
         @XTemplate(source = "template.html")
         public SafeHtml renderTemplate(Person data);
       }

       //...
       panel.clear();
       panel.add(new HTML(renderer.renderTemplate(person)));
       panel.forceLayout();


Wednesday, November 2, 2011
Multiple Templates
       public interface DataRenderer extends XTemplates {
         @XTemplate("<p>Name: {data.name}</p><p>Company: {data.company}</p>
             <p>Location: {data.location}</p>")
         public SafeHtml render(Person data);
        
         @XTemplate(source = "template.html")
         public SafeHtml renderTemplate(Person data);
       }




Wednesday, November 2, 2011
3.0 Compiled Templates
       Template compiled to Java with SafeHtmlTemplates
       Paths translated to getters, or error
       Compiler can optimize Java to JavaScript
       Returns SafeHtml objects




Wednesday, November 2, 2011
The XTemplate
                                Language




Wednesday, November 2, 2011
Control Statements
       <tpl if="condition">
           contents
       </tpl>



       <tpl for="collection">
           properties of item
       </tpl>




Wednesday, November 2, 2011
Variable Scoping
       Scoped like JavaScript, not Java
       Loops can reuse variable names
       If only one argument to template, no need to name it
       parent refers to previously scoped object
       # is a 1-indexed current position in collection




Wednesday, November 2, 2011
public SafeHtml renderTemplate(Person data);




       <tpl for="data.kids">
           <tpl if="age < 100">
               <p>{#}. {parent.name}'s kid - {name} - {bday:date("M/d/yyyy")}</p>
           </tpl>
       </tpl>




       <tpl for="kids">
           <tpl if="age < 100">
               <p>{#}. {data.name}'s kid - {name} - {bday:date("M/d/yyyy")}</p>
           </tpl>
       </tpl>




Wednesday, November 2, 2011
Variable Replacement
       Expressions wrapped in {} turn into getters




Wednesday, November 2, 2011
Customization




Wednesday, November 2, 2011
String Formatting




Wednesday, November 2, 2011
Standard formats
       value:date
       value:date(formatString)
       value:number
       value:number(formatString)
       value:scientific
       value:decimal
       value:currency
       value:percentage




Wednesday, November 2, 2011
Custom Formatters




Wednesday, November 2, 2011
Custom Formatter
       <div class="{style.thumb}"><img src="{photo.path}" title="{photo.name}"></div>
       <span class="x-editable">{photo.name:shorten(18)}</span>


       public class Shorten implements Formatter<String> {
         private int length;

         public Shorten(int length) {
           this.length = length;
         }
         @Override
         public String format(String data) {
           return Format.ellipse(data, length);
         }
       }




Wednesday, November 2, 2011
Custom Formatter
       @FormatterFactories(@FormatterFactory(factory = ShortenFactory.class, name =
             "shorten"))
       interface Renderer extends XTemplates {
         @XTemplate(source = "ListViewExample.html")
         public SafeHtml renderItem(Photo photo, Style style);
       }

       public class Shorten implements Formatter<String> {
         private int length;

         public Shorten(int length) {
           this.length = length;
         }
         @Override
         public String format(String data) {
           return Format.ellipse(data, length);
         }
       }

       public class ShortenFactory {
         public static Shorten getFormat(int length) {
           return new Shorten(length);
         }
       }
Wednesday, November 2, 2011
Nesting Templates
       public interface Template extends XTemplates {
         @XTemplate(source = "DivFrame.html")
         SafeHtml render(DivFrameStyle style, SafeHtml content);
       }

       <div class="{style.contentArea}">
         <div class="{style.content}">{content}</div>
         <div class="{style.topLeft}"></div>
         <div class="{style.top}"></div>
         <div class="{style.topRight}"></div>
         <div class="{style.bottomLeft}"></div>
         <div class="{style.bottom}"></div>
         <div class="{style.bottomRight}"></div>
         <div class="{style.left}"></div>
         <div class="{style.right}"></div>
       </div>




Wednesday, November 2, 2011
Questions?




Wednesday, November 2, 2011
Thank you!




Wednesday, November 2, 2011

More Related Content

What's hot

Symfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 IntegrationSymfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 Integration
Jonathan Wage
 
Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1
Kevin Octavian
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
Syed Shahul
 

What's hot (18)

Kotlin Data Model
Kotlin Data ModelKotlin Data Model
Kotlin Data Model
 
Symfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 IntegrationSymfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 Integration
 
Hibernate Mapping
Hibernate MappingHibernate Mapping
Hibernate Mapping
 
DITA 1.3 Keyscopes
DITA 1.3 KeyscopesDITA 1.3 Keyscopes
DITA 1.3 Keyscopes
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
 
High performance JPA with Oracle Coherence
High performance JPA with Oracle CoherenceHigh performance JPA with Oracle Coherence
High performance JPA with Oracle Coherence
 
Locators groups 1_0_2
Locators groups 1_0_2Locators groups 1_0_2
Locators groups 1_0_2
 
Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1
 
Kick Start Jpa
Kick Start JpaKick Start Jpa
Kick Start Jpa
 
Metaworks3
Metaworks3Metaworks3
Metaworks3
 
Smart Join Algorithms for Fighting Skew at Scale
Smart Join Algorithms for Fighting Skew at ScaleSmart Join Algorithms for Fighting Skew at Scale
Smart Join Algorithms for Fighting Skew at Scale
 
DOM and Events
DOM and EventsDOM and Events
DOM and Events
 
Backendless apps
Backendless appsBackendless apps
Backendless apps
 
Data binding в массы! (1.2)
Data binding в массы! (1.2)Data binding в массы! (1.2)
Data binding в массы! (1.2)
 
3
33
3
 
Android - Saving data
Android - Saving dataAndroid - Saving data
Android - Saving data
 
Final_Project
Final_ProjectFinal_Project
Final_Project
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 

Similar to Ext GWT 3.0 Advanced Templates

Mike hostetler - jQuery knowledge append to you
Mike hostetler - jQuery knowledge append to youMike hostetler - jQuery knowledge append to you
Mike hostetler - jQuery knowledge append to you
StarTech Conference
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
Sudharsan S
 

Similar to Ext GWT 3.0 Advanced Templates (20)

Ext GWT 3.0 Theming and Appearances
Ext GWT 3.0 Theming and AppearancesExt GWT 3.0 Theming and Appearances
Ext GWT 3.0 Theming and Appearances
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
XML Support: Specifications and Development
XML Support: Specifications and DevelopmentXML Support: Specifications and Development
XML Support: Specifications and Development
 
XML.ppt
XML.pptXML.ppt
XML.ppt
 
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
 
Struts2
Struts2Struts2
Struts2
 
Broadleaf Presents Thymeleaf
Broadleaf Presents ThymeleafBroadleaf Presents Thymeleaf
Broadleaf Presents Thymeleaf
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Jquery 2
Jquery 2Jquery 2
Jquery 2
 
Mike hostetler - jQuery knowledge append to you
Mike hostetler - jQuery knowledge append to youMike hostetler - jQuery knowledge append to you
Mike hostetler - jQuery knowledge append to you
 
Xml11
Xml11Xml11
Xml11
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
Xml
XmlXml
Xml
 
Building Non-shit APIs with JavaScript
Building Non-shit APIs with JavaScriptBuilding Non-shit APIs with JavaScript
Building Non-shit APIs with JavaScript
 
DB2 Native XML
DB2 Native XMLDB2 Native XML
DB2 Native XML
 
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web TechnologyInternet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
 
AdvancedXPath
AdvancedXPathAdvancedXPath
AdvancedXPath
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Loving.Net Project Portfolio for Roger Loving
.Net Project Portfolio for Roger Loving
 
Xml
XmlXml
Xml
 

More from Sencha

More from Sencha (20)

Breathe New Life into Your Existing JavaScript Applications with Web Components
Breathe New Life into Your Existing JavaScript Applications with Web ComponentsBreathe New Life into Your Existing JavaScript Applications with Web Components
Breathe New Life into Your Existing JavaScript Applications with Web Components
 
Ext JS 6.6 Highlights
Ext JS 6.6 HighlightsExt JS 6.6 Highlights
Ext JS 6.6 Highlights
 
Sencha Roadshow 2017: BufferedStore Internals featuring eyeworkers interactiv...
Sencha Roadshow 2017: BufferedStore Internals featuring eyeworkers interactiv...Sencha Roadshow 2017: BufferedStore Internals featuring eyeworkers interactiv...
Sencha Roadshow 2017: BufferedStore Internals featuring eyeworkers interactiv...
 
Sencha Roadshow 2017: Build Progressive Web Apps with Ext JS and Cmd
Sencha Roadshow 2017: Build Progressive Web Apps with Ext JS and Cmd Sencha Roadshow 2017: Build Progressive Web Apps with Ext JS and Cmd
Sencha Roadshow 2017: Build Progressive Web Apps with Ext JS and Cmd
 
Sencha Roadshow 2017: Best Practices for Implementing Continuous Web App Testing
Sencha Roadshow 2017: Best Practices for Implementing Continuous Web App TestingSencha Roadshow 2017: Best Practices for Implementing Continuous Web App Testing
Sencha Roadshow 2017: Best Practices for Implementing Continuous Web App Testing
 
Sencha Roadshow 2017: What's New in Sencha Test
Sencha Roadshow 2017: What's New in Sencha TestSencha Roadshow 2017: What's New in Sencha Test
Sencha Roadshow 2017: What's New in Sencha Test
 
Sencha Roadshow 2017: Sencha Upgrades - The Good. The Bad. The Ugly - Eva Luc...
Sencha Roadshow 2017: Sencha Upgrades - The Good. The Bad. The Ugly - Eva Luc...Sencha Roadshow 2017: Sencha Upgrades - The Good. The Bad. The Ugly - Eva Luc...
Sencha Roadshow 2017: Sencha Upgrades - The Good. The Bad. The Ugly - Eva Luc...
 
Sencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
Sencha Roadshow 2017: Modernizing the Ext JS Class System and ToolingSencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
Sencha Roadshow 2017: Modernizing the Ext JS Class System and Tooling
 
Sencha Roadshow 2017: Sencha Best Practices: Coworkee App
Sencha Roadshow 2017: Sencha Best Practices: Coworkee App Sencha Roadshow 2017: Sencha Best Practices: Coworkee App
Sencha Roadshow 2017: Sencha Best Practices: Coworkee App
 
Sencha Roadshow 2017: Mobile First or Desktop First
Sencha Roadshow 2017: Mobile First or Desktop FirstSencha Roadshow 2017: Mobile First or Desktop First
Sencha Roadshow 2017: Mobile First or Desktop First
 
Sencha Roadshow 2017: Innovations in Ext JS 6.5 and Beyond
Sencha Roadshow 2017: Innovations in Ext JS 6.5 and BeyondSencha Roadshow 2017: Innovations in Ext JS 6.5 and Beyond
Sencha Roadshow 2017: Innovations in Ext JS 6.5 and Beyond
 
Leveraging React and GraphQL to Create a Performant, Scalable Data Grid
Leveraging React and GraphQL to Create a Performant, Scalable Data GridLeveraging React and GraphQL to Create a Performant, Scalable Data Grid
Leveraging React and GraphQL to Create a Performant, Scalable Data Grid
 
Learn Key Insights from The State of Web Application Testing Research Report
Learn Key Insights from The State of Web Application Testing Research ReportLearn Key Insights from The State of Web Application Testing Research Report
Learn Key Insights from The State of Web Application Testing Research Report
 
Introducing ExtReact: Adding Powerful Sencha Components to React Apps
Introducing ExtReact: Adding Powerful Sencha Components to React AppsIntroducing ExtReact: Adding Powerful Sencha Components to React Apps
Introducing ExtReact: Adding Powerful Sencha Components to React Apps
 
SenchaCon 2016: Keynote Presentation - Art Landro, Gautam Agrawal, Mark Brocato
SenchaCon 2016: Keynote Presentation - Art Landro, Gautam Agrawal, Mark BrocatoSenchaCon 2016: Keynote Presentation - Art Landro, Gautam Agrawal, Mark Brocato
SenchaCon 2016: Keynote Presentation - Art Landro, Gautam Agrawal, Mark Brocato
 
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
 
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
 
SenchaCon 2016: Expect the Unexpected - Dealing with Errors in Web Apps
SenchaCon 2016: Expect the Unexpected - Dealing with Errors in Web AppsSenchaCon 2016: Expect the Unexpected - Dealing with Errors in Web Apps
SenchaCon 2016: Expect the Unexpected - Dealing with Errors in Web Apps
 
Ext JS Architecture Best Practices - Mitchell Simeons
Ext JS Architecture Best Practices - Mitchell SimeonsExt JS Architecture Best Practices - Mitchell Simeons
Ext JS Architecture Best Practices - Mitchell Simeons
 
SenchaCon 2016: Mobile First? Desktop First? Or Should you Think Universal Ap...
SenchaCon 2016: Mobile First? Desktop First? Or Should you Think Universal Ap...SenchaCon 2016: Mobile First? Desktop First? Or Should you Think Universal Ap...
SenchaCon 2016: Mobile First? Desktop First? Or Should you Think Universal Ap...
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Ext GWT 3.0 Advanced Templates

  • 2. ADVANCED TEMPLATES Colin Alworth, Sencha colin.alworth@sencha.com @ambisinister Wednesday, November 2, 2011
  • 4. Templates + Data = HTML Wednesday, November 2, 2011
  • 5. Features Control statements Conditions Loops String formatting Built-in and custom Simple expressions JavaScript-like scoping Bean path syntax Wednesday, November 2, 2011
  • 6. Easier to Write, Better Performance GWT: SafeHtmlTemplates 2.0: Template and XTemplate 3.0: XTemplates methods Wednesday, November 2, 2011
  • 7. XTemplates in 2.0 private native String getTemplate() /*-{   var html = [   '<p>Name: {name}</p>',   '<p>Company: {company}</p>',   '<p>Location: {location}</p>',   '<p>Salary: {income:currency}</p>',   '<p>Kids:</p>',   '<tpl for="kids" if="name=='Darrell Meyer'">',   '<tpl if="age > 1"><p>{#}. {parent.name}'s kid - {name} - ', '{bday:date("M/d/yyyy")}</p></tpl>',   '</tpl>'   ];   return html.join(""); }-*/; //... XTemplate tpl = XTemplate.create(getTemplate());   panel.removeAll();   panel.addText(tpl.applyTemplate(Util.getJsObject(person, 3))); panel.layout(); Wednesday, November 2, 2011
  • 8. XTemplates in 3.0 <p>Name: {data.name}</p> <p>Company: {data.company}</p> <p>Location: {data.location}</p> <p>Salary: {data.income:currency}</p> <p>Kids:</p> <tpl for="data.kids"> <tpl if="age < 100"> <p>{#}. {parent.name}'s kid - {name} - {bday:date("M/d/yyyy")}</p> </tpl> </tpl> public interface DataRenderer extends XTemplates {   @XTemplate(source = "template.html")   public SafeHtml renderTemplate(Person data); } //... panel.clear(); panel.add(new HTML(renderer.renderTemplate(person))); panel.forceLayout(); Wednesday, November 2, 2011
  • 9. Multiple Templates public interface DataRenderer extends XTemplates {   @XTemplate("<p>Name: {data.name}</p><p>Company: {data.company}</p> <p>Location: {data.location}</p>")   public SafeHtml render(Person data);     @XTemplate(source = "template.html")   public SafeHtml renderTemplate(Person data); } Wednesday, November 2, 2011
  • 10. 3.0 Compiled Templates Template compiled to Java with SafeHtmlTemplates Paths translated to getters, or error Compiler can optimize Java to JavaScript Returns SafeHtml objects Wednesday, November 2, 2011
  • 11. The XTemplate Language Wednesday, November 2, 2011
  • 12. Control Statements <tpl if="condition"> contents </tpl> <tpl for="collection"> properties of item </tpl> Wednesday, November 2, 2011
  • 13. Variable Scoping Scoped like JavaScript, not Java Loops can reuse variable names If only one argument to template, no need to name it parent refers to previously scoped object # is a 1-indexed current position in collection Wednesday, November 2, 2011
  • 14. public SafeHtml renderTemplate(Person data); <tpl for="data.kids"> <tpl if="age < 100"> <p>{#}. {parent.name}'s kid - {name} - {bday:date("M/d/yyyy")}</p> </tpl> </tpl> <tpl for="kids"> <tpl if="age < 100"> <p>{#}. {data.name}'s kid - {name} - {bday:date("M/d/yyyy")}</p> </tpl> </tpl> Wednesday, November 2, 2011
  • 15. Variable Replacement Expressions wrapped in {} turn into getters Wednesday, November 2, 2011
  • 18. Standard formats value:date value:date(formatString) value:number value:number(formatString) value:scientific value:decimal value:currency value:percentage Wednesday, November 2, 2011
  • 20. Custom Formatter <div class="{style.thumb}"><img src="{photo.path}" title="{photo.name}"></div> <span class="x-editable">{photo.name:shorten(18)}</span> public class Shorten implements Formatter<String> {   private int length;   public Shorten(int length) {     this.length = length;   }   @Override   public String format(String data) {     return Format.ellipse(data, length);   } } Wednesday, November 2, 2011
  • 21. Custom Formatter @FormatterFactories(@FormatterFactory(factory = ShortenFactory.class, name = "shorten")) interface Renderer extends XTemplates {   @XTemplate(source = "ListViewExample.html")   public SafeHtml renderItem(Photo photo, Style style); } public class Shorten implements Formatter<String> {   private int length;   public Shorten(int length) {   this.length = length;   }   @Override   public String format(String data) {     return Format.ellipse(data, length);   } } public class ShortenFactory {   public static Shorten getFormat(int length) {     return new Shorten(length);   } } Wednesday, November 2, 2011
  • 22. Nesting Templates public interface Template extends XTemplates { @XTemplate(source = "DivFrame.html") SafeHtml render(DivFrameStyle style, SafeHtml content); } <div class="{style.contentArea}"> <div class="{style.content}">{content}</div> <div class="{style.topLeft}"></div> <div class="{style.top}"></div> <div class="{style.topRight}"></div> <div class="{style.bottomLeft}"></div> <div class="{style.bottom}"></div> <div class="{style.bottomRight}"></div> <div class="{style.left}"></div> <div class="{style.right}"></div> </div> Wednesday, November 2, 2011