Invenzzia » Resources / Articles / Sections and data formats in OPT / Answers

6. Answers

In the first problem your task was to decide, how OPT understands the data format StaticGenerator/Objective/Array. Starting from the left we have: the list is returned by the generator, it is an object, and the list elements are arrays.

Later, we asked for the pros of moving the implementation details from templates to the script:

  1. The template can be created earlier than the script or developed indepentendly. Moreover, the designer can do it much faster, because he does not have to waste his time for implementation details and spend a lot of time on testing the connection.
  2. The templates can serve as a dummy model of the website for the client or for the programmers. They can be introduced to the suggested website structure, and moreover the same templates can be used in the final version of the code without any changes. All we have to do is to change the data formats and feed them with the real data.
  3. Simplified code refactoring. The data are provided as arrays or objects. Sometimes we need to change it due to the optimization or new feature purposes (or both). In OPT we simply change the data format and recompile the template, while in other template engines we would have to rewrite it.

In the third exercise was to select the data formats for nested sections. We want the list of posts to be object, a single post - an array and the tags taken from a generator:

$view->setFormat('entries', 'Objective/Array');
$view->setFormat('tags', 'StaticGenerator/Array');

The StaticGenerator format looks for the generator object in the parent section element, like in SingleArray. After the slash we must add one more format that describes what the generator returns.

The last problem concerns opt:show and nested sections:

<opt:show name="top">
  <opt:section name="middle" parent="top">
    Element.
  </opt:section>
 
  <opt:section>
    Element listy.
  </opt:section>
</opt:show>

The question is whether this code works... and why not. The actual section consists of the repeated code only. The opt:show tag defines the section neighbourhood only and this is why our middle section cannot be connected to it in this particular place. The reason is simple. What parent element we should be connected with, if we have not started displaying the parent elements yet? To fix this code, we must either remove the parent attribute (the middle section is not nested then) or move the section between the empty opt:section tags.