Message-ID: <32207203.2552.1485845426159.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2551_444198311.1485845426159" ------=_Part_2551_444198311.1485845426159 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Content view

Content view

=20 =20

The ViewController

eZ Publish comes with a native controller to display your content, known= as the ViewController. It is called each tim= e you try to reach a content item from its Url Alias (= human readable, translatable URI generated for any = content based on URL patterns defined per Content Type) and is able to rend= er any content previously edited in the admin interface or via the eZ Publish Public API.

It can also be called directly by its direct URI : /content/locati= on/<locationId>

A content item can also have different view types (full= page, abstract in a list, block in a landing page...). By default the view= type is full (for full page), but it can be anything (line, block...).

Important note regarding visibility

Location visibility flag, which you can change with hide/unhide in admin= , is not permission based and thus acts as a simple potential filter. It is not meant to restrict access to content.

If you need to restrict access to a given content, use Sections = or Object states, which are permission based.

 

View selection

To display a content item, the ViewController uses a view manager which = selects the appropriate template depending on matching rules.

For more information about the=20 view provider configuration, please=20 refer to the dedicated= page.

Location view template

A content view template is like any other template, with several specifi= c aspects.

Available variables

Variable name Type Description
location eZ\Publish\Core\Repository\Values\Conte= nt\Location The location object. Contains meta information o= n the content (ContentInfo)
(only when accessing a locati= on) 
content eZ\Publish\Core\Repository\= Values\Content\Content The content object, containing all= fields and version information (VersionInfo)
noLayout Boolean If true, indicates if the content/= location is to be displayed without any pagelayout (i.e. AJAX, sub-requests= ...).
It's generally false when displaying a content item i= n view type full
viewBaseLayout String The base layout template to use wh= en the view is requested to be generated outside of the pagelayout (when noLayout is true).

Template inheritance

Like any template, a content view template can use template inheritance. However= keep in mind that your content can be also requested via sub-requests (see below how to rend= er embedded content objects). In this case your template should probably no= t extend your main layout.

In this regard, it is recommended to use inheritance this way:

