SlideShare ist ein Scribd-Unternehmen logo
1 von 59
Downloaden Sie, um offline zu lesen
RubyKaigi 2010



How to Make Designer-Friendly
Template Engine
- Keep Template as pure HTML -

                                                    makoto kuwata
                                        http://www.kuwata-lab.com/




                 copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                          1
README.txt

 Goal
          ✤   For template engine users: get
              correct knowledge
          ✤   For template engine desingers: get
              an ideal solution

 Motiv.   ✤   Want correct wrong explanations
              related to template engine
          ✤   Want to insist that eRuby is not an
              ideal solution

 Target   ✤   Template engine users and designers

                copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                        2
I must apologize...
✤   I would talk about...
    ✤   How to keep html design of template files
    ✤   What are performance bottole necks of engine
    ✤   What is wrong about existing template engines
    ✤   How to design engine which is designer-friendly,
        very fast, and easy to implement in any languages

✤   But I only have 30 minutes
    ✤   I can talk about the first issue

                       copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                               3
Reference
✤   Rubyist Magazine - Guide to Template System
    ✤   http://jp.rubyist.net/magazine/?0024-TemplateSystem
    ✤   http://jp.rubyist.net/magazine/?0024-TemplateSystem2




                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              4
Agenda
✤   Fundamental section:
    ✤   Business Layer and Presentation Layer
    ✤   Existing Issues and the Cause
    ✤   Think about Presentation Logic

✤   Kwartz section:
    ✤   Introduction to Kwartz template engine
    ✤   Presentation Logic Pattern


                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              5
Fundamental section:
Business Layer & Presentation Layer




             copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                     6
Business layer and Presentation layer
✤   Business layer
    ✤   WHAT should be displayed?
    ✤   by main program

✤   Presentation layer
    ✤   HOW to display it?
    ✤   by template engine, template file




                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              7
Data and Logic
✤   Both layers have each data and logic


               Business layer                              Presentation layer
               (main program)                                  (template engine)


       Data    Business Data                                Presentation Data


       Logic   Business Logic                              Presentation Logic


               separated by template engine
                 copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                   8
Applicaiton Example
                                           Business logic
✤   Select top 20

✤   of book sales                           Business data

✤   by <table> tag                                     Presentaion
                                                       data
✤   with toggling bgcolor

              Presentation
              logic

                copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                        9
Frequent Misunderstandings (1)

Mis:     ✤   Template engine separates
             presentation from logic.

Truth:   ✤   Template engine separatin
             presentation layer from business
             layer.
             ✤   Presentation layer have own
                 logics.

                                               common mistake in Japan...
                   copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            10
Frequent Misunderstandings (2)

Mis:     ✤   Presentation layer shouldn't
             contain any logics.

Truth:   ✤   Presentation layer should contain
             presentation logics.
             ✤   "HTML template shouldn't contain any
                 presentation logics" and "Layers should
                 be separated" are different things.




                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            11
Add-up
✤   Business layer
    ✤   WHAT should be displayed?
    ✤   By main program

✤   Presentation layer
    ✤   HOW to display it?
    ✤   By template engine and template file

✤   Both layers have each data and logics

                     copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                             12
Fundamental section:
Existing Issues and the Cause




             copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                     13
Templates Annoys Designers
        ProjectA: Rails
 <table>                                                             ProjectC: PHP
 <%	 @arr.each	 {	 %>                                         <table>
 	 	 ....                                                     <?php	 foreach()	 {	 ?>
 <%	 }	 %>                                                    	 	 ....
 </table>                                                     <?php	 }	 ?>
                                                              </table>
             ProjectB: JSP
      <table>
      <forEach	 items="">
                                                              Designer: Why I must
      	 	 ....
                                                              master so much langs?
      </forEach>
      </table>
                  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                        14
Edit & Update the same file
           template file
           <table>
           <%	 @arr.each	 {	 %>
           	 	 ....
           <%	 }	 %>
           </table>
                     Conflict!
                                                              edit presentaion
   edit HTML                                                  logic


  Designer                                                    Programmer

               copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                 15
The Cause of Issues

 ✤   It is the cause that                                                    template file
     presentation logics                            <table>
     are embedded in                                <%	 i	 =	 0	 %>
                                                    <%	 @arr.eachx	 do	 |x|	 %>
     template file
                                                    <%	 	 	 i	 +=	 i	 %>
                                                    <%	 	 	 c	 =	 i.odd	 ?	 	 %>
                                                    <%	 	 	 	 	 	 	 'odd':'even'	 %>
     pure HTML is the                               	 	 <tr	 class="<%=c%>">
     solution                                       	 	 	 	 <td><%=	 x	 %></td>
                                                    	 	 </tr>
                                                    <%	 end	 %>
                                                    </table>

                     copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                            16
Pure HTML Template is Great
                   ✤   Easy to preview by browsers
Designer
             for


                   ✤   No need to learn a lot of template langs!
                   ✤   Easy to validate by HTML validator
                   ✤   Allow to use their favorite HTML editor


                       Avoid edit confliction
Programmer
             for




                   ✤


                   ✤   Remove whitespaces to reduce traffic
                   ✤   Transform template files for mobile phones

                              copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                      17
The Question which bothered Matz
✤   So, where is presentation logics?


    Matz diary (2004-08-24)
    http://www.rubyist.net/ matz/20040824.html#p01

      (Summary)
      When view layer needs some logics,
      where should I put it in?
      I don't want it to be in HTML template...


                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            18
Add-up
✤   Presentation logics in template files
    causes a lot of problems
✤   Pure HTML template is great
✤   Where is presentation logics?




                copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                        19
Fundamenta section:
Think about Presentaion Logic




            copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                    20
Where should be Presentation Logics?

 A. In template file (eRuby, JSP, Kid)
       Presentation Logic
                                                             Business Logic & Data
       + Presentaion Data


 B. In main program (Amrita2, XMLC, HTML::Template)
                                                            Presentation Logic
        Presentation Data
                                                          +Business Logic & Data


 C. In independed file (Kwartz, Tapestry, Mayaa)
        Presentation Logic
                                                            Business Logic & Data
        Presentation Data

                                               template file               main program
                   copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                          21
A. In Template File (1)

 ex: eRuby (Ruby)
  <table>
  <%	 odd	 =	 false	 %>
  <%	 for	 item	 in	 @list	 %>
  <%	 	 	 odd	 =	 !	 odd
  <%	 	 	 cls	 =	 odd	 ?	 'odd'	 :	 'even'	 %>
  	 	 <tr	 class="<%=	 cls	 %>">
  	 	 	 	 <td><%=h	 item	 %></td>
  	 	 </tr>
  <%	 end	 %>                         •HTML and non-HTML
  </table>                             are mixed
                                                       → never pure HTML !


                  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                             22
