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

Template: self-removing blocks #17

Open
koczkatamas opened this issue Feb 3, 2018 · 0 comments
Open

Template: self-removing blocks #17

koczkatamas opened this issue Feb 3, 2018 · 0 comments

Comments

@koczkatamas
Copy link
Collaborator

There are multiple cases where we only have to add eg. a method if it has content. This sometimes needs to be predicted beforehand, which requires some ugly hacks right now.

Most of the cases happens where we use an {{if}} inside a {{for}} and we don't know many elements will be printed out actually. But even if we use two of fors or ifs it complicates the "predictor" expression and looks ugly.

We also need an {{else}} option where we don't just remove the block but replace it with some other content (eg. with pass in Python)

Some places where we need this logic:

  • every class.needsConstructor property usage
  • Golang: init method
  • Python: fill method body or use pass
  • TODO: others...

How does it work?

If every child template gives empty result then the block is not showed (even if there is static content inside the block, eg. prefix / postfix text). If {{else}} is specified then instead of empty result, the contents of {{else}} is shown.

Proposed syntax

Python use pass if the method has no statements:

{{block}}
  {{genBody(method.body)}}
{{else}}
  pass
{{/block}}

Go / other langs: remove constructor if not used:

{{block}}
  func init() {
    {{for class in classes|sep=\n\n}}
      {{if class.reflect}}
        ...
      {{/if}}
    {{/for}}
{{/block}}

To-be-decided

What happens if we want to use that information whether a block was generated or not? Eg. only call an initialization method if it was generated beforehand? I don't know such case now, but we may run into one in the future...

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

No branches or pull requests

1 participant