Global navigation

   Documentation Center
   eZ Studio & eZ Platform
     User Manual
     Technical Manual
     Glossary
   eZ Publish 4.x / legacy

 
eZ Publish (5.x)

eZ Publish 5.x | For eZ Platform & eZ Studio topics see Technical manual and User manual, for eZ Publish 4.x and Legacy topics see eZ Publish legacy

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: adding infos about viewBlockById (EZP-23702)

...

Tip
titleTip

You can use a custom controller to display a block.

However, if you do so, you might need to get access to the PageService. You can get it via the service container with identifier ezpublish.fieldType.ezpage.pageService.

Using ez_page:viewBlock

This controller is responsible of choosing the right template for your block, depending on the rules you defined.

...

Info

valid_items and valid_contentinfo_items variables are available as of v5.2 / 2013.11.

Usage of pageService is deprecated as of v5.2 / 2013.11 and will be removed in v6.0.

 

Using pageView:viewBlockById
Status
colourYellow
title>= EZP 5.3.2

You can render Blocks with ESI strategy in all contexts, ie including using Varnish with the controller viewBlockById.

 

Note

This feature is available from eZ Platform 5.3.2.

Available arguments
NameDescriptionTypeDefault value
idThe block ID of the block you want to render
A \eZ\Publish\API\Repository\Exceptions\NotFoundException will be thrown, if block could not be found
stringN/A
params

Hash of variables you want to inject to sub-template, key being the exposed variable name.

Code Block
{{ render(
      controller( 
          "ez_page:viewBlockById", 
          {
              'id': 42,
              'params': { 'some_variable': 'some_value' }
          }
      )
) }}
hashempty
cacheSettings

Hash of cache settings to use by the sub-controller (useful if you use ESI or Hinclude strategies).

Code Block
{{ render_esi(
      controller( 
          "ez_page:viewBlockById", 
          {
              'id': 42,
              'params': { 'some_variable': 'some_value' },
              'cacheSettings': { 'smax-age': 600 }
          }
      )
) }}
hash (accepted keys are max-age and smax-age)empty

 

 

 

Rendering Block items

As said above, a block holds your displayable content as block items which consists of eZ\Publish\Core\FieldType\Page\Parts\Item objects. Among the available properties, you will find contentId and locationId which reference the content/location you want to display. All you have to do then is to render it view ez_content:viewLocation or ez_content:viewContent (see full example below).

...

Code Block
languagehtml/xml
title2zoneslayout1.html.twig
<h2>TWIG<h2>Twig Template for 2zoneslayout12 zoneslayout1 zone</h2>
<div class="zone-layout-{{ zone_layout|lower }} row">
    <div class="span8">
        <section class="content-view-block">
        {% if zones[0].blocks %}
            {# Rendering blocks with default PageController #}
            {% for block in zones[0].blocks %}
                {{ render( controller( "ez_page:viewBlock", {'block': block} ) ) }}
            {% endfor %}
            <div class="block-separator"></div>
        {% endif %}
        </section>
    </div>
    <div class="span4">
        <aside>
            <section class="content-view-block content-view-aside">
            {% if zones[1].blocks %}
                {# Still rendering with default PageController, but passing specific cache value (TTL of 100 seconds) and using ESI #}
                {% for block in zones[1].blocks %}
                    {{ render_esi( controller( "ez_page:viewBlock", {'block': block, 'cacheSettings': {'smax-age': 100}} ) ) }}
                {% endfor %}
                <div class="block-separator"></div>
            {% endif %}
            </section>
        </aside>
    </div>
</div>
Code Block
languagehtml/xml
titlecampaign_block.html.twig
<h3>TWIG<h3>Twig Template for Campaign blockBlock type</h3>
{% set validContentInfoItems = pageService.getValidBlockItemsAsContentInfo( block ) %}
{% set validItems = pageService.getValidBlockItems( block ) %}
<!-- BLOCK: START -->
<div class="block-type-campaign">
    <div class="campaign">
        <a href="#" class="navig prev" style="opacity:0;"><span class="hide">&lt;</span></a>
        <a href="#" class="navig next"><span class="hide">&gt;</span></a>
        <ul class="indicator">
            {% for contentInfo in validContentInfoItems %}
                <li><span>{{ contentInfo.name }} (#{{ contentInfo.id }})</span></li>
            {% endfor %}
        </ul>
        <ul class="images">
        {# Rendering valid items with regular view controller, with "block_item_campaign" view type #}
        {# Also passing an "image_class" parameter which will be available in sub-template. #}
        {% for item in validItems %}
            {{ render(
                controller(
                    'ez_content:viewLocation',
                    {
                        'locationId': item.locationId,
                        'viewType': 'block_item_campaign',
                        'params': {'image_class': 'campaign'}
                    }
                )
            ) }}
        {% endfor %}
        </ul>
    </div>
</div>
<!-- BLOCK: END -->