=20
{% extends noLayout ? viewbaseLayout : "AcmeDemoBundle::pagelayout.=
html.twig" %}

{% block content %}
...
{% endblock %}
=20

Rendering content's fields<= /h3>

As stated above, a view template receives the requested Content object, = holding all fields.

In order to display the fields' value the way you want, you can either m= anipulate the Field Value object itself or use a template.

Getting raw Field value

Having access to the Content object in the template, you can use its public methods to access to all the information you need. Y= ou can also use ez_field_value= helper to get the Field value in the current language if translation i= s available.

=20
{# With the following, myFieldValue will be in the content's main l=
anguage, regardless the current language #}
{% set myFieldValue =3D content.getFieldValue( 'some_field_identifier' ) %}
 
{# Here myTranslatedFieldValue will be in the current language if a transla=
tion is available. If not, the content's main language will be used #}
{% set myTranslatedFieldValue =3D ez_field_value( content, 'some_field_iden=
tifier' ) %}
=20

Using the FieldType= 's template block

All built-in FieldTypes come with a piec= e of Twig template code you can take advantage of by calling ez_r= ender_field() helper.

=20
{{ ez_render_field( content, 'some_field_identifier' ) }}
=20

Refer to ez_render_field(= ) reference page for further information.

As this makes use of reusable templates, using ez_render_f= ield() is the recommended way and is to be considered as a best prac= tice.

Rendering Content name

The name of a content item is its generic "title", gene= rated by the repository considering several rules in the FieldDefinition. I= t usually consists in the normalized value of the first field.

There are 2 different ways to access to this special property:

Name property in ContentIn= fo

This property is the actual content name, but in main language o= nly (so it is not translated).

=20
<h2>Content name: {{ content.contentInfo.name }}</h2&=
gt;
=20
=20
$contentName =3D $content->contentInfo->name;
=20

Translated name

The TranslationHelper service is available as of version 5.2 / 2013.09

The translate= d name is held in VersionInfo object, in the names proper= ty which consists of hash indexed by locale. You can easily retrieve it in = the right language via the TranslationHelper service.

=20
<h2>Translated content name: {{ ez_content_name( content=
 ) }}</h2>
<h3>Also works from ContentInfo : {{ ez_content_name( content.content=
Info ) }}</h3>
=20

You can refer to ez_content_nam= e() reference page for further information.

 

=20
// Assuming we're in a controller action
$translationHelper =3D $this->get( 'ezpublish.translation_helper' );
 
// From Content
$translatedContentName =3D $translationHelper->getTranslatedContentName(=
 $content );
// From ContentInfo
$translatedContentName =3D $translationHelper->getTranslatedContentNameB=
yContentInfo( $contentInfo );
=20

The helper will respect the prioritized languages. 

If there is no translation for your prioritized languages, the helper wi= ll always return the name in the main language.

You can also force a locale in a 2nd argument:

=20
{# Force fre-FR locale. #}
<h2>{{ ez_content_name( content, 'fre-FR' ) }}</h2>
=20
=20
// Assuming we're in a controller action
$translatedContentName =3D $this->get( 'ezpublish.translation_helper' )-=
>getTranslatedName( $content, 'fre-FR' );
=20

Exposing additional vari= ables

It is possible to expose additional variables in a content view template= . See p= arameters injection in content views or us= e your own custom controller to render a content item/location.

Making links to other lo= cations

Linking to other locations is fairly easy and is done with native path() Twig helper (or url() if you want to generate absolute URLs). You just = have to pass it the Location object and path() will generate t= he URLAlias for you.

=20
{# Assuming "location" variable is a valid eZ\Publish\API\Repositor=
y\Values\Content\Location object #}
<a href=3D"{{ path( location ) }}">Some link to a location</a><=
/pre>=20

If you don't have the Location object, but only its ID, you can generate= the URLAlias the following way:

=20
<a href=3D"{{ path( "ez_urlalias", {"locationId": 123} ) }}">=
Some link to a location, with its Id only</a>
=20

As of = 5.4 / 2014.11, you can also use the Content ID. In that case gene= rated link will point to the content main location.

=20
<a href=3D"{{ path( "ez_urlalias", {"contentId": 456} ) }}">S=
ome link from a contentId</a>
=20

Under the hood

In the backend, path() uses the Router to generate links.

This makes also easy to generate links from PHP, via the router service.

See also : Cross SiteAcc= ess links

Render embedded content= objects

Rendering an embedded content from a Twig template is pretty straight fo= rward as you just need to do a subrequest with ez_content controller.

Using ez_conten= t controller

This controller is exactly the same as the ViewController presented above and has 2 main action= s:

  • viewLocation to render a location (same a= s when accessing a content item through an URLAlias)
  • viewContent to render a content item

You can use this controller from templates with the following syntax:

eZ Publish 5.1+ / Symfony 2.2+
=20
{{ render( controller( "ez_content:viewLocation", {"locationId": 12=
3, "viewType": "line"} ) ) }}
=20

 

The example above= allows you to render a Location which ID is 123, with the view type line.

Reference of ez_content controller follow the syntax of controllers as a service, a= s explained in Symfony documentation.

Available arguments

As any controller, you can pass arguments to ez_content:viewLocati= on or ez_content:viewContent to fit your needs.

Name Description Type Default value
locationId Id of the location you want to render.
Only for ez_content:viewLocation 
integer N/A
contentId

Id of the content you want to r= ender.
Only for ez_content:viewContent&nbs= p;

integer N/A
viewType

The view type you want to rende= r your content/location in.
Will be used by the ViewManager to select co= rresponding template, according to defined rules. 

Example: full= , line, my_custom_view, ...

string full
layout

Indicates if the sub-view needs= to use the main layout (see av= ailable variables in a view template)

 

boolean false
params

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

Available as of eZ Publish 5.1

=20
{{ render(
      controller( 
          "ez_content:viewLocation", 
          {
              "locationId": 123,
              "viewType": "line",
              "params": { "some_variable": "some_value" }
          }
      )
) }}
=20
hash empty hash

Render block

>=3D EZP 5.4>=3D 2014.11

You can specify which controller will be called for a specific block vie= w match, much like defining custom controllers for location view or content= view match.

Also, since there are two possible actions with which one can view a blo= ck: ez_page:viewBlock and ez_page:viewBlockById, = it is possible to specify a controller action with a signature matching eit= her one of original actions.

Example of configuration in ezpublish/config/ezpublish.yml:=

=20
ezpublish:
    system:
        eng_frontend_group:
            block_view:
                ContentGrid:
                    template: NetgenSiteBundle:block:content_grid.html.twig
                    controller: NetgenSiteBundle:Block:viewContentGridBlock
                    match:
                        Type: ContentGrid
=20

ESI

Just as for regular Symfony controllers, you can take advantage of ESI a= nd use different cache levels:

Using ESI (eZ Publish 5.1+ / Symfony 2.2+)
=20
{{ render_esi( controller( "ez_content:viewLocation", {"locationId"=
: 123, "viewType": "line"} ) ) }}
=20

Only scalable variables can be sent via render_esi (not object)

 

 Asynchron= ous rendering

Symfony also supports asynchronous content rendering with the help of&nb= sp;hinclude.js library.

Asynchronous rendering (eZ Publish 5.1+ / Symfony 2.2+)
=20
{{ render_hinclude( controller( "ez_content:viewLocation", {"locati=
onId": 123, "viewType": "line"} ) ) }}
=20

Only scalable variables can be sent via render_hinclude (not object)

Display a default text

If you want to display a default text while a controller is loaded async= hronously, you have to pass a second parameters to your render_hinclude twi= g function.

Display a default text while asynchronous loading of a controller
=20
{{ render_hinclude( controller( 'EzCorporateDesignBundle:Header:use=
rLinks' ), { 'default': "<div style=3D'color:red'>loading</div>=
" }) }}
=20

See also :  How to use a custom controller to di= splay a content item or location

 

 

hinclude.js needs to be properly included in your layout = to work.

Please refer to Symfony documentation for all available options.

------=_Part_2551_444198311.1485845426159--