Lesson 5.1: Fetching nodes and lists
fetch() is how a template asks the kernel for data: a node, a list of children, the current user. Every module can
offer fetch functions, and each is described in its function_definition.php. This lesson teaches the content
fetches every template uses and shows learners where to read the parameters of any fetch.
At a glance
| Course | Developers |
| Module | Module 5: Fetch functions |
| Duration | 45 minutes (15 presenting, 30 exercise) |
| Level | Intermediate |
| Audience | Developers and template builders |
| Prerequisites | Module 2: the template playground |
Learning objectives
After this lesson, learners can:
- write
fetch( '<module>', '<function>', hash( ... ) )and read its result; - load one node with
fetch( 'content', 'node', hash( 'node_id', ... ) ); - list children with
fetch( 'content', 'list', ... )and count them withlist_count; - find a fetch function and its parameters in
kernel/<module>/function_definition.php; - explain why a list fetch needs a
limit.
Trainer's notes
How to present it
Sources: kernel/content/function_definition.php and
Templates and design, part 6.
- The form (3 minutes).
fetch( 'content', 'list', hash( 'parent_node_id', $node.node_id, 'limit', 5 ) ): module, function, named parameters. The result is whatever the function returns, here an array of nodes. - Where fetches are defined (4 minutes). Open
kernel/content/function_definition.php. Each$FunctionList['<name>']names the PHP method (eZContentFunctionCollection::fetchContentNodefornode) and its parameters with type, default and whether they are required. List the content functions:grep -o "FunctionList\['[a-z_]*'\]" kernel/content/function_definition.php. node(2 minutes). Parametersnode_id,node_path,language_codeandremote_id.listandlist_count(4 minutes).listtakesparent_node_id,limit,offset,sort_by,class_filter_type,class_filter_array,attribute_filter,depth,language,load_data_mapand more;list_counttakes the same filters and returns a number. Always fetch with alimitand count withlist_countfor paging.- Permissions (2 minutes). Content fetches return only what the current user may read. An anonymous visitor sees fewer items than an editor.
Common questions
fetch or $node.children? $node.children loads every child without limit or sorting control; fetch with a
limit loads one page.
Why does my fetch return nothing in a script? Scripts run as the anonymous user unless they log in a user; fetches respect permissions.
Can I fetch by path? fetch( 'content', 'node', hash( 'node_path', 'about-us' ) ) finds a node by its URL alias.
Pitfalls
- A list without
limit. On a folder with thousands of items the page becomes slow and memory-hungry. - Fetching in a loop. A
fetchinside aforeachruns once per item; fetch once before the loop when you can. - Parameter names. A misspelt parameter is ignored silently; compare with
function_definition.php.
Hands-on exercise
Use the template playground (extension/mysite_hello/scripts/play.tpl) and run it after each step.
Load node 2 with a fetch instead of the variable:
def $top = fetch( 'content', 'node', hash( 'node_id', 2 ) ) [$top.name|wash]List its five newest children:
{def $items = fetch( 'content', 'list', hash( 'parent_node_id', 2, 'limit', 5, 'sort_by', array( 'published', false() ) ) )} foreach $items as $item$item.name|wash ($item.class_identifier)delimiter, /delimiter/foreachCount all children:
{fetch( 'content', 'list_count', hash( 'parent_node_id', 2 ) )}.List the content fetch functions in a shell:
grep -o "FunctionList\['[a-z_]*'\]" kernel/content/function_definition.php.Read the parameters of
listinkernel/content/function_definition.phpand findclass_filter_array.Load your course folder by its node ID and list its children with a
limitof 3.Compare with the administration: the same items, in the order you chose.
Free the variables with
{undef}and commit the template.
Expected result
- Step 1 prints the name of node 2.
- Step 2 prints up to five names with their class identifiers, newest first.
- Step 3 prints a number at least as large as the list of step 2.
- Step 6 lists the learner's own items from the earlier modules.
- The trainer asks each learner to name the PHP method behind
fetch( 'content', 'node', ... ).
Quiz
- What are the three parts of a fetch call?
- Which fetch returns the number of children without loading them?
A.
listB.list_countC.treeD.node - Where are the parameters of the content fetch functions defined?
- Why does an anonymous visitor see fewer items in a fetch than an editor?
- Name two parameters of
fetch( 'content', 'node', ... ).
Answers
- The module, the function name and a hash of named parameters.
- B.
list_count. - In
kernel/content/function_definition.php. - Content fetches return only what the current user may read.
- Two of
node_id,node_path,language_code,remote_id.
Further reading
- Templates and design: the language and a complete list template.
- The content model and editing content, part 9: the class filter default for fetches.
- Access and cache filters: how extensions can take part in access checks and view caching.
Related pages
- Module 5: Fetch functions
- Developers: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.