Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use expressions in attribute values #35

Open
cossssmin opened this issue Mar 11, 2021 · 3 comments
Open

Use expressions in attribute values #35

cossssmin opened this issue Mar 11, 2021 · 3 comments

Comments

@cossssmin
Copy link
Member

@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).

@Scrum
Copy link
Member

Scrum commented Mar 12, 2021

@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>

@cossssmin
Copy link
Member Author

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?

@Scrum
Copy link
Member

Scrum commented Mar 12, 2021

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

I think this will be a good start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants