官术网_书友最值得收藏!

Business logic versus presentation logic

We will start this chapter by discussing an important architectural choice modern applications make--how to turn data into a presentation.

So, what would be the best way to get our data and functionality marked up? Do we simply wrap each piece of data in HTML and return the whole as a giant string, as shown in the following example?

return '<div class="wrapper">' . $data . '</div>';

No, we don't. Like all other well-designed applications, Drupal separates its business logic from its presentation logic. It's true, previous versions of Drupal did have this kind of approach, especially when it came to theme functions, but even so, they were easily overridable. So, constructs like these were not found smack in the middle of business logic but were encapsulated in a special theming function that was called by the client code. So, the separation of business logic from presentation logic was clearly there, if at times, not so much one between PHP and HTML code.

Traditionally, the primary motivations for this separation of concerns were as follows:

  • To make the code easier to maintain
  • To make it possible to easily swap out one layer's implementation without having to rewrite the other layers

As we will see, Drupal takes the "swapability" aspect quite far. You might think that the theme you select on the Appearance page is responsible for applying the HTML and CSS for the website. This is true, but only to some extent. There are thousands of contributed modules on Drupal.org. Also, you can write a bunch of your own. Should the theme be responsible for marking up all of those modules' data? Obviously not.

Since a module is most intimately familiar with its own data and functionality, it is its own responsibility to provide the default theme implementation (a theme hook)--that initial look and feel that is independent of design but that should be displayed correctly, regardless of the theme. However, as long as the module uses the theme system properly, a theme will be able to override any HTML and/or CSS by swapping the module's implementation with its own.

In other words, after data is retrieved and manipulated inside the module (the business logic), it will need to provide the default theme implementation to wrap it inside its markup. Sometimes a particular theme will need to override this implementation in order for it to achieve a specific design goal. If the theme provides its own implementation, Drupal will use the theme implementation instead of the module's default implementation. This is usually called overriding. Otherwise, the default fallback will still be there. The theme also provides the option of applying styling via CSS only and leaving the markup provided by the module intact.

主站蜘蛛池模板: 祁阳县| 山东省| 从江县| 达日县| 梧州市| 湘乡市| 枣强县| 云梦县| 汤原县| 凤庆县| 文安县| 崇明县| 平远县| 明水县| 昭平县| 正蓝旗| 江门市| 胶南市| 沈丘县| 抚远县| 舟山市| 赫章县| 政和县| 通海县| 赤城县| 徐汇区| 杭锦后旗| 浮梁县| 金寨县| 安丘市| 建水县| 平昌县| 泸州市| 凤山县| 武城县| 霍山县| 芒康县| 吐鲁番市| 平顺县| 蒲江县| 寿阳县|