{"action":"created","issue":{"url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/1749","labels_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/1749/labels{/name}","comments_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/1749/comments","events_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/1749/events","html_url":"https://github.com/jhipster/generator-jhipster/issues/1749","id":95846566,"number":1749,"title":"Modularize yeoman templates for easier maintenance and evolution","user":{"login":"gmarziou","id":361007,"avatar_url":"https://avatars.githubusercontent.com/u/361007?v=3","gravatar_id":"","url":"https://api.github.com/users/gmarziou","html_url":"https://github.com/gmarziou","followers_url":"https://api.github.com/users/gmarziou/followers","following_url":"https://api.github.com/users/gmarziou/following{/other_user}","gists_url":"https://api.github.com/users/gmarziou/gists{/gist_id}","starred_url":"https://api.github.com/users/gmarziou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmarziou/subscriptions","organizations_url":"https://api.github.com/users/gmarziou/orgs","repos_url":"https://api.github.com/users/gmarziou/repos","events_url":"https://api.github.com/users/gmarziou/events{/privacy}","received_events_url":"https://api.github.com/users/gmarziou/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/jhipster/generator-jhipster/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-07-18T18:15:03Z","updated_at":"2015-08-14T22:18:41Z","closed_at":null,"body":"Hi contributors!\r\n\r\nThe entity sub generator has probably the most complex yeoman templates of JHipster because they have grown each time new features were added, lately enums and validation have added even more complexity to the point that I think it's quite hard to evolve them.\r\n\r\nSo, thinking that there must be a better way, I looked at the template engine used by yeoman: [EJS](http://ejs.co/) and found that we could benefit from using these features:\r\n\r\n-  `include `       Sub templates\r\n- `<%-`             Outputs the unescaped value into the template\r\n- `<%#`            Comment tag, no execution, no output\r\n- `-%>`             Trim-mode ('newline slurp') tag, trims following newline\r\n\r\nI have applied this approach to field validation:\r\n  - _Entity.java (annotations)\r\n  - _entity-dialog.html (angular directives on form input fields)\r\n\r\nThe main change is to extract complex logic from the template and to externalize it to a sub template included by the first one and located in same folder.\r\n\r\n- [_entity-dialog.html](https://github.com/gmarziou/generator-jhipster/blob/validators/entity/templates/src/main/webapp/app/_entity-dialog.html) includes [ng_validators.ejs](https://github.com/gmarziou/generator-jhipster/blob/validators/entity/templates/src/main/webapp/app/ng_validators.ejs)\r\n- [_Entity.java](https://github.com/gmarziou/generator-jhipster/blob/validators/entity/templates/src/main/java/package/domain/_Entity.java) includes [field_validators.ejs](https://github.com/gmarziou/generator-jhipster/blob/validators/entity/templates/src/main/java/package/domain/field_validators.ejs)\r\n\r\nI chose the ejs extension because it's the default one and it allows to have a very concise syntax:\r\n\r\n    <%- include field_validators -%>\r\n\r\nHopefully, you'll find that this more readable than current templates embedding this logic without any good indentation and mixing 2 languages.\r\n\r\nAnother benefit is that the ejs sub templates can easily be unit tested.\r\n\r\nEmbedded templates can still be used and they are good for 1 to 3 lines of JS, but for bigger JS fragments I think we should refactor the template using this approach.\r\n\r\nMy fork is [here](https://github.com/gmarziou/generator-jhipster/blob/validators) but there's nothing more than these 4 files I linked above.\r\n"},"comment":{"url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/comments/131253744","html_url":"https://github.com/jhipster/generator-jhipster/issues/1749#issuecomment-131253744","issue_url":"https://api.github.com/repos/jhipster/generator-jhipster/issues/1749","id":131253744,"user":{"login":"jdubois","id":316835,"avatar_url":"https://avatars.githubusercontent.com/u/316835?v=3","gravatar_id":"","url":"https://api.github.com/users/jdubois","html_url":"https://github.com/jdubois","followers_url":"https://api.github.com/users/jdubois/followers","following_url":"https://api.github.com/users/jdubois/following{/other_user}","gists_url":"https://api.github.com/users/jdubois/gists{/gist_id}","starred_url":"https://api.github.com/users/jdubois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdubois/subscriptions","organizations_url":"https://api.github.com/users/jdubois/orgs","repos_url":"https://api.github.com/users/jdubois/repos","events_url":"https://api.github.com/users/jdubois/events{/privacy}","received_events_url":"https://api.github.com/users/jdubois/received_events","type":"User","site_admin":false},"created_at":"2015-08-14T22:18:41Z","updated_at":"2015-08-14T22:18:41Z","body":"@gmarziou yes OK! I still need to get used to those, but it's really cool!"}}