Changes between Version 4 and Version 5 of PresentationTier


Ignore:
Timestamp:
05/18/2015 01:41:42 PM (2 years ago)
Author:
apoirier
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PresentationTier

    v4 v5  
    5858    >>> from nagare.namespaces import xhtml 
    5959    >>> h = xhtml.Renderer() 
    60      
    61     >>> root = h.div   
     60 
     61    >>> root = h.div 
    6262    >>> root.set('id', 'content') 
    63      
    64     >>> title = h.h1     
     63 
     64    >>> title = h.h1 
    6565    >>> title.text = 'Hello world' 
    66      
     66 
    6767    >>> root.append(title) 
    6868    >>> root.write_htmlstring() 
     
    8181 
    8282The ``_Tag`` class implements a ``__call__``  method to add children, text or 
    83 attributes to a DOM object:  
     83attributes to a DOM object: 
    8484 
    8585  .. code-block:: python 
    86    
     86 
    8787     def __call__(self, *children, **attributes) 
    8888 
     
    9494  - list, tuple or generator which each elements are added to the DOM object 
    9595  - dictionary which each items are added as attributes of the DOM object 
    96    
     96  - :wiki:`ComponentModel component` ``comp`` wich will be rendered with its 
     97    default view (i.e ``comp.render(h)``) and the resulting DOM tree added 
     98    to the DOM object 
     99 
    97100Each items in ``attributes`` are added as attributes of the DOM object. 
    98101 
     
    106109 
    107110  .. code-block:: python 
    108    
     111 
    109112     >>> from nagare.namespaces import xhtml 
    110113     >>> h = xhtml.Renderer() 
     
    128131 
    129132   >>> from __future__ import with_statement 
    130    
     133 
    131134   >>> from nagare.namespaces import xhtml 
    132135   >>> h = xhtml.Renderer() 
     
    136139   ...     with h.h1: 
    137140   ...         h << 'Hello world' 
    138    
     141 
    139142   >>> root = h.root 
    140143   >>> root.write_htmlstring() 
     
    145148  Dont't forget the line ``from __future__ import with_statement`` at the 
    146149  beginning of the file to activate the ``with`` statement in Python 2.5. 
    147    
     150 
    148151  In a component view, don't forget to return the tree with ``h.root`` 
    149152 
     
    154157 
    155158   >>> from __future__ import with_statement 
    156     
     159 
    157160   >>> from nagare.namespaces import xhtml 
    158161   >>> h = xhtml.Renderer() 
     
    160163   >>> with h.div(id='content'): 
    161164   ...     h << h.h1('Hello world') 
    162     
     165 
    163166   >>> root = h.root 
    164167   >>> root.write_htmlstring() 
     
    172175   # Functional 
    173176   >>> root = h.ul([u.li(element) for element in l]) 
    174     
     177 
    175178   # Imperative 
    176179   >>> with h.ul: 
     
    214217   >>> from nagare.namespaces import xhtml 
    215218   >>> h = xhtml.Renderer() 
    216     
     219 
    217220   >>> tree = h.html( 
    218221   ...            h.body( 
     
    237240   >>> from nagare.namespaces import xhtml 
    238241   >>> h = xhtml.Renderer() 
    239     
     242 
    240243   >>> root = h.parse_html('/tmp/template.xml', xhtml=True) 
    241     
     244 
    242245   >>> print root.write_xmlstring() 
    243246   <html xmlns="http://www.w3.org/1999/xhtml" xmlns:meld="http://www.plope.com/software/meld3"> 
     
    256259   >>> from nagare.namespaces import xhtml 
    257260   >>> h = xhtml.Renderer() 
    258     
     261 
    259262   >>> root = h.parse_html('/tmp/template.xml', xhtml=True) 
    260     
     263 
    261264   >>> root.findmeld('title') 
    262265   <Element {http://www.w3.org/1999/xhtml}h1 at 8410c5c> 
     
    267270Once a DOM object is found, you can manipulate it with the normal ElementTree, 
    268271Lxml, functional or imperative API: 
    269   
    270 .. code-block:: pycon 
    271  
    272    >>> from nagare.namespaces import xhtml 
    273    >>> h = xhtml.Renderer() 
    274     
     272 
     273.. code-block:: pycon 
     274 
     275   >>> from nagare.namespaces import xhtml 
     276   >>> h = xhtml.Renderer() 
     277 
    275278   >>> root = h.parse_html('/tmp/template.xml', xhtml=True) 
    276     
     279 
    277280   >>> t = root.findmeld('title') 
    278281   >>> t.text = 'World' 
     
    293296 
    294297    .. code-block:: python 
    295        
     298 
    296299       >>> from nagare.namespaces import xhtml 
    297300       >>> h = xhtml.Renderer() 
    298      
     301 
    299302       >>> root = h.div(h.h1('Hello world'), id='content') 
    300303       >>> root.write_htmlstring() 
    301        <div id="content"><h1>Hello world</h1></div>'       
    302         
     304       <div id="content"><h1>Hello world</h1></div>' 
     305 
    303306       >>> root.fill("Go to ", h.a("Nagare home", href="http://www.nagare.org"), class_="description") 
    304         
     307 
    305308       >>> root.write_htmlstring() 
    306309        '<div id="content" class="description">Go to <a href="http://www.nagare.org">Nagare home</a></div>' 
    307      
     310 
    308311  - ``replace(self, *children)`` -- replace the DOM object by the given children: 
    309312 
    310313    .. code-block:: python 
    311        
     314 
    312315       >>> from nagare.namespaces import xhtml 
    313316       >>> h = xhtml.Renderer() 
    314      
     317 
    315318       >>> root = h.div(h.h1('Hello world'), id='content') 
    316319       >>> root.write_htmlstring() 
    317320       <div id="content"><h1>Hello world</h1></div>' 
    318321       >>> root[0].write_htmlstring() 
    319        <h1>Hello world</h1>     
    320         
     322       <h1>Hello world</h1> 
     323 
    321324       >>> root[0].replace(h.h2('Nagare')) 
    322         
     325 
    323326       >>> root.write_htmlstring() 
    324327       '<div id="content"><h2>Nagare</h2></div>' 
    325    
     328 
    326329  - ``repeat(self, iterable, childname=None)`` -- (description taken from 
    327330    the `meld3 <http://plope.com/software/meld3/>`_ site) 
    328      
     331 
    329332      Repeats an element with values from an iterable. 
    330      
     333 
    331334      If ``childname`` is not ``None``, repeat the element on which repeat was called, 
    332335      otherwise find the child element with a ``meld:id`` matching ``childname`` 
    333336      and repeat that.  The element is repeated within its parent element. 
    334      
     337 
    335338      This method returns an iterable; the value of each iteration is a 
    336339      two-sequence in the form (newelement, data).  ``newelement`` is a clone of 
     
    338341      been seated in its parent element in the template. ``data`` is a value from 
    339342      the passed in iterable.  Changing ``newelement`` (typically based on values 
    340       from ``data``) mutates the element "in place".  
     343      from ``data``) mutates the element "in place". 
    341344 
    342345    .. code-block:: pycon 
    343      
     346 
    344347        >>> from nagare.namespaces import xhtml 
    345348        >>> h = xhtml.Renderer() 
    346          
     349 
    347350        >>> root = h.parse_xmlstring('''<ul xmlns:meld="http://www.plope.com/software/meld3"> 
    348351        ...   <li meld:id="alist" align="center">A line example</li> 
    349352        ... </ul>''') 
    350      
     353 
    351354        >>> items = ['Item %d' % i for i in range(3)] 
    352      
     355 
    353356        >>> for (element, item) in root.findmeld('alist').repeat(items): 
    354357        ...     element.text = item 
    355358        ... 
    356      
     359 
    357360        >>> print root.write_xmlstring() 
    358361        <ul xmlns:meld="http://www.plope.com/software/meld3"> 
     
    372375    removed, else they are kept so that the xml string can be re-parse as a 
    373376    template. 
    374      
     377 
    375378  - ``write_htmlstring(self, encoding='utf-8', pipeline=True)`` -- serializes 
    376379    the DOM tree into a HTML string, encoded according to the ``encoding``