posthtml-extend
posthtml-extend copied to clipboard
Use expressions in attribute values
@Scrum what do you think about adding support for posthtml-expressions inside attribute values?
So we could then do:
<extends src="{{ condition ? 'primary' : 'secondary' }}.html">
... or:
<block name="{{ condition ? 'foo' : 'bar' }}">
I know you can just use posthtml-expressions in the plugins: [] option, but this can result in multiple expression evaluations if your build chain uses posthtml-expressions later on (example).
@cossssmin I have two opinions on this:
- I don't really like the logic in templates - it makes the template unreadable
- I don't really like to search for logic from templates - it takes my time and switches between files
I would prefer to move the logic into the method. the logic can be more complex and it will not be readable in templates
locals: {
isProduction: (env) => env === 'production' ? 'active' : 'hidden'
}
<p class="{{ isProduction(env) }}">in production!</p>
I would prefer to move the logic into the method
Of course, my examples were very basic, users would be free to choose how they handle it.
I came to think of this since there's no easy way to programatically extend a layout file - right now you have to write the <extend> tag twice, which means code duplication that could easily be avoided if we could evaluate simple expressions in the attribute values.
Should I get started on a PR to show how I imagine this working?
Should I get started on a PR to show how I imagine this working?
I think this will be a good start.