A. In Template File (2)

 ex: JSP with custom tags (Java)
  <table>
  	 <c:forEach	 var="item"	 items="${list}"
  	 	 	 	 	 	 	 	 	 	 	 	 varStatus="loop">
  	 	 <c:set	 var="klass"
  	 	 	 	 	 value="${loop.count%2==0?'odd':'even'}"	 />
  	 	 <tr	 class="${klass}">
  	 	 	 	 <td><c:out	 value="${item}"	 /></td>
  	 	 </tr>
  	 </c:forEach>                •Even if you use custom tags,
  </table>                       presentation logics are mixed into
                                HTML (just same as eRuby).

                     copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                             23
A. In Template File (3)

 ex: Kid template engine (Python)
  <table>
  	 	 <tr	 py:for="i,	 item	 in	 enumerate(items)"
  	 	 	 	 	 	 class="${i	 %	 2	 and	 'even'	 or	 'odd'}">
  	 	 	 	 <td	 py:content="item">dummy</td>
  	 	 </tr>
  </table>
                  •Using HTML tags and attributess, Kid
                   aquired designer-friendly template!
                  •But notice that presentation logics are
                   still embedded into templates


                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            24
A. In Template File (4)
 Pros.
         ✤   Presentation layer (= template file) is
             compretely separated from business layer.
         ✤   Easy to uderstand, easy to implement


             Hard to keep pure HTML
 Cons.




         ✤


         ✤   Designer and programmer will edit the
             same file at the same time, and conflicted
             in the result
         ✤   Designer may break presentation logics in
             template files accidentally
                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            25
Where should be Presentation Logics?

 A. In template file (eRuby, JSP, Kid)
       Presentation Logic
                                                             Business Logic & Data
       + Presentaion Data


 B. In main program (Amrita2, XMLC, HTML::Template)
                                                            Presentation Logic
        Presentation Data
                                                          +Business Logic & Data


 C. In independed file (Kwartz, Tapestry, Mayaa)
        Presentation Logic
                                                            Business Logic & Data
        Presentation Data

                                               template file               main program
                   copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                          26
B. In Main Program (1)

 ex: Amrita2 temlate file (Ruby)
  <table>
  	 	 <tr	 id="list"	 class="odd">
  	 	 	 	 <td	 id="item">dummy</td>
  	 	 </tr>
  </table>

                 •Just to place "mark" (= id attr in
                  Amrita2) into template file
                 •Pure HTML template (because no
                  presentation logics in it)



                  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                          27
B. In Main Program (2)
 ex: Amrita2 main program (Ruby)
  ##	 business	 data	 to	 display
  list	 =	 [	 'A',	 'B',	 'C'	 ]         •Necessary to "process"
  ##	 presentation	 logic                 bussiness data for
  list2	 =	 [];	 	 odd	 =	 false          presentation layer
  for	 x	 in	 list
  	 	 cls	 =	 (odd	 =	 !odd)	 ?	 'odd'	 :	 'even'
  	 	 item2	 =	 a(:class=>cls)	 {	 {:item=>x}	 }
  	 	 list2	 <<	 item2
  end
  context	 =	 {	 :list=>list2	 }
  ##	 read	 template	 and	 render	 with	 data
  tmpl	 =	 Amrita2::TemplateFile.new('ex1.html')
  tmpl.expand(html='',	 context)
  print	 html
                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            28
B. In Main Program (3)
 Pros.
         ✤   Just embed "mark" into template
         ✤   pure HTML template (in most cases)
         ✤   No conflicts when editing files
         ✤   No accidental change of logics by designer
 Cons.




         ✤   Business layer and Presentation layer are
             NOT separated! (*)
         ✤   Confusable usage (because logic should be
             represented by data)

                     (*) devisable to avoid (ex. define dedicated class)
                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            29
Where should be Presentation Logics?

 A. In template file (eRuby, JSP, Kid)
       Presentation Logic
                                                             Business Logic & Data
       + Presentaion Data


 B. In main program (Amrita2, XMLC, HTML::Template)
                                                            Presentation Logic
        Presentation Data
                                                          +Business Logic & Data


 C. In independed file (Kwartz, Tapestry, Mayaa)
        Presentation Logic
                                                            Business Logic & Data
        Presentation Data

                                               template file               main program
                   copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                          30
C. In Independed File (1)

 ex: Kwartz presentation data file (Ruby)
  <table>
  	 	 <tr	 id="mark:list"	 class="odd">
  	 	 	 	 <td	 id="mark:item">dummy</td>
  	 	 </tr>
  </table>
                 •Just place "mark" (= id attr in
                   Kwartz) in template file




                  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                          31
C. In Independent File (2)

 ex: Kwartz presentation logic file (Ruby)
  #item	 {	 	 	 	 	 	 	 	 	 	 ##	 id="mark:item"
  	 	 value:	 x;	 	 	 	 	 	 ##	 display	 value	 of	 x
  }
  #list	 {	 	 	 	 	 	 	 	 	 	 ##	 id="mark:list"
  	 	 logic:	 {	 	 	 	 	 	 	 ##	 iterate	 over	 element
  	 	 	 	 for	 x	 in	 @list
  	 	 	 	 	 	 _elem
  	 	 	 	 end
  	 	 }                         •Write your presentation logics
  }
                                 just like CSS
                                •No presentation logics in HTML !
                     copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                             32
CSS
                   (Visual Design)




                           HTML
                         (Document
                         Structure)
 Kwartz                                                            JavaScript
(Presentation                                                           (Client-side
    Logic)                                                                 Logic)


                copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                       33
C. In Independent File (3)
 Pros.
         ✤   Business layer and Presentation layer are
             completely separated!
         ✤   Presentation data (=HTML) and
             Presentation logic are also separated!
         ✤   Pure HTML template
         ✤   No conflicts when editiong files
         ✤   No accidental modification of logics
 Cons.




         ✤   Number of files are increased

                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            34
Important thing, More Important thing
✤   Important thing
    ✤   Template format is pure HTML

✤   More important thing
    ✤   Presentation logic should be separated (or
        independent) from others!
    ✤   Pure HTML template is just derivation from it




                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              35
Frequent Misunderstanding (1)

Mis.:    ✤   My template is pure HTML, so
             presentation layer is separated!


Truth:   ✤   "Template is pure HTML" doesn't
             mean "Presentation layer is
             separated"
             ✤   Probably she/he misunderstands "No
                 logics in template" as "Presentation layer
                 is separated"

                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              36
Frequent Misunderstanding (2)

Mis.:    ✤   Web designer may write buggy
             logic in raw PHP, so I introduced
             Smarty!

Truth:   ✤   Smarty can't prevent buggy logic
             nor accidental logic modification.
             ✤   The root cause of the problem is that
                 desginer can access to presention logic
             ✤   A radical solution is to separate
                 presentation logic from template file

                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              37
