doc/book/en/B1020-define-views.en.txt
changeset 1253 9716a9f77980
parent 1249 905d76e38433
child 1280 ebf1d748ed5a
equal deleted inserted replaced
1251:af40e615dc89 1253:9716a9f77980
    59   
    59   
    60 * `html_headers()`, returns a list of HTML headers to set by the main template
    60 * `html_headers()`, returns a list of HTML headers to set by the main template
    61 
    61 
    62 * `page_title()`, returns the title to use in the HTML header `title`
    62 * `page_title()`, returns the title to use in the HTML header `title`
    63 
    63 
    64 * `creator(eid)`, returns the eid and the login of the entity creator of the entity
    64 
    65   having the eid given in the parameter 
    65 Other basic view classes
    66 
    66 ````````````````````````
    67 Other basic views classes
       
    68 `````````````````````````
       
    69 Here are some of the subclasses of `View` defined in `cubicweb.common.view`
    67 Here are some of the subclasses of `View` defined in `cubicweb.common.view`
    70 that are more concrete as they relates to data rendering within the application:
    68 that are more concrete as they relate to data rendering within the application:
    71 
    69 
    72 * `EntityView`, view applying to lines or cell containing an entity (e.g. an eid)
    70 * `EntityView`, view applying to lines or cell containing an entity (e.g. an eid)
    73 * `StartupView`, start view that does not require a result set to apply to
    71 * `StartupView`, start view that does not require a result set to apply to
    74 * `AnyRsetView`, view applied to any result set 
    72 * `AnyRsetView`, view applied to any result set 
    75 * `EmptyRsetView`, view applied to an empty result set
    73 * `EmptyRsetView`, view applied to an empty result set
    81 
    79 
    82 - an identifier (all objects in `CubicWeb` are entered in a registry
    80 - an identifier (all objects in `CubicWeb` are entered in a registry
    83   and this identifier will be used as a key). This is defined in the class
    81   and this identifier will be used as a key). This is defined in the class
    84   attribute ``id``.
    82   attribute ``id``.
    85   
    83   
    86 - a filter to select the resulsets it can be applied to. This is defined in
    84 - a filter to select the result sets it can be applied to. This is defined in
    87   the class attribute ``__selectors__``, which expects a tuple of selectors
    85   the class attribute ``__selectors__``, which expects a tuple of selectors
    88   as its value.
    86   as its value.
    89 
    87 
    90 
    88 
    91 For a given identifier, multiple views can be defined. `CubicWeb` uses
    89 For a given identifier, multiple views can be defined. `CubicWeb` uses
    92 a selector which computes scores so that it can identify and select the
    90 a selector which computes scores to identify and select the
    93 best view to apply in context. The selectors library is in 
    91 best view to apply in the given context. The selectors library is in 
    94 ``cubicweb.common.selector`` and a library of the methods used to
    92 ``cubicweb.common.selector`` and a library of the methods used to
    95 compute scores is in ``cubicweb.vregistry.vreq``.
    93 compute scores is in ``cubicweb.vregistry.vreq``.
    96 
    94 
    97 .. include:: B1021-views-selectors.en.txt
    95 .. include:: B1021-views-selectors.en.txt
    98 
    96 
   105 
   103 
   106 A `registerer` can, for example, identify when we register an 
   104 A `registerer` can, for example, identify when we register an 
   107 object that is equivalent to an already registered object, which
   105 object that is equivalent to an already registered object, which
   108 could happen when we define two `primary` views for an entity type.
   106 could happen when we define two `primary` views for an entity type.
   109 
   107 
   110 The purpose of a `registerer` is to control objects registry
   108 The purpose of a `registerer` is to control object registry
   111 at the application startup whereas `selectors` controls objects
   109 at the application startup whereas `selectors` control objects
   112 when they are selected for display.
   110 when they are selected for display.
   113 
   111 
   114 
   112 
   115 `CubicWeb` provides a lot of standard views for the default class
   113 `CubicWeb` provides a lot of standard views for the default class
   116 `EntityType`. You can find them in ``cubicweb/web/views/``.
   114 `EntityType`. You can find them in ``cubicweb/web/views/``.
   177   12.         self.w(u'<p>%s</p>' % entity.text)
   175   12.         self.w(u'<p>%s</p>' % entity.text)
   178 
   176 
   179 The above source code defines a new primary view (`line 03`) for
   177 The above source code defines a new primary view (`line 03`) for
   180 ``BlogEntry`` (`line 05`). 
   178 ``BlogEntry`` (`line 05`). 
   181 
   179 
   182 Since views are applied to resultsets and resulsets can be tables of
   180 Since views are applied to result sets which can be tables of
   183 data, it is needed to recover the entity from its (row,col)
   181 data, we have to recover the entity from its (row,col)-coordinates (`line 08`).
   184 coordinates (`line 08`). We will get to this in more detail later.
   182 We will get to this in more detail later.
   185 
   183 
   186 The view has a ``self.w()`` method that is used to output data. Here `lines
   184 The view method ``self.w()`` is used to output data. Here `lines
   187 09-12` output HTML tags and values of the entity's attributes.
   185 09-12` output HTML tags and values of the entity's attributes.
   188 
   186 
   189 When displaying same blog entry as before, you will notice that the
   187 When displaying the same blog entry as before, you will notice that the
   190 page is now looking much nicer.
   188 page is now looking much nicer. [FIXME: it is not clear to what this refers.]
   191 
   189 
   192 .. image:: images/lax-book.09-new-view-blogentry.en.png
   190 .. image:: images/lax-book.09-new-view-blogentry.en.png
   193    :alt: blog entries now look much nicer
   191    :alt: blog entries now look much nicer
   194 
   192 
   195 Let us now improve the primary view of a blog ::
   193 Let us now improve the primary view of a blog ::
   204   08.         self.w(u'<p>%s</p>' % entity.description)
   202   08.         self.w(u'<p>%s</p>' % entity.description)
   205   09.         rset = self.req.execute('Any E WHERE E entry_of B, B eid "%s"' % entity.eid)
   203   09.         rset = self.req.execute('Any E WHERE E entry_of B, B eid "%s"' % entity.eid)
   206   10.         self.wview('primary', rset)
   204   10.         self.wview('primary', rset)
   207 
   205 
   208 In the above source code, `lines 01-08` are similar to the previous
   206 In the above source code, `lines 01-08` are similar to the previous
   209 view we defined.
   207 view we defined. [FIXME: defined where ?]
   210 
   208 
   211 At `line 09`, a simple request in made to build a resultset with all
   209 At `line 09`, a simple request is made to build a result set with all
   212 the entities linked to the current ``Blog`` entity by the relationship
   210 the entities linked to the current ``Blog`` entity by the relationship
   213 ``entry_of``. The part of the framework handling the request knows
   211 ``entry_of``. The part of the framework handling the request knows
   214 about the schema and infer that such entities have to be of the
   212 about the schema and infer that such entities have to be of the
   215 ``BlogEntry`` kind and retrieves them.
   213 ``BlogEntry`` kind and retrieves them.
   216 
   214 
   217 The request returns a selection of data called a resultset. At 
   215 The request returns a selection of data called a result set. At 
   218 `line 10` the view 'primary' is applied to this resultset to output
   216 `line 10` the view 'primary' is applied to this result set to output
   219 HTML. 
   217 HTML. 
   220 
   218 
   221 **This is to be compared to interfaces and protocols in object-oriented
   219 **This is to be compared to interfaces and protocols in object-oriented
   222 languages. Applying a given view to all the entities of a resultset only
   220 languages. Applying a given view called 'a_view' to all the entities
   223 requires the availability, for each entity of this resultset, of a
   221 of a result set only requires to have for each entity of this result set,
   224 view with that name that can accepts the entity.**
   222 an available view called 'a_view' which accepts the entity.**
   225 
   223 
   226 Assuming we added entries to the blog titled `MyLife`, displaying it
   224 Assuming we added entries to the blog titled `MyLife`, displaying it
   227 now allows to read its description and all its entries.
   225 now allows to read its description and all its entries.
   228 
   226 
   229 .. image:: images/lax-book.10-blog-with-two-entries.en.png
   227 .. image:: images/lax-book.10-blog-with-two-entries.en.png
   230    :alt: a blog and all its entries
   228    :alt: a blog and all its entries
   231 
   229 
   232 **Before we move forward, remember that the selection/view principle is
   230 **Before we move forward, remember that the selection/view principle is
   233 at the core of `CubicWeb`. Everywhere in the engine, data is requested
   231 at the core of `CubicWeb`. Everywhere in the engine, data is requested
   234 using the RQL language, then HTML/XML/text/PNG is output by applying a
   232 using the RQL language, then HTML/XML/text/PNG is output by applying a
   235 view to the resultset returned by the query. That is where most of the
   233 view to the result set returned by the query. That is where most of the
   236 flexibility comes from.**
   234 flexibility comes from.**
   237 
   235 
   238 [WRITE ME]
   236 [WRITE ME]
   239 
   237 
   240 * implementing interfaces, calendar for blog entries
   238 * implementing interfaces, calendar for blog entries
   241 * show that a calendar view can export data to ical
   239 * show that a calendar view can export data to ical
   242 
   240 
   243 We will implement the `cubicweb.interfaces.ICalendarable` interfaces on
   241 We will implement the `cubicweb.interfaces.ICalendarable` interfaces on
   244 entities.BlogEntry and apply the OneMonthCalendar and iCalendar views
   242 entities.BlogEntry and apply the OneMonthCalendar and iCalendar views
   245 to resultsets like "Any E WHERE E is BlogEntry"
   243 to result sets like "Any E WHERE E is BlogEntry"
   246 
   244 
   247 * create view "blogentry table" with title, publish_date, category
   245 * create view "blogentry table" with title, publish_date, category
   248 
   246 
   249 We will show that by default the view that displays 
   247 We will show that by default the view that displays 
   250 "Any E,D,C WHERE E publish_date D, E category C" is the table view.
   248 "Any E,D,C WHERE E publish_date D, E category C" is the table view.
   260 
   258 
   261 
   259 
   262 Templates
   260 Templates
   263 ---------
   261 ---------
   264 
   262 
   265 *Templates* are specific view that does not depend on a result set. The basic
   263 *Templates* are specific views that do not depend on a result set. The basic
   266 class `Template` (`cubicweb.common.view`) is derived from the class `View`.
   264 class `Template` (`cubicweb.common.view`) is derived from the class `View`.
   267 
   265 
   268 To build a HTML page, a *main template* is used. In general, the template of
   266 To build a HTML page, a *main template* is used. In general, the template of
   269 identifier `main` is the one to use (it is not used in case an error is raised or for
   267 identifier `main` is the one to use (it is not used in case an error is raised or for
   270 the login form for example). This template uses other templates in addition
   268 the login form for example). This template uses other templates in addition
   278 
   276 
   279 You will find out more about templates in :ref:`templates`. 
   277 You will find out more about templates in :ref:`templates`. 
   280 
   278 
   281 XML views, binaries...
   279 XML views, binaries...
   282 ----------------------
   280 ----------------------
   283 For the views generating other formats that HTML (an image generated dynamically
   281 For views generating other formats than HTML (an image generated dynamically
   284 for example), and which can not usually be included in the HTML page generated
   282 for example), and which can not simply be included in the HTML page generated
   285 by the main template (see above), you have to:
   283 by the main template (see above), you have to:
   286 
   284 
   287 * set the atribute `templatable` of the class to `False`
   285 * set the attribute `templatable` of the class to `False`
   288 * set, through the attribute `content_type` of the class, the MIME type generated
   286 * set, through the attribute `content_type` of the class, the MIME type generated
   289   by the view to `application/octet-stream`
   287   by the view to `application/octet-stream`
   290 
   288 
   291 For the views dedicated to binary content creation (an image dynamically generated
   289 For views dedicated to binary content creation (like dynamically generated 
   292 for example), we have to set the attribute `binary` of the class to `True` (which
   290 images), we have to set the attribute `binary` of the class to `True` (which
   293 implies that `templatable == False`, so that the attribute `w` of the view could be
   291 implies that `templatable == False`, so that the attribute `w` of the view could be
   294 replaced by a binary flow instead of unicode).
   292 replaced by a binary flow instead of unicode).
   295 
   293 
   296 (X)HTML tricks to apply
   294 (X)HTML tricks to apply
   297 -----------------------
   295 -----------------------