daily-email: add 2023-10-12
Business logic in template files?
This commit is contained in:
parent
bfa5ac13dc
commit
30a78a65f6
22
src/content/daily-email/2023-10-12.md
Normal file
22
src/content/daily-email/2023-10-12.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
---
|
||||
title: >
|
||||
Business logic in template files?
|
||||
pubDate: 2023-10-12
|
||||
permalink: >
|
||||
archive/2023/10/12/business-logic-in-template-files
|
||||
tags:
|
||||
- software-development
|
||||
- drupal
|
||||
- twig
|
||||
- automated-testing
|
||||
---
|
||||
|
||||
I've often heard and advocated for not "putting logic" in template files and having a separation of concerns.
|
||||
|
||||
The templates should be as simple to read and change as possible, and any complicated logic should be moved elsewhere.
|
||||
|
||||
There can be presentational logic - such as a simple if condition to determine if a value should be shown or looping over a list of items, but business logic should be within custom code - ideally within Service, Action or Command classes - and injected into the templates.
|
||||
|
||||
As well as making the templates simpler and cleaner, this logic can be tested separately with automated tests to ensure it works as expected. If the logic is within a template, this can be done for some things using Functional tests, such as whether some text appears on a page, but testing whether it appears in a certain order, for example, is much harder. This is best done within kernel or unit tests.
|
||||
|
||||
As well as being more testable, extracting the logic from a template makes it more reusable. You can use a service from multiple places within your website without duplicating it, making your website code smaller and easier to maintain.
|
Loading…
Reference in a new issue