Add-up
✤   Presentation logic can be...
    ✤   In template file (non-html, conflictions)
    ✤   In main program (layers are not separated)
    ✤   In independent file (ideal)

✤   Pure HTML template is important
✤   Separation of presentation logic is
    more important

                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              38
Kwartz section:
Introduction to Kwartz




             copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                     39
Kwartz Overview
✤   What is Kwartz?
    ✤   A template engine which can separate
        Presentation logic from Presentation data
        (=HTML)
    ✤   Pure HTML template
    ✤   Implemented in Ruby, PHP
    ✤   http://www.kuwata-lab.com/kwartz/




                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              40
Example: Presentation Data
                                Add "marking" (=id attr)
                                to where you want to
 ex.html                        manipulate dinamically
  <table>
  	 	 <tr	 id="mark:list">
  	 	 	 	 <td	 id="mark:item">Foo</td>
  	 	 </tr>
  	 	 <tr	 id="dummy:d1">
  	 	 	 	 <td>Bar</td>
  	 	 </tr>
  </table>                 `id="dummy:xxx"' means
                                  dummy elelment



                  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                          41
Example: Presentation Logic

 ex.plogic                  Write presentation logic
  #list	 {                  for each elements
  	 	 logic:	 {
  	 	 	 	 for	 x	 in	 @list
  	 	 	 	 	 	 _stag	 	 	 	 ##	 start-tag
  	 	 	 	 	 	 _cont	 	 	 	 ##	 content
  	 	 	 	 	 	 _etag	 	 	 	 ##	 end-tag
  	 	 	 	 end
  	 	 }                 Repeat <tr> element
  }
  #item	 {
  	 	 value:	 x;
                             Display value of x as
  }
                            content of <td> element
                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              42
Example: Compilation

 Compile files into a eRuby file
 $	 kwartz	 -l	 eruby	 -p	 ex.plogic	 ex.html	 >	 ex.rhtml

 ex.rhtml (compiled by Kwartz)
 <table>
 <%	 	 	 	 	 for	 x	 in	 @list	 %>
 	 	 <tr>                          •Attribute `id="mark:xxx"' is
 	 	 	 	 <td><%=	 x	 %></td>        removed automatically
 	 	 </tr>                          (not removed if `id="xxx"')
 <%	 	 	 	 	 end	 %>
 </table>                          •Element which has
                                                   `id="dummy:xxx"' is also
                                                   removed
                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              43
Example: Main Program

 Main program
 ##	 business	 data	 to	 be	 displayed
 list	 =	 ['A',	 'B',	 'C']
 context	 =	 {	 :list	 =>	 list	 }

 ##	 load	 template	 and	 render	 it	 with	 data
 require	 'erubis'
 eruby	 =	 Erubis::Eruby.load_file('ex.rhtml')
 html	 =	 eruby.evaluate(context)
 print	 html
                                              Main program doesn't
                                              require Kwartz

                  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                          44
Pros and Cons
✤   Pros.   ✤   pure HTML template
            ✤   Very fast
            ✤   Supports eRuby, PHP, JSP, and so on
            ✤   Available with non-HTML text file


✤   Cons.   ✤   Messy compile (should be automated)
            ✤   Runtime error is reported with line
                number AFTER compiled code, not
                original source code

                    copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                            45
Practical Examples (1)

 repeat over element                                 repeat only content
  #list	 {                                            #list	 {
  	 	 logic:	 {                                       	 	 logic:	 {
  	 	 	 	 for	 item	 in	 @list                        	 	 	 	 _stag
  	 	 	 	 	 	 _stag	 #	 start-tag                     	 	 	 	 for	 item	 in	 @list
  	 	 	 	 	 	 _cont	 #	 content                       	 	 	 	 	 	 _cont
  	 	 	 	 	 	 _etag	 #	 end-tag                       	 	 	 	 end
  	 	 	 	 end                                         	 	 	 	 _etag
  	 	 }                                               	 	 }
  }                                                   }             Useful to repeat
                                                                              <dt> and <dd>



                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                              46
Practical Examples (2)

 display only when                                   display default value
  #list	 {                                            #list	 {
  	 	 logic:	 {                                       	 	 logic:	 {
  	 	 	 	 if	 @list.size	 >	 0                        	 	 	 	 _stag
  	 	 	 	 	 	 _stag                                   	 	 	 	 if	 @name.blank?
  	 	 	 	 	 	 _cont                                   	 	 	 	 	 	 print('World')
  	 	 	 	 	 	 _etag                                   	 	 	 	 else
  	 	 	 	 end                                         	 	 	 	 	 	 _cont
  	 	 }                                               	 	 	 	 end
  }                                                   	 	 	 	 _etag
                                                      	 	 }
                                                      }

                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                   47
Practical Examples (3)

 remove dummy tag                                remove dummy element
  #list	 {                                        #list	 {
  	 	 logic:	 {                                   	 	 logic:	 {
  	 	 	 	 #_stag                                  	 	 }
  	 	 	 	 _cont                                   }
  	 	 	 	 #_etag
  	 	 }                                           replace by other
  }
                                                    #list	 {
                                                    	 	 logic:	 {
                                                    	 	 	 	 _element(list2)
                                                    	 	 }
                                                    }            reuse a certain
                                                                           element
                   copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                     48
Practical Examples (4)

 より複雑なプレゼンテーションロジック
  #list	 {
  	 	 attrs:	 "class"	 cls;	 	 #	 attribue
  	 	 logic:	 {
  	 	 	 	 odd	 =	 false               Presentation logic is
  	 	 	 	 for	 x	 in	 @list           separated from HTML file
  	 	 	 	 	 	 odd	 =	 !odd
  	 	 	 	 	 	 cls	 =	 odd	 ?	 'odd'	 :	 'even'
  	 	 	 	 	 	 _elem
  	 	 	 	 end                     No need to touch HTML at all
  	 	 }                           even if you change presentaion
  }                               logic dramatically

                     copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                             49
Add-up
✤   Kwartz tempate engine
    ✤   Separates presentation logics from presentation
        data (=HTML)


    ✤   Pure HTML Template
    ✤   No need to touch HTML file at all even if you
        change presentation logics




                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              50
Kwartz section:
Presentation Pattern




             copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                     51
Overview
✤   What is Presentation Pattern?
    ✤   Best practices in presentation layer (data & logic)
    ✤   (Notice that this is my original term and not
        popular at all)
    ✤   Very easy to understand, compared to GoF!
    ✤   http://www.kuwata-lab.com/kwartz/kwartz3ruby-
        pattern-catalog.html




                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              52
List of Patterns
✤   Replacement                                      ✤    Iteration
    ✤   Replace Element with                               ✤    Iterate Element Pattern
        Value Pattern                                      ✤
                                                                Iterate Content Pattern
    ✤   Replace Content with
        Value Pattern                                ✤    Selection
    ✤   Default Content Pattern                            ✤    Select Element/Content
    ✤   Replace Element with                                    Pattern
        Element/Content Pattern                            ✤    Pick-up Element/Content
    ✤   Replace Content with                                    Pattern
        Element/Content Pattern                            ✤    Extract Element/Content
                                                                Pattern
