Move all files to sculpin/
This commit is contained in:
parent
c5d71803a5
commit
0f61b4e9ee
1514 changed files with 0 additions and 0 deletions
22
sculpin/source/_posts/2023-10-12.md
Normal file
22
sculpin/source/_posts/2023-10-12.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
---
|
||||
title: >
|
||||
Business logic in template files?
|
||||
pubDate: 2023-10-12
|
||||
permalink: >-
|
||||
daily/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…
Add table
Add a link
Reference in a new issue