Lesson 4.2: Datatypes and their templates
Every attribute is shown by the template of its datatype: a text line by ezstring.tpl, rich text by its own,
an image by ezimage.tpl. This lesson shows learners where those templates are, what the content of an attribute
is for different datatypes, and how to change the view of one attribute of one class with an override instead of
changing the datatype template for the whole site.
At a glance
| Course | Developers |
| Module | Module 4: Content classes and datatypes |
| Duration | 45 minutes (15 presenting, 30 exercise) |
| Level | Intermediate |
| Audience | Developers and template builders |
| Prerequisites | Lesson 4.1 and Lesson 3.2 |
Learning objectives
After this lesson, learners can:
- find a datatype's templates in
content/datatype/view/andcontent/datatype/edit/; - read an attribute's
data_type_string,has_contentandcontentin a template; - explain why
contentis a string forezstringbut an object forezxmltextorezimage; - override the view template of one attribute with
Match[attribute_identifier]andMatch[class_identifier].
Trainer's notes
How to present it
Sources: kernel/common/eztemplateautoload.php (the definition of attribute_view_gui) and
design/standard/templates/content/datatype/.
- Where the templates are (3 minutes).
ls design/standard/templates/content/datatype/view/and.../edit/: one file per datatype string,ezstring.tpl,ezimage.tpland so on.attribute_view_guiusesview/,attribute_edit_gui(in the edit form)edit/. An information collector attribute usescollect/. - What
contentis (4 minutes).$attribute.contentis whatever the datatype returns: a string forezstring, an integer forezinteger, an object forezxmltextorezimage.$attribute.has_contentsays whether something is stored. Datatype templates know their content; generic code should not. - The override keys of an attribute (4 minutes).
attribute_view_guisets the keysattribute_identifier,attribute,class_identifierandclass. An override withSource=content/datatype/view/ezstring.tpl,Match[class_identifier]=noteandMatch[attribute_identifier]=titlechanges only the title of notes. - Do not edit the kernel's datatype templates (2 minutes). The override touches one field; editing
ezstring.tplin your design would change every text line of the site. has_contentbefore printing (2 minutes).{if $node.data_map.image.has_content}avoids empty markup.
Common questions
How do I show an image in a given size? The image datatype's template takes an image_class parameter, for
example {attribute_view_gui attribute=$node.data_map.image image_class='medium'}; the sizes are image aliases in
image.ini.
Can I pass my own parameters to a datatype template? Yes, any extra parameter of attribute_view_gui becomes a
variable in the template.
Where is the edit form's template? In content/datatype/edit/<datatype>.tpl, used by attribute_edit_gui in
the content edit form.
Pitfalls
- Printing
contentof a rich text field.{$node.data_map.body.content}prints an object, not HTML. Useattribute_view_gui. - The identifier of the wrong class.
attribute_identifier=titlematches every class with atitleunless the rule also hasMatch[class_identifier]. - Forgetting the caches. Like every new override:
ini,template-override,templateandcontent.
Hands-on exercise
On the development installation, with the Note class from lesson 4.1 and one note published. Use the playground
from lesson 2.1, changing the node ID in play.php to the note's node ID.
List the datatype templates:
ls design/standard/templates/content/datatype/view/.In
play.tpl, print for every attribute its identifier, datatype and whether it has content:foreach $node.data_map as $identifier => $attribute $identifier: $attribute.data_type_string cond( $attribute.has_content, 'filled', 'empty' ) /foreachPrint the title's content directly and through its datatype template, and compare:
[$node.data_map.title.content|wash] [attribute_view_gui attribute=$node.data_map.title]Open
design/standard/templates/content/datatype/view/ezstring.tpland read it.Create the override template
extension/mysite_hello/design/<design>/override/templates/datatype/note_title.tpl:<span class="note-title">$attribute.content|wash|upcase</span>Add the rule in the extension's siteaccess scope with
exp:ini setas in lesson 3.2: blockmysite_note_title,Source=content/datatype/view/ezstring.tpl,MatchFile=datatype/note_title.tpl,Subdir=templates,Match[class_identifier]=note,Match[attribute_identifier]=title.Clear the
ini,template-override,templateandcontentcaches and open the note on the site.Open an article: its title is unchanged.
Commit the template and the settings file.
Expected result
- Step 2 prints
title: ezstring filledandbody: eztext filled(orempty). - Step 3 prints the same text twice; the second from
ezstring.tpl. - Step 7: the note's title is in capitals inside
<span class="note-title">; with path comments on, the comment shows(content/datatype/view/ezstring.tpl). - Step 8: articles are unchanged.
- The trainer checks one learner's settings file for the two
Matchlines.
Quiz
- In which folder does
attribute_view_guilook for the template of anezimageattribute? - Which two keys limit an override to the title of notes?
A.
nodeandobjectB.class_identifierandattribute_identifierC.sectionandviewmodeD.classanddepth - What does
$attribute.has_contenttell you? - Why should
ezstring.tplnot be copied into your design to change one class's title? - What is
$node.data_map.body.contentfor a rich text field?
Answers
content/datatype/view/ezimage.tplin the design chain.- B.
Match[class_identifier]=noteandMatch[attribute_identifier]=title. - Whether a value is stored in the attribute.
- It would change every text line of the site; an override limits the change.
- An object that holds the structured rich text, not HTML; its datatype template renders it.
Further reading
- The content model and editing content: datatypes in the editor.
- Rich text: supplementary characters and TeX source: how rich text is stored.
- Templates and design: overrides.
Related pages
- Module 4: Content classes and datatypes
- 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.