✤   Deletion
    ✤   Delete Element Pattern                                                  Today's menu
    ✤   Delete Tag Pattern
                        copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                               53
Select Element Pattern
✤   Req: Want to change data according to condition

    <div	 id="mark:message">                         #message	 {
    	 	 <div	 id="error">                            	 	 logic:	 {
    	 	 	 	 ERROR!                                   	 	 	 	 if	 status	 ==	 'error'
    	 	 </div>                                       	 	 	 	 	 	 _element(error)
    	 	 <div	 id="warning">                          	 	 	 	 elsif	 status	 ==	 'warn'
    	 	 	 	 Warning:                                 	 	 	 	 	 	 _element(warning)
    	 	 </div>                                       	 	 	 	 else
    	 	 <div	 id="noerror">                          	 	 	 	 	 	 _element(noerror)
    	 	 	 	 No	 error.                               	 	 	 	 end
    	 	 </dvi>                                       	 	 }            Select an elem
                   Add different id                                     according to
    </div>                                           }
                for each elems                                                   condition
                         copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                             54
Pick-up Element Pattern
✤   Req: Want to include a lot of dummy elems (for preview)

    <ol	 id="mark:list">                          #list	 {
    	 	 <li	 id="mark:item">                      	 	 logic:	 {
    	 	 	 	 Item1                                 	 	 	 	 _stag
    	 	 </li>      add id to                      	 	 	 	 _element(item)
    	 	 <li>Item2</li>
                   non-dummy                      	 	 	 	 _etag
    	 	 <li>Item3</li>
                   elements                       	 	 }
    	 	 <li>Item4</li>                            }
                                                                    Pick up only non-
    	 	 <li>Item5</li>                                              dummy elems, and
    	 	 <li>Item6</li>                                              dummy elems are
    </ol>
                                                                    removed in the result


                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                            55
Extract Element Pattern
✤   Req: Want to extract a certain elem form a document

    <html	 id="mark:whole">                          #whole	 {
    	 <body>                                         	 	 logic:	 {
    	 	 <form	 id="form">                            	 	 	 	 _element(form)
    	 	 	 <input	 ...	 />                            	 	 }
                                                                  Replace document
    	 	 	 <input	 ...	 />                            }
                                                                                 by a certain elem
    	 	 	 <input	 ...	 />
    	 	 </form>                                      #DOCUMENT	 {
    	 </body>                                        	 	 logic:	 {
    </html>       Add id attr to                     	 	 	 	 _element(form)
                document root                        	 	 }
                elem (=<html>)                              Kwartz provides special
                                                     }
                                                               id name for this purpose
                         copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                                                     56
Add-up
✤   Presentation pattern
    ✤   Best practices for presentation layer (data & logic)
    ✤   Select Element Pattern:
          select only a elemement according to condition
    ✤   Pick-up Element Pattern:
           use only necessary element, and remove others
    ✤   Extract Element Pattern:
          replace document by a certain element



                      copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                                              57
questions?

  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                          58
thank you

  copyright(c) 2010 kuwata-lab.com all rights reserved.
                                                          59

Weitere ähnliche Inhalte

Ähnlich wie How to Make Designer-Friendly Template Engine

A SharePoint Developers Guide to Project Server
A SharePoint Developers Guide to Project ServerA SharePoint Developers Guide to Project Server
A SharePoint Developers Guide to Project ServerAlexander Burton
 
COMPRO- WEB ALBUM & MOTION ANALYZER
COMPRO- WEB ALBUM  & MOTION ANALYZERCOMPRO- WEB ALBUM  & MOTION ANALYZER
COMPRO- WEB ALBUM & MOTION ANALYZERAshish Tanwer
 
Comparison of Programming Platforms
Comparison of Programming PlatformsComparison of Programming Platforms
Comparison of Programming PlatformsAnup Hariharan Nair
 
ODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptx
ODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptxODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptx
ODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptxToon Koppelaars
 
Leverage Enterprise Integration Patterns with Apache Camel and Twitter
Leverage Enterprise Integration Patterns with Apache Camel and TwitterLeverage Enterprise Integration Patterns with Apache Camel and Twitter
Leverage Enterprise Integration Patterns with Apache Camel and TwitterBruno Borges
 
ServerTemplate Deep Dive
ServerTemplate Deep DiveServerTemplate Deep Dive
ServerTemplate Deep DiveRightScale
 
Tales of modernizing trello's web stack
Tales of modernizing trello's web stackTales of modernizing trello's web stack
Tales of modernizing trello's web stackVincent Kok
 
Search Engines and Flash: Secrets, Tricks, and Black Magic
Search Engines and Flash: Secrets, Tricks, and Black MagicSearch Engines and Flash: Secrets, Tricks, and Black Magic
Search Engines and Flash: Secrets, Tricks, and Black Magicguestb1f3a
 
Serverless Java Challenges & Triumphs
Serverless Java Challenges & TriumphsServerless Java Challenges & Triumphs
Serverless Java Challenges & TriumphsDavid Delabassee
 
Liferay Developer Best Practices for a Successful Deployment
Liferay Developer Best Practices for a Successful DeploymentLiferay Developer Best Practices for a Successful Deployment
Liferay Developer Best Practices for a Successful Deploymentrivetlogic
 
Avantage BPM Key Features
Avantage BPM Key FeaturesAvantage BPM Key Features
Avantage BPM Key FeaturesGeoffrey Long
 
AngularJS for Web and Mobile
 AngularJS for Web and Mobile AngularJS for Web and Mobile
AngularJS for Web and MobileRocket Software
 
Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015Dave Stokes
 
Avantage Enterprise Architecture
Avantage Enterprise ArchitectureAvantage Enterprise Architecture
Avantage Enterprise ArchitectureGeoffrey Long
 
Ebs troubleshooting con9019_pdf_9019_0001
Ebs troubleshooting con9019_pdf_9019_0001Ebs troubleshooting con9019_pdf_9019_0001
Ebs troubleshooting con9019_pdf_9019_0001jucaab
 
Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010joycsc
 
Working Software Over Comprehensive Documentation
Working Software Over Comprehensive DocumentationWorking Software Over Comprehensive Documentation
Working Software Over Comprehensive DocumentationAndrii Dzynia
 

Ähnlich wie How to Make Designer-Friendly Template Engine (20)

A SharePoint Developers Guide to Project Server
A SharePoint Developers Guide to Project ServerA SharePoint Developers Guide to Project Server
A SharePoint Developers Guide to Project Server
 
