{ "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?\n" } ], "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": "\n
I've often heard and advocated for not \"putting logic\" in template files and having a separation of concerns.<\/p>\n\n
The templates should be as simple to read and change as possible, and any complicated logic should be moved elsewhere.<\/p>\n\n
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>\n\n
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>\n\n
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>\n\n ", "format": "full_html", "processed": "\n
I've often heard and advocated for not \"putting logic\" in template files and having a separation of concerns.<\/p>\n\n
The templates should be as simple to read and change as possible, and any complicated logic should be moved elsewhere.<\/p>\n\n
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>\n\n
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>\n\n
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>\n\n ", "summary": null } ], "feeds_item": [ { "imported": "2025-05-11T09:00:30+00:00", "guid": null, "hash": "2bb59d5691c8de6da82be10d5c3b871c", "target_type": "feeds_feed", "target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76" } ] }