Development

Changeset 1304

You must first sign up to be able to contribute.

Changeset 1304

Show
Ignore:
Timestamp:
05/03/06 09:41:50 (3 years ago)
Author:
francois
Message:

Updated cache documentation and removed echo in front of the include_partial helpers calls

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/doc/book/content/cache.txt

    r1273 r1304  
    1414As all the pages may contain dynamic information, the HTML cache is disabled by default. It is up to the site administrator to activate it in order to improve performance. 
    1515 
    16 Symfony handles three different types of HTML cache: 
    17  
    18 * cache of the result of an action 
     16Symfony handles four different types of HTML cache: 
     17 
     18* cache of an action 
     19* cache of a partial, a component, or of a component slot 
    1920* cache of an entire page 
    2021* cache of a fragment of a template 
    2122 
    22 The two first types are handled with YAML configuration files, the last one is managed by calls to helper functions in the template. 
     23The three first types are handled with YAML configuration files, the last one is managed by calls to helper functions in the template. 
    2324 
    2425The symfony cache system uses files stored on the web server hard disk. This keeps the cache simple and efficient, without any other prerequisites than the framework itself. It is not yet possible to cache in memory or in a database. 
     
    4344One other cache parameter can be changed in the `settings.yml` file: The default cache lifetime, i.e. the number of seconds after which a cached file is overwritten and the page processed again. The default `default_cache_lifetime` is set to one day, or 86400 seconds. 
    4445 
    45 Caching the result of an action 
    46 ------------------------------- 
    47  
    48 Actions that display static information (i.e. without any call to a database) or actions that read the information in a database but without modifying it are often good clients for caching. 
     46Caching an action 
     47----------------- 
     48 
     49Actions that display static information (i.e. without any call to a database) or actions that read the information in a database but without modifying it (typically GET requests) are often good clients for caching. 
    4950 
    5051For instance, imagine an action that returns the list of all the users of your website (`user/list`). Unless a user is modified, added or removed (and this matter will be discussed later), this list always displays the same information, so it is a good candidate for caching. 
     
    6162      lifeTime:   86400 
    6263   
    63 This configuration stipulates that the cache is `on` for the `list` action, and that it is of the `slot` type (i.e. caching the result of an action, as opposed to caching the whole page, which will be described later). The `lifeTime` is the time (in seconds) after which the page will be processed again and the cached version replaced.  
     64This configuration stipulates that the cache is `on` for the `list` action, and that it is of the `slot` type (i.e. caching an action, as opposed to caching the whole page, which will be described later). The `lifeTime` is the time (in seconds) after which the page will be processed again and the cached version replaced.  
    6465 
    6566Now, if you try to call this action from your browser (probably by requesting an URL like `http://myapp.example.com/user/list`), you will notice no difference the first time, but refreshing the page will probably show a notable boost in response time.  
     
    7273 
    7374Now, requests like `http://myapp.example.com/user/show/id/12` will create new records in the cache folder and if you repeat this request, it will be much faster the second time.  
     75 
     76Caching a partial, a component, or a component slot 
     77--------------------------------------------------- 
     78 
     79The [view chapter](view.txt) explains how to reuse code fragments across several templates, using the `include_partial()` helper. 
     80 
     81    [php] 
     82    <?php include_partial('mymodule/mypartial') ?> 
     83 
     84A partial is as easy to cache as an action. To activate it, create a `cache.yml` in the partial module `config/` directory (in the example above, in `modules/mymodule/config/`) and activate the cache for the partials by declaring their names with a trailing underscore: 
     85 
     86    _mypartial: 
     87      activate: on 
     88     
     89    all: 
     90      activate: off 
     91 
     92Now all the templates using this partial won't actually execute the PHP code of the partial, but use the cached version instead.  
     93 
     94>**Note**: The `slot` type cache is more powerful than the partial cache, since when an action is cached, the template is not even executed - and if the template contains calls to partials, theses calls are not performed. Therefore, the partial caching is useful only if you don't use action caching in the caling action. 
     95 
     96Just like for actions, partial caching is also relevant when the result of the partial depends on parameters: 
     97 
     98    [php] 
     99    <?php include_partial('mymodule/my_other_partial', array('foo' => 'bar)) ?> 
     100 
     101A component is a lightweight action put on top of a partial. A component slot is a component for which the action varies according to the calling actions. These two inclusion types are very similar to partials, and support caching the same way. For instance, if your global layout shows a: 
     102 
     103    [php] 
     104    <?php include_component('global/day') ?> 
     105     
     106...in order to show the current date, then you can cache this component with such a `cache.yml`: 
     107 
     108    _day: 
     109      activate: on 
     110 
     111>**Note**: Global components (the ones located in the application `templates/` directory) can be cached, provided you declare the activation in the application `cache.yml` file. 
    74112 
    75113Caching an entire page 
     
    98136...the full pages will be completely cached, and the second time you request them, the response will be even faster than with the action result cached. 
    99137 
    100 Unfortunately, the layout often contains some dynamic elements, including [slots](view.txt). So the cases where the whole page can be cached are not very common. As a matter of fact, this type is often most used for RSS feeds, pop-ups, or pages with a layout that doesn't depend on cookies. 
     138Unfortunately, the layout often contains some dynamic elements, including [component slots](view.txt). So the cases where the whole page can be cached are not very common. As a matter of fact, this type is often most used for RSS feeds, pop-ups, or pages with a layout that doesn't depend on cookies. 
    101139 
    102140Caching a template fragment 
     
    174212The files stored in these folders depend on the type of caching used: 
    175213 
    176 cache type | file name 
    177 ---------- | --------- 
    178 slot       | slot.cache 
    179 page       | page.cache 
    180 fragment   | fragment_users.cache 
    181            | fragment_other_unique_name.cache 
    182            | ... 
     214cache type                 | file name 
     215-------------------------- | --------- 
     216slot, partial, components  | slot.cache 
     217page                       | page.cache 
     218fragment                   | fragment_users.cache 
     219                           | fragment_other_unique_name.cache 
     220                           | ... 
    183221       
    184222The name given to a fragment in the `cache()` helper is concatenated after `fragment_`. Here, the second fragment was generated by: 
     
    188226    ... 
    189227 
    190 If you use slots in the layout, remember that their cache files are found in a tree structure relative to the module/action of the slot, not the module/action initially called. 
     228If you use component or component slots in the layout, remember that their cache files are found in a tree structure relative to the module/action of the slot, not the module/action initially called. 
    191229       
    192230Feel free to browse the cache folder to look at the chunks of code that are saved; you might feel more comfortable with the cache if you see that it only saves what it is supposed to save. 
  • trunk/doc/book/content/templating_configuration.txt

    r1118 r1304  
    371371    ... 
    372372    <div id="adviceBlock"> 
    373       <?php echo include_component_slot('advices') ?> 
     373      <?php include_component_slot('advices') ?> 
    374374    </div> 
    375375    ... 
  • trunk/doc/book/content/view.txt

    r1118 r1304  
    351351 
    352352    [php] 
    353     <?php echo include_component('news', 'headlines') ?> 
     353    <?php include_component('news', 'headlines') ?> 
    354354     
    355355Just like the partials, components accept additional parameters in the shape of an associative array. 
     
    368368    ... 
    369369    <div id="#sidebar"> 
    370       <?php echo include_component_slot('sidebar') ?> 
     370      <?php include_component_slot('sidebar') ?> 
    371371    </div>  
    372372