COMPRO- WEB ALBUM & MOTION ANALYZER
COMPRO- WEB ALBUM  & MOTION ANALYZERCOMPRO- WEB ALBUM  & MOTION ANALYZER
COMPRO- WEB ALBUM & MOTION ANALYZER
 
Comparison of Programming Platforms
Comparison of Programming PlatformsComparison of Programming Platforms
Comparison of Programming Platforms
 
ODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptx
ODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptxODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptx
ODTUG_NoPlsql_vs_SmartDB_Part1_and_2.pptx
 
Leverage Enterprise Integration Patterns with Apache Camel and Twitter
Leverage Enterprise Integration Patterns with Apache Camel and TwitterLeverage Enterprise Integration Patterns with Apache Camel and Twitter
Leverage Enterprise Integration Patterns with Apache Camel and Twitter
 
JavaCro'15 - HTTP2 Comes to Java! - David Delabassee
JavaCro'15 - HTTP2 Comes to Java! - David DelabasseeJavaCro'15 - HTTP2 Comes to Java! - David Delabassee
JavaCro'15 - HTTP2 Comes to Java! - David Delabassee
 
ServerTemplate Deep Dive
ServerTemplate Deep DiveServerTemplate Deep Dive
ServerTemplate Deep Dive
 
Tales of modernizing trello's web stack
Tales of modernizing trello's web stackTales of modernizing trello's web stack
Tales of modernizing trello's web stack
 
Search Engines and Flash: Secrets, Tricks, and Black Magic
Search Engines and Flash: Secrets, Tricks, and Black MagicSearch Engines and Flash: Secrets, Tricks, and Black Magic
Search Engines and Flash: Secrets, Tricks, and Black Magic
 
Serverless Java Challenges & Triumphs
Serverless Java Challenges & TriumphsServerless Java Challenges & Triumphs
Serverless Java Challenges & Triumphs
 
Liferay Developer Best Practices for a Successful Deployment
Liferay Developer Best Practices for a Successful DeploymentLiferay Developer Best Practices for a Successful Deployment
Liferay Developer Best Practices for a Successful Deployment
 
Avantage BPM Key Features
Avantage BPM Key FeaturesAvantage BPM Key Features
Avantage BPM Key Features
 
AngularJS for Web and Mobile
 AngularJS for Web and Mobile AngularJS for Web and Mobile
AngularJS for Web and Mobile
 
10 Ways To Improve Your Code
10 Ways To Improve Your Code10 Ways To Improve Your Code
10 Ways To Improve Your Code
 
Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015
 
Avantage Enterprise Architecture
Avantage Enterprise ArchitectureAvantage Enterprise Architecture
Avantage Enterprise Architecture
 
Ebs troubleshooting con9019_pdf_9019_0001
Ebs troubleshooting con9019_pdf_9019_0001Ebs troubleshooting con9019_pdf_9019_0001
Ebs troubleshooting con9019_pdf_9019_0001
 
Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010
 
Marcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL WorkbenchMarcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL Workbench
 
Working Software Over Comprehensive Documentation
Working Software Over Comprehensive DocumentationWorking Software Over Comprehensive Documentation
Working Software Over Comprehensive Documentation
 

Mehr von kwatch

How to make the fastest Router in Python
How to make the fastest Router in PythonHow to make the fastest Router in Python
How to make the fastest Router in Pythonkwatch
 
Migr8.rb チュートリアル
Migr8.rb チュートリアルMigr8.rb チュートリアル
Migr8.rb チュートリアルkwatch
 
なんでもID
なんでもIDなんでもID
なんでもIDkwatch
 
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方kwatch
 
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方kwatch
 
O/Rマッパーによるトラブルを未然に防ぐ
O/Rマッパーによるトラブルを未然に防ぐO/Rマッパーによるトラブルを未然に防ぐ
O/Rマッパーによるトラブルを未然に防ぐkwatch
 
正規表現リテラルは本当に必要なのか?
正規表現リテラルは本当に必要なのか?正規表現リテラルは本当に必要なのか?
正規表現リテラルは本当に必要なのか?kwatch
 
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)kwatch
 
DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!kwatch
 
PHPとJavaScriptにおけるオブジェクト指向を比較する
PHPとJavaScriptにおけるオブジェクト指向を比較するPHPとJavaScriptにおけるオブジェクト指向を比較する
PHPとJavaScriptにおけるオブジェクト指向を比較するkwatch
 
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?kwatch
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Pythonkwatch
 
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策kwatch
 
PHP5.5新機能「ジェネレータ」初心者入門
PHP5.5新機能「ジェネレータ」初心者入門PHP5.5新機能「ジェネレータ」初心者入門
PHP5.5新機能「ジェネレータ」初心者入門kwatch
 
Pretty Good Branch Strategy for Git/Mercurial
Pretty Good Branch Strategy for Git/MercurialPretty Good Branch Strategy for Git/Mercurial
Pretty Good Branch Strategy for Git/Mercurialkwatch
 
Oktest - a new style testing library for Python -
Oktest - a new style testing library for Python -Oktest - a new style testing library for Python -
Oktest - a new style testing library for Python -kwatch
 
文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみた文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみたkwatch
 
I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"kwatch
 
Cより速いRubyプログラム
Cより速いRubyプログラムCより速いRubyプログラム
Cより速いRubyプログラムkwatch
 
Javaより速いLL用テンプレートエンジン
Javaより速いLL用テンプレートエンジンJavaより速いLL用テンプレートエンジン
Javaより速いLL用テンプレートエンジンkwatch
 

Mehr von kwatch (20)

How to make the fastest Router in Python
How to make the fastest Router in PythonHow to make the fastest Router in Python
How to make the fastest Router in Python
 
Migr8.rb チュートリアル
Migr8.rb チュートリアルMigr8.rb チュートリアル
Migr8.rb チュートリアル
 
なんでもID
なんでもIDなんでもID
なんでもID
 
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
 
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
 
O/Rマッパーによるトラブルを未然に防ぐ
O/Rマッパーによるトラブルを未然に防ぐO/Rマッパーによるトラブルを未然に防ぐ
O/Rマッパーによるトラブルを未然に防ぐ
 
正規表現リテラルは本当に必要なのか?
正規表現リテラルは本当に必要なのか?正規表現リテラルは本当に必要なのか?
正規表現リテラルは本当に必要なのか?
 
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
 
DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!
 
PHPとJavaScriptにおけるオブジェクト指向を比較する
PHPとJavaScriptにおけるオブジェクト指向を比較するPHPとJavaScriptにおけるオブジェクト指向を比較する
PHPとJavaScriptにおけるオブジェクト指向を比較する
 
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
 
PHP5.5新機能「ジェネレータ」初心者入門
PHP5.5新機能「ジェネレータ」初心者入門PHP5.5新機能「ジェネレータ」初心者入門
PHP5.5新機能「ジェネレータ」初心者入門
 
