09. Blocks - ihofmann/open-websoccer GitHub Wiki
In contrast to pages, blocks are only fragments of HTML output code. They are added to existing pages. They typically enhance the functionality of a page or will be loaded via AJAX.
Blocks will be registered at the module configuration file module.xml (see also: Create new pages), like as follows:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE module SYSTEM "../module.dtd">
<module version="1.0.0">
<blocks>
<block
id="mynewblock"
template="mynewblock"
model="MyNewBlockModel"
includepages="all"
excludepages="home,profile"
area="sidebar"
weight="3"
role="guest,user" />
</blocks>
</module>
The attributes of element block have the following meanings:
- id: Unique ID of the block. Enables the integration into a page (see paragraph below).
- template: Template file name, without suffix.
- model: Optional. Name of the model class.
- includepages: Indicates the pages on which the block will be visible. Can be either a comma separated list of page IDs or _all: (block is visible on all pages).
- excludepages: Optional. Indicates exceptions from the rules defined at "includepages". In the example above, the block will be visible on every page but home and profile.
- area: Indicates at which area the block shall be integrated. Following values are possible:
- sidebar: Block appears at the sidebar.
- content_top: Block appears at the content area above the actual content.
- content_bottom: Block appears at the content area below the actual content.
- custom: The block does not appear automatically, but must be integrated manually (see chapter below).
- weight: Optional. If multiple blocks are defined (for instance for sidebar), then they are sorted by this weight.
- role: The block will be shown only to the specified user role(s).
Blocks are implemented the same way pages are done. The only difference: Template file are located in the folder _blocks: (instead of views) and do not extend the base.twig template.
An example block displayed at the sidebar within a box:
{% extends "box.twig" %}
{% block box_title %}
{{ i18n.getMessage('mynewblock_title') }}
{% endblock %}
{% block box_content %}
<p>{{ i18n.getMessage('mynewblock_content') }}</p>
{% endblock %}
If you have registered a block with area="custom", you have to integrate it manually. Just add the below code at the desired place within your page:
{{ viewHandler.renderBlock('mynewblock')|raw }}
Often times you want to display additional information on click on a link. You can implement this with blocks and AJAX. Read the corresponding manual page about the usage of AJAX.
For AJAX links and forms, you can specify an additional attribute which will load a block into the target element: data-ajaxblock. The following example shows how to implement a hyper link which loads additional information on click:
<a href="#mylink" class="ajaxLink"
data-ajaxtarget="additionalinfo"
data-ajaxblock="mynewblock"
data-ajaxquerystr="id=20">More information</a>
<div id="additionalinfo"></div>