Skip to the content
Documentation

Outputting node and object data

No ratings yet

A node template has the node in $node, and a fetch returns nodes too. This page shows how to print what a node and its object hold.

Node and object

A node is a place in the content tree; its object holds the content and can have several nodes. Both are reached from $node:

{$node.name|wash}                                  {* the object's name, in the current language *}
{$node.node_id} {$node.parent_node_id} {$node.depth}
{$node.url_alias|ezurl}                            {* the node's address *}
{$node.class_identifier}                           {* such as article *}
{$node.object.id}
{$node.object.published|l10n( 'shortdatetime' )}   {* first published *}
{$node.object.modified|l10n( 'shortdatetime' )}    {* last changed *}
{$node.object.owner.name|wash}                     {* who created it *}
{$node.object.current.creator.name|wash}           {* who made the current version *}

Dates are Unix timestamps; l10n formats them for the locale of the siteaccess ('shortdate', 'date', 'shortdatetime', 'datetime', 'time'), and datetime( 'custom', '%Y-%m-%d' ) formats them as you write. Every attribute: node attributes and object attributes.

Content attributes

The fields of an object, such as the title, the body or the image of an article, are its content attributes. $node.data_map (short for $node.​object.​data_​map) is a hash of them, by the attribute identifiers of the class:

{attribute_view_gui attribute=$node.data_map.title}
{attribute_view_gui attribute=$node.data_map.body}
{attribute_view_gui attribute=$node.data_map.image image_class='medium'}

attribute_view_gui renders the attribute with the view template of its datatype, content/​datatype/​view/​<datatype>.​tpl of the design chain, so a rich text is rendered as HTML, an image as an img element in the image alias you ask for, a date in the locale. This is the right way to print an attribute, and the datatype templates can be overridden like any other.

The raw value

For a test or a value you process further, read the attribute's content directly. What .content is depends on the datatype:

{if $node.data_map.image.has_content}...{/if}             {* is the attribute filled? *}
{$node.data_map.title.content|wash}                        {* a text line: a string *}
{$node.data_map.price.content.price|l10n( 'currency' )}    {* a price: an object *}
{$node.data_map.image.content['medium'].url|ezroot}        {* an image alias *}

Raw values bypass the datatype's escaping: wash every string you print this way (see Template security). What .content holds for each datatype: Content attribute reference and data_map and content.

Which identifiers a class has is shown in the administration interface under Setup, Classes, and in a template with {$node.​data_​map|attribute( 'show', 1 )} (see Array and object inspection).

In this section

Contributors

Comments (12 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. node display

    heiner ·

    to display the values of the node variable:

    {$node|attribute(show,2)}
  2. For an speed boost and lowering number of fetches / sql quries

    AtR ·

    If you need to display some information about the children or grand children in the node template this is possible with $node.children and $node.children.0.children (if any children exist this will loop thru any children of your first child)
    To loop thru children:

    &lt;table>
    {foreach $node.children as $nodechild}
       &lt;tr>
        &lt;td>&lt;a href={$nodechild.url_alias|ezurl}>{$nodechild.name}&lt;/a>&lt;/td>
        &lt;td>{$nodechild.node_id}&lt;/td>
      &lt;/tr>
    {/foreach}
    &lt;/table>

    The same thing can be done with parents: $node.parent
    Remember to only use $node in node templates and not in your pagelayout file.
    Because it's not available on cached pages (ViewCaching).

    1. Re: Re: For an speed boost and lowering number of fetches / sql quries

      André ·

      This is wrong!
      .children and some other variables are dynamicly feched if you use them.
      So it's better to use tree fetch if you want content on different levels!

  3. Show

    Neeraj ·

    What is show? A function?
    I don't see it documented anywhere but I can see its being used in many templates.

  4. raw output of an image attribute

    Bergfrid Skaara ·

    Under some circumstances, in my case HostMatchType = URL and non-virtual host mode, the following syntax

    &lt;img src="{$node.object.data_map.my_image.content[image_size].full_path}" ... />

    does not provide a sufficient path and the image will consequently not be displayed.
    In that case do the following steps:
    1) check the value of the src parameter in the source code of your page
    2) compare that value to the one you get by using attribute_view_gui to show my_image
    3) if the difference is that the latter starts with the name of the folder eZ publish is installed in, then you should include the ezroot operator like this (note the removal of quote marks):

    &lt;img src={$node.object.data_map.my_image.content[image_size].full_path|ezroot} ... />

    ps: this issue is reported in bug report no. 7917

  5. missing example how to display value of Selection object

    vytis ·

    there is missing example, how to display value of Selection object
    is it correct?
    $node.data_map.my_selection.class_content.options[$node.data_map.my_selection.data_text].name

  6. Working example

    Mike LECOMTE ·

    <ul>
    {for 0 to $node.object.data_map.options02.content|count()|dec as $counter}
    <li> ID : {$node.object.data_map.options02.content[$counter]} - VALUE : {$node.object.data_map.options02.class_content.options[$node.object.data_map.options02.value[$counter]].name}</li>
    {/for}
    </ul>

  7. dynamically accessing data_map

    zurgutt ·

    Apparently you can do this, to dynamically access attributes:

    {foreach $attribute_names as $attname}
        {$node.object.data_map.[$attname].content}
    {/foreach}
  8. Relations

    P. Renaud ·

    You could find these informations elsewhere, but here are some useful things :
    - output data only if attribute has some content :

    {if $node.data_map.my_attribute.has_content}
        {* do whatever must be done *}
    {/if}

    - if my_attribute is an object relation :

    {def $my_relation_object=$node.data_map.my_attribute.content.main_node}
    {* display $my_relation_object *}

    - if my_attribute is an object relation list :

    {def $my_relation_list_object=$node.data_map.my_attribute.content.relation_list}
    {foreach $my_relation_list_object as $current_object}
        {* display $current_node *}
    {/foreach}
  9. Language ID

    Andry ·

    Hi, I'm a newbie, can anybody share how to output the language id ?
    Thanks

Comments

Add a comment

Comments are read by a moderator before they appear.