posthtml-extend icon indicating copy to clipboard operation
posthtml-extend copied to clipboard

Use expressions in attribute values

Open cossssmin opened this issue 4 years ago • 3 comments

@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 avatar Mar 11 '21 13:03 cossssmin

@cossssmin I have two opinions on this:

  1. I don't really like the logic in templates - it makes the template unreadable
  2. 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>

Scrum avatar Mar 12 '21 06:03 Scrum

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?

cossssmin avatar Mar 12 '21 09:03 cossssmin

Should I get started on a PR to show how I imagine this working?

I think this will be a good start.

Scrum avatar Mar 12 '21 09:03 Scrum