Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Some concepts are used in different languages in a different manner. And although the same words are used, they are not the same. It can be quite confusing. This is in my opinion is the source of your question. I will describe three patterns used in templating. Each pattern has its specific relationship between parent and children.</p> <h3>1. differentiating implementations</h3> <p>A template design pattern has strict parent - child relationships. The children are extensions of the parent, which is normally an abstract class. Every child is a different implementation of the parent. An example of an abstract template class is for instance a Shape. Child implementations can be Triangle, Square and Circle. They all share some public abstract methods like draw() and resize() that they all have the same implementation. The purpose of the abstract method is to ensure that they all have an unique, characteristic implementation of that method(for that child). The base class can also have some non abstract methods. Examples are methods like fill() and rotate(). Every child is allowed to override those methods, but it is not needed, if the default implementation suffices. The children will be able to use the public/protected data and methods from the parent, see <a href="http://en.wikipedia.org/wiki/Template_method_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Template_method_pattern</a>.</p> <h3>2. repeatable composition</h3> <p>The template system in html-files serves a similar concept, but with a different purpose. The purpose of the template system in html-files is to create a flexible insertion system of independent blocks. Each block is independent of any other block, hence you can not assume that they have anything in common. They might have, but it is not mandatory. You can compare this with the shared methods of the abstract class, because within every html-file you have to implement it in the same way as you would in other files. They are independent blocks to ensure maximum flexibility in design. The same block of content can be used in different template files without any problem. And they should be able to function this way. This is the composite design pattern. The parent-child relationship can be best described as part-whole relationships, see <a href="http://en.wikipedia.org/wiki/Composite_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Composite_pattern</a>.</p> <h3>3. shared data, unique presentation</h3> <p>Within each html-fragment is the same data (all the data of the current request and session) used. They share the same data, but they should not inherit anything from each other. They also do not share any implementation feature. You can have a block of content in which you show an overview of search findings using a pager. Or you can have a block with a static footer. In every html template there is only one header and one body. There can be however more menus and a particular menu can appear in different shapes on different places. Therefor your should think about the Observer or MVC pattern. The data used is the Model part. It is the same for all Views on the data during one request. Each block is a different view on the Model and will use the information that it requires. No block needs to have knowledge about the state or data of any other block. This in my opinion is the reason why you asked if $_page should be a global? You needed it, because of the shared data. Not because of the html part of the file. (That is a matter of composition.) If you however separate those two parts of the root.php and have a common data object for all blocks, then it can work without the $_page being a global.</p> <h3>Conclusion:</h3> <p>Where the parent-child relationships in the template design pattern is between the classes (not the objects), there is the part-whole relationships for html-files between the blocks of html-fragments and the model-view relationships between the shared data and the required data for any particular html-fragment. The parent-child relationship between the classes is fixed, the part-whole relationships between the html-fragments is flexible, the model-view relationship is standardized, but not fixed.</p> <p>It is most efficient if the data object is only created once and is for the duration of the request unaltered. A global can do that, but a local variable in a method can do that as well, just as a static variable of a class. I would advise you to use that implementation that suits the maintenance of your application best.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload