65 lines
3.3 KiB
YAML
65 lines
3.3 KiB
YAML
uuid:
|
|
- value: 898e3ece-caad-49b2-8486-400e653eea9d
|
|
langcode:
|
|
- value: en
|
|
type:
|
|
- target_id: daily_email
|
|
target_type: node_type
|
|
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
|
|
revision_timestamp:
|
|
- value: '2025-05-11T09:00:30+00:00'
|
|
revision_uid:
|
|
- target_type: user
|
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
|
revision_log: { }
|
|
status:
|
|
- value: true
|
|
uid:
|
|
- target_type: user
|
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
|
title:
|
|
- value: |
|
|
Business logic in template files?
|
|
created:
|
|
- value: '2023-10-12T00:00:00+00:00'
|
|
changed:
|
|
- value: '2025-05-11T09:00:30+00:00'
|
|
promote:
|
|
- value: false
|
|
sticky:
|
|
- value: false
|
|
default_langcode:
|
|
- value: true
|
|
revision_translation_affected:
|
|
- value: true
|
|
path:
|
|
- alias: /daily/2023/10/12/business-logic-in-template-files
|
|
langcode: en
|
|
body:
|
|
- value: |
|
|
<p>I've often heard and advocated for not "putting logic" in template files and having a separation of concerns.</p>
|
|
|
|
<p>The templates should be as simple to read and change as possible, and any complicated logic should be moved elsewhere.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
|
|
format: full_html
|
|
processed: |
|
|
<p>I've often heard and advocated for not "putting logic" in template files and having a separation of concerns.</p>
|
|
|
|
<p>The templates should be as simple to read and change as possible, and any complicated logic should be moved elsewhere.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
|
|
summary: null
|
|
field_daily_email_cta: { }
|