Pretty Good Branch Strategy for Git/Mercurial
Pretty Good Branch Strategy for Git/MercurialPretty Good Branch Strategy for Git/Mercurial
Pretty Good Branch Strategy for Git/Mercurial
 
Oktest - a new style testing library for Python -
Oktest - a new style testing library for Python -Oktest - a new style testing library for Python -
Oktest - a new style testing library for Python -
 
文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみた文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみた
 
I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"
 
Cより速いRubyプログラム
Cより速いRubyプログラムCより速いRubyプログラム
Cより速いRubyプログラム
 
Javaより速いLL用テンプレートエンジン
Javaより速いLL用テンプレートエンジンJavaより速いLL用テンプレートエンジン
Javaより速いLL用テンプレートエンジン
 

Kürzlich hochgeladen

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Kürzlich hochgeladen (20)

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

How to Make Designer-Friendly Template Engine

  • 1. RubyKaigi 2010 How to Make Designer-Friendly Template Engine - Keep Template as pure HTML - makoto kuwata http://www.kuwata-lab.com/ copyright(c) 2010 kuwata-lab.com all rights reserved. 1
  • 2. README.txt Goal ✤ For template engine users: get correct knowledge ✤ For template engine desingers: get an ideal solution Motiv. ✤ Want correct wrong explanations related to template engine ✤ Want to insist that eRuby is not an ideal solution Target ✤ Template engine users and designers copyright(c) 2010 kuwata-lab.com all rights reserved. 2
  • 3. I must apologize... ✤ I would talk about... ✤ How to keep html design of template files ✤ What are performance bottole necks of engine ✤ What is wrong about existing template engines ✤ How to design engine which is designer-friendly, very fast, and easy to implement in any languages ✤ But I only have 30 minutes ✤ I can talk about the first issue copyright(c) 2010 kuwata-lab.com all rights reserved. 3
  • 4. Reference ✤ Rubyist Magazine - Guide to Template System ✤ http://jp.rubyist.net/magazine/?0024-TemplateSystem ✤ http://jp.rubyist.net/magazine/?0024-TemplateSystem2 copyright(c) 2010 kuwata-lab.com all rights reserved. 4
  • 5. Agenda ✤ Fundamental section: ✤ Business Layer and Presentation Layer ✤ Existing Issues and the Cause ✤ Think about Presentation Logic ✤ Kwartz section: ✤ Introduction to Kwartz template engine ✤ Presentation Logic Pattern copyright(c) 2010 kuwata-lab.com all rights reserved. 5
  • 6. Fundamental section: Business Layer & Presentation Layer copyright(c) 2010 kuwata-lab.com all rights reserved. 6
  • 7. Business layer and Presentation layer ✤ Business layer ✤ WHAT should be displayed? ✤ by main program ✤ Presentation layer ✤ HOW to display it? ✤ by template engine, template file copyright(c) 2010 kuwata-lab.com all rights reserved. 7
  • 8. Data and Logic ✤ Both layers have each data and logic Business layer Presentation layer (main program) (template engine) Data Business Data Presentation Data Logic Business Logic Presentation Logic separated by template engine copyright(c) 2010 kuwata-lab.com all rights reserved. 8
  • 9. Applicaiton Example Business logic ✤ Select top 20 ✤ of book sales Business data ✤ by <table> tag Presentaion data ✤ with toggling bgcolor Presentation logic copyright(c) 2010 kuwata-lab.com all rights reserved. 9
  • 10. Frequent Misunderstandings (1) Mis: ✤ Template engine separates presentation from logic. Truth: ✤ Template engine separatin presentation layer from business layer. ✤ Presentation layer have own logics. common mistake in Japan... copyright(c) 2010 kuwata-lab.com all rights reserved. 10
  • 11. Frequent Misunderstandings (2) Mis: ✤ Presentation layer shouldn't contain any logics. Truth: ✤ Presentation layer should contain presentation logics. ✤ "HTML template shouldn't contain any presentation logics" and "Layers should be separated" are different things. copyright(c) 2010 kuwata-lab.com all rights reserved. 11
  • 12. Add-up ✤ Business layer ✤ WHAT should be displayed? ✤ By main program ✤ Presentation layer ✤ HOW to display it? ✤ By template engine and template file ✤ Both layers have each data and logics copyright(c) 2010 kuwata-lab.com all rights reserved. 12
  • 13. Fundamental section: Existing Issues and the Cause copyright(c) 2010 kuwata-lab.com all rights reserved. 13
  • 14. Templates Annoys Designers ProjectA: Rails <table> ProjectC: PHP <% @arr.each { %> <table> .... <?php foreach() { ?> <% } %> .... </table> <?php } ?> </table> ProjectB: JSP <table> <forEach items=""> Designer: Why I must .... master so much langs? </forEach> </table> copyright(c) 2010 kuwata-lab.com all rights reserved. 14
  • 15. Edit & Update the same file template file <table> <% @arr.each { %> .... <% } %> </table> Conflict! edit presentaion edit HTML logic Designer Programmer copyright(c) 2010 kuwata-lab.com all rights reserved. 15
  • 16. The Cause of Issues ✤ It is the cause that template file presentation logics <table> are embedded in <% i = 0 %> <% @arr.eachx do |x| %> template file <% i += i %> <% c = i.odd ? %> <% 'odd':'even' %> pure HTML is the <tr class="<%=c%>"> solution <td><%= x %></td> </tr> <% end %> </table> copyright(c) 2010 kuwata-lab.com all rights reserved. 16
  • 17. Pure HTML Template is Great ✤ Easy to preview by browsers Designer for ✤ No need to learn a lot of template langs! ✤ Easy to validate by HTML validator ✤ Allow to use their favorite HTML editor Avoid edit confliction Programmer for ✤ ✤ Remove whitespaces to reduce traffic ✤ Transform template files for mobile phones copyright(c) 2010 kuwata-lab.com all rights reserved. 17
  • 18. The Question which bothered Matz ✤ So, where is presentation logics? Matz diary (2004-08-24) http://www.rubyist.net/ matz/20040824.html#p01 (Summary) When view layer needs some logics, where should I put it in? I don't want it to be in HTML template... copyright(c) 2010 kuwata-lab.com all rights reserved. 18
  • 19. Add-up ✤ Presentation logics in template files causes a lot of problems ✤ Pure HTML template is great ✤ Where is presentation logics? copyright(c) 2010 kuwata-lab.com all rights reserved. 19
  • 20. Fundamenta section: Think about Presentaion Logic copyright(c) 2010 kuwata-lab.com all rights reserved. 20
  • 21. Where should be Presentation Logics? A. In template file (eRuby, JSP, Kid) Presentation Logic Business Logic & Data + Presentaion Data B. In main program (Amrita2, XMLC, HTML::Template) Presentation Logic Presentation Data +Business Logic & Data C. In independed file (Kwartz, Tapestry, Mayaa) Presentation Logic Business Logic & Data Presentation Data template file main program copyright(c) 2010 kuwata-lab.com all rights reserved. 21
  • 22. A. In Template File (1) ex: eRuby (Ruby) <table> <% odd = false %> <% for item in @list %> <% odd = ! odd <% cls = odd ? 'odd' : 'even' %> <tr class="<%= cls %>"> <td><%=h item %></td> </tr> <% end %> •HTML and non-HTML </table> are mixed → never pure HTML ! copyright(c) 2010 kuwata-lab.com all rights reserved. 22
  • 23. A. In Template File (2) ex: JSP with custom tags (Java) <table> <c:forEach var="item" items="${list}" varStatus="loop"> <c:set var="klass" value="${loop.count%2==0?'odd':'even'}" /> <tr class="${klass}"> <td><c:out value="${item}" /></td> </tr> </c:forEach> •Even if you use custom tags, </table> presentation logics are mixed into HTML (just same as eRuby). copyright(c) 2010 kuwata-lab.com all rights reserved. 23
  • 24. A. In Template File (3) ex: Kid template engine (Python) <table> <tr py:for="i, item in enumerate(items)" class="${i % 2 and 'even' or 'odd'}"> <td py:content="item">dummy</td> </tr> </table> •Using HTML tags and attributess, Kid aquired designer-friendly template! •But notice that presentation logics are still embedded into templates copyright(c) 2010 kuwata-lab.com all rights reserved. 24
  • 25. A. In Template File (4) Pros. ✤ Presentation layer (= template file) is compretely separated from business layer. ✤ Easy to uderstand, easy to implement Hard to keep pure HTML Cons. ✤ ✤ Designer and programmer will edit the same file at the same time, and conflicted in the result ✤ Designer may break presentation logics in template files accidentally copyright(c) 2010 kuwata-lab.com all rights reserved. 25
  • 26. Where should be Presentation Logics? A. In template file (eRuby, JSP, Kid) Presentation Logic Business Logic & Data + Presentaion Data B. In main program (Amrita2, XMLC, HTML::Template) Presentation Logic Presentation Data +Business Logic & Data C. In independed file (Kwartz, Tapestry, Mayaa) Presentation Logic Business Logic & Data Presentation Data template file main program copyright(c) 2010 kuwata-lab.com all rights reserved. 26
  • 27. B. In Main Program (1) ex: Amrita2 temlate file (Ruby) <table> <tr id="list" class="odd"> <td id="item">dummy</td> </tr> </table> •Just to place "mark" (= id attr in Amrita2) into template file •Pure HTML template (because no presentation logics in it) copyright(c) 2010 kuwata-lab.com all rights reserved. 27
  • 28. B. In Main Program (2) ex: Amrita2 main program (Ruby) ## business data to display list = [ 'A', 'B', 'C' ] •Necessary to "process" ## presentation logic bussiness data for list2 = []; odd = false presentation layer for x in list cls = (odd = !odd) ? 'odd' : 'even' item2 = a(:class=>cls) { {:item=>x} } list2 << item2 end context = { :list=>list2 } ## read template and render with data tmpl = Amrita2::TemplateFile.new('ex1.html') tmpl.expand(html='', context) print html copyright(c) 2010 kuwata-lab.com all rights reserved. 28
  • 29. B. In Main Program (3) Pros. ✤ Just embed "mark" into template ✤ pure HTML template (in most cases) ✤ No conflicts when editing files ✤ No accidental change of logics by designer Cons. ✤ Business layer and Presentation layer are NOT separated! (*) ✤ Confusable usage (because logic should be represented by data) (*) devisable to avoid (ex. define dedicated class) copyright(c) 2010 kuwata-lab.com all rights reserved. 29
  • 30. Where should be Presentation Logics? A. In template file (eRuby, JSP, Kid) Presentation Logic Business Logic & Data + Presentaion Data B. In main program (Amrita2, XMLC, HTML::Template) Presentation Logic Presentation Data +Business Logic & Data C. In independed file (Kwartz, Tapestry, Mayaa) Presentation Logic Business Logic & Data Presentation Data template file main program copyright(c) 2010 kuwata-lab.com all rights reserved. 30
  • 31. C. In Independed File (1) ex: Kwartz presentation data file (Ruby) <table> <tr id="mark:list" class="odd"> <td id="mark:item">dummy</td> </tr> </table> •Just place "mark" (= id attr in Kwartz) in template file copyright(c) 2010 kuwata-lab.com all rights reserved. 31
  • 32. C. In Independent File (2) ex: Kwartz presentation logic file (Ruby) #item { ## id="mark:item" value: x; ## display value of x } #list { ## id="mark:list" logic: { ## iterate over element for x in @list _elem end } •Write your presentation logics } just like CSS •No presentation logics in HTML ! copyright(c) 2010 kuwata-lab.com all rights reserved. 32
  • 33. CSS (Visual Design) HTML (Document Structure) Kwartz JavaScript (Presentation (Client-side Logic) Logic) copyright(c) 2010 kuwata-lab.com all rights reserved. 33
  • 34. C. In Independent File (3) Pros. ✤ Business layer and Presentation layer are completely separated! ✤ Presentation data (=HTML) and Presentation logic are also separated! ✤ Pure HTML template ✤ No conflicts when editiong files ✤ No accidental modification of logics Cons. ✤ Number of files are increased copyright(c) 2010 kuwata-lab.com all rights reserved. 34
  • 35. Important thing, More Important thing ✤ Important thing ✤ Template format is pure HTML ✤ More important thing ✤ Presentation logic should be separated (or independent) from others! ✤ Pure HTML template is just derivation from it copyright(c) 2010 kuwata-lab.com all rights reserved. 35
  • 36. Frequent Misunderstanding (1) Mis.: ✤ My template is pure HTML, so presentation layer is separated! Truth: ✤ "Template is pure HTML" doesn't mean "Presentation layer is separated" ✤ Probably she/he misunderstands "No logics in template" as "Presentation layer is separated" copyright(c) 2010 kuwata-lab.com all rights reserved. 36
  • 37. Frequent Misunderstanding (2) Mis.: ✤ Web designer may write buggy logic in raw PHP, so I introduced Smarty! Truth: ✤ Smarty can't prevent buggy logic nor accidental logic modification. ✤ The root cause of the problem is that desginer can access to presention logic ✤ A radical solution is to separate presentation logic from template file copyright(c) 2010 kuwata-lab.com all rights reserved. 37
  • 38. Add-up ✤ Presentation logic can be... ✤ In template file (non-html, conflictions) ✤ In main program (layers are not separated) ✤ In independent file (ideal) ✤ Pure HTML template is important ✤ Separation of presentation logic is more important copyright(c) 2010 kuwata-lab.com all rights reserved. 38
  • 39. Kwartz section: Introduction to Kwartz copyright(c) 2010 kuwata-lab.com all rights reserved. 39
  • 40. Kwartz Overview ✤ What is Kwartz? ✤ A template engine which can separate Presentation logic from Presentation data (=HTML) ✤ Pure HTML template ✤ Implemented in Ruby, PHP ✤ http://www.kuwata-lab.com/kwartz/ copyright(c) 2010 kuwata-lab.com all rights reserved. 40
  • 41. Example: Presentation Data Add "marking" (=id attr) to where you want to ex.html manipulate dinamically <table> <tr id="mark:list"> <td id="mark:item">Foo</td> </tr> <tr id="dummy:d1"> <td>Bar</td> </tr> </table> `id="dummy:xxx"' means dummy elelment copyright(c) 2010 kuwata-lab.com all rights reserved. 41
  • 42. Example: Presentation Logic ex.plogic Write presentation logic #list { for each elements logic: { for x in @list _stag ## start-tag _cont ## content _etag ## end-tag end } Repeat <tr> element } #item { value: x; Display value of x as } content of <td> element copyright(c) 2010 kuwata-lab.com all rights reserved. 42
  • 43. Example: Compilation Compile files into a eRuby file $ kwartz -l eruby -p ex.plogic ex.html > ex.rhtml ex.rhtml (compiled by Kwartz) <table> <% for x in @list %> <tr> •Attribute `id="mark:xxx"' is <td><%= x %></td> removed automatically </tr> (not removed if `id="xxx"') <% end %> </table> •Element which has `id="dummy:xxx"' is also removed copyright(c) 2010 kuwata-lab.com all rights reserved. 43
  • 44. Example: Main Program Main program ## business data to be displayed list = ['A', 'B', 'C'] context = { :list => list } ## load template and render it with data require 'erubis' eruby = Erubis::Eruby.load_file('ex.rhtml') html = eruby.evaluate(context) print html Main program doesn't require Kwartz copyright(c) 2010 kuwata-lab.com all rights reserved. 44
  • 45. Pros and Cons ✤ Pros. ✤ pure HTML template ✤ Very fast ✤ Supports eRuby, PHP, JSP, and so on ✤ Available with non-HTML text file ✤ Cons. ✤ Messy compile (should be automated) ✤ Runtime error is reported with line number AFTER compiled code, not original source code copyright(c) 2010 kuwata-lab.com all rights reserved. 45
  • 46. Practical Examples (1) repeat over element repeat only content #list { #list { logic: { logic: { for item in @list _stag _stag # start-tag for item in @list _cont # content _cont _etag # end-tag end end _etag } } } } Useful to repeat <dt> and <dd> copyright(c) 2010 kuwata-lab.com all rights reserved. 46
  • 47. Practical Examples (2) display only when display default value #list { #list { logic: { logic: { if @list.size > 0 _stag _stag if @name.blank? _cont print('World') _etag else end _cont } end } _etag } } copyright(c) 2010 kuwata-lab.com all rights reserved. 47
  • 48. Practical Examples (3) remove dummy tag remove dummy element #list { #list { logic: { logic: { #_stag } _cont } #_etag } replace by other } #list { logic: { _element(list2) } } reuse a certain element copyright(c) 2010 kuwata-lab.com all rights reserved. 48
  • 49. Practical Examples (4) より複雑なプレゼンテーションロジック #list { attrs: "class" cls; # attribue logic: { odd = false Presentation logic is for x in @list separated from HTML file odd = !odd cls = odd ? 'odd' : 'even' _elem end No need to touch HTML at all } even if you change presentaion } logic dramatically copyright(c) 2010 kuwata-lab.com all rights reserved. 49
  • 50. Add-up ✤ Kwartz tempate engine ✤ Separates presentation logics from presentation data (=HTML) ✤ Pure HTML Template ✤ No need to touch HTML file at all even if you change presentation logics copyright(c) 2010 kuwata-lab.com all rights reserved. 50
  • 51. Kwartz section: Presentation Pattern copyright(c) 2010 kuwata-lab.com all rights reserved. 51
  • 52. Overview ✤ What is Presentation Pattern? ✤ Best practices in presentation layer (data & logic) ✤ (Notice that this is my original term and not popular at all) ✤ Very easy to understand, compared to GoF! ✤ http://www.kuwata-lab.com/kwartz/kwartz3ruby- pattern-catalog.html copyright(c) 2010 kuwata-lab.com all rights reserved. 52
  • 53. List of Patterns ✤ Replacement ✤ Iteration ✤ Replace Element with ✤ Iterate Element Pattern Value Pattern ✤ Iterate Content Pattern ✤ Replace Content with Value Pattern ✤ Selection ✤ Default Content Pattern ✤ Select Element/Content ✤ Replace Element with Pattern Element/Content Pattern ✤ Pick-up Element/Content ✤ Replace Content with Pattern Element/Content Pattern ✤ Extract Element/Content Pattern ✤ Deletion ✤ Delete Element Pattern Today's menu ✤ Delete Tag Pattern copyright(c) 2010 kuwata-lab.com all rights reserved. 53
  • 54. Select Element Pattern ✤ Req: Want to change data according to condition <div id="mark:message"> #message { <div id="error"> logic: { ERROR! if status == 'error' </div> _element(error) <div id="warning"> elsif status == 'warn' Warning: _element(warning) </div> else <div id="noerror"> _element(noerror) No error. end </dvi> } Select an elem Add different id according to </div> } for each elems condition copyright(c) 2010 kuwata-lab.com all rights reserved. 54
  • 55. Pick-up Element Pattern ✤ Req: Want to include a lot of dummy elems (for preview) <ol id="mark:list"> #list { <li id="mark:item"> logic: { Item1 _stag </li> add id to _element(item) <li>Item2</li> non-dummy _etag <li>Item3</li> elements } <li>Item4</li> } Pick up only non- <li>Item5</li> dummy elems, and <li>Item6</li> dummy elems are </ol> removed in the result copyright(c) 2010 kuwata-lab.com all rights reserved. 55
  • 56. Extract Element Pattern ✤ Req: Want to extract a certain elem form a document <html id="mark:whole"> #whole { <body> logic: { <form id="form"> _element(form) <input ... /> } Replace document <input ... /> } by a certain elem <input ... /> </form> #DOCUMENT { </body> logic: { </html> Add id attr to _element(form) document root } elem (=<html>) Kwartz provides special } id name for this purpose copyright(c) 2010 kuwata-lab.com all rights reserved. 56
  • 57. Add-up ✤ Presentation pattern ✤ Best practices for presentation layer (data & logic) ✤ Select Element Pattern: select only a elemement according to condition ✤ Pick-up Element Pattern: use only necessary element, and remove others ✤ Extract Element Pattern: replace document by a certain element copyright(c) 2010 kuwata-lab.com all rights reserved. 57
  • 58. questions? copyright(c) 2010 kuwata-lab.com all rights reserved. 58
  • 59. thank you copyright(c) 2010 kuwata-lab.com all rights reserved. 59