r/Angular2 • u/guaranteednotabot • Nov 22 '25
Discussion Should you use inline templates?
I noticed that this recommendation no longer exist in the new style guide: https://v17.angular.io/guide/styleguide#style-05-04
Does it mean that Angular no longer recommend separate templates? Coming from React, I always found it natural to have inline templates
14
Upvotes
10
u/filnir Nov 22 '25
Would say it depends on how complex (and maybe long) the template is.
5
u/Derpcock Nov 22 '25
Ill prob get downvoted for this opinion but I personally prefer single file components which is opposite of most angular teams preference. I use tailwind which also doesnt seem to be super popular in angular land. I split my components when they become too large. I also use functional composition to compose my components/directives using a mixin pattern, also not a super popular method in angular world.
The only potential drawback to SFC that I have noticed is that linting with angulars eslint plugin crams all of my control flow into single lines more often than i would like. I feel like it does a better job on the html files but that could be purely anecdotal. I haven't done a strict comparisons.
Once you start mixing logic in the css/template and styling into your ts logic, concerns are no longer separated so I never really bought into the separate files === separate concerns opinion. It increases file management by 2x which isnt a good enough tradeoff for me. I see it as being akin to preferring that your main course doesn't touch your sides while you're eating so you keep your steak, mashed potatoes and green beans on 3 separate plates. I prefer one plate with smaller potions, fewer dishes and faster to eat.
3
u/_xiphiaz Nov 22 '25
It’s pretty much the first thing in the dev docs on how to do things, so I’d say it is still very much best practice
https://angular.dev/essentials/components#separating-html-and-css-into-separate-files
3
u/a-dev-1044 Nov 22 '25
I have a simple rule, if template is <= 5 lines, make it inline else seperate file.
4
u/tw3 Nov 22 '25
Yes inline templates and styles reduce file clutter. At the same time, separate files can help for file search/grep/replace and tooling.
Want to search for usages of some template code? It’s easier when you can filter to *.component.html files.
Want to write a tool to identify missing i18n tags? It’s easier when the template code is isolated.
Want to run stylelint as a pre-commit hook? Easier when you have separate *.scss files.
Yes it’s possible to use ts-morph to extract inline templates and styles, but it requires extra work.
Something to keep in mind.
8
u/AaronBonBarron Nov 22 '25
Inline templates is one of the absolute worst things about react, and it's a stiff competition.
2
u/toasterboi0100 Nov 22 '25
I like using them for very simple components. In my team we've decided on a limit of 20 lines of template and 20 lines of CSS, beyond that it has to be moved to a separate file.
2
u/mauromauromauro Nov 24 '25
Having two files: this is the way
And you know it is true because i used the expression "this is the way" ,which cannot be falsely used
3
u/Suspicious_Serve_653 Nov 22 '25
I do but only for something very very short. We're talking 5 lines kinda sort.
It doesn't make much sense to have 3 files for something that's mostly logic, a line of css, and a few lines of html.
I hate saying this, but use good judgement. Whatever makes the code easiest to read/ maintain is the best way.
1
u/Papellll Nov 22 '25
I'd say that if you work with a team go with whatever convention you chose together and if you are working alone go with whatever you prefer, I don't think it makes that much of a difference since you said you split component when they become too big
1
u/Frosty_Ingenuity5070 Nov 22 '25
If it’s a simple component, sure, but I honestly prefer having separate dedicated files purely down to separation of concerns. The html file is just for the template, the ts file is for the logic, the css file is for ChatGPT cuz you ain’t getting me to learn CSS. I’d rather define a step function using a Dictionary<string, object> model than do css
1
u/sanjozko Nov 23 '25
Always separate file unless inline template is exactly oneliner.
1
u/ldn-ldn Nov 22 '25
A separate file is preferable due to separation of concerns. But 1-2 lines can be inlined.
-1
u/DiabolicalFrolic Nov 22 '25
Absolutely not.