- Drupal 9 Module Development
- Daniel Sipos Antonio De Marco
- 445字
- 2021-06-11 18:36:05
Business logic versus presentation logic
We start this chapter by discussing an important architectural choice that modern applications make: how to turn data into 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 thing as a giant string, as shown in the following example?
return '<p class="wrapper">' . $data . '</p>';
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 use 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.
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 may 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—that initial look and feel that is independent of design and that should display the data 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 that 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.
- 人性之根
- 區(qū)塊鏈國際監(jiān)管與合規(guī)應(yīng)對(duì)
- 直播電商:從消費(fèi)紅利到數(shù)智創(chuàng)新
- 秒懂視頻號(hào):短視頻變現(xiàn)+直播帶貨+私域?qū)Я?流量運(yùn)營
- 淘寶開店一點(diǎn)通
- 共鳴:內(nèi)容運(yùn)營方法論
- 電商密碼
- 玩轉(zhuǎn)電商:商業(yè)分析+運(yùn)營推廣+營銷技巧+實(shí)戰(zhàn)案例
- 去淘寶網(wǎng)開店賺錢
- 微信的秘密:智能手機(jī)時(shí)代的新營銷寶典
- 興趣電商:短視頻運(yùn)營與流量變現(xiàn)
- 抖音上的財(cái)富:帶貨盈利月入5萬秘技大公開
- 二維碼的秘密:智能手機(jī)時(shí)代的新營銷寶典
- 社交新零售:圈層經(jīng)濟(jì)的整合與變現(xiàn)
- 直播帶貨:口才訓(xùn)練+成交技巧+高效溝通