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

Can't call functions defined in template from scuri:template:* #135

Open
danjor opened this issue Nov 23, 2021 · 2 comments
Open

Can't call functions defined in template from scuri:template:* #135

danjor opened this issue Nov 23, 2021 · 2 comments

Comments

@danjor
Copy link

danjor commented Nov 23, 2021

Hello,

I need to use string helper functions to generate strings and have a custom template the way I want.
This said, I then have a lot of dupplicate code in my custom template.
So, I tried to define custom functions in my ejs template. For instance :

<%
myCustomFunction = function(p) {
   return camelize(p.type)
}
%>

I can call them from everywhere but not from inside all scuri:template:*
I'm getting the following error :

ReferenceError: myCustomFunction is not defined

@danjor danjor changed the title Call functions defined in template from scuri:template:* Can't call functions defined in template from scuri:template:* Nov 23, 2021
@gparlakov
Copy link
Owner

@danjor thanks for your support and active participation!

This sounds like an annoying problem.

Sorry,

Handling it on SCuri's side would be much more difficult (update templates are executed in isolation from each other and the main template).

@danjor
Copy link
Author

danjor commented Nov 24, 2021

Hello,
Thanks for your reply.

I'm giving you another example with more details to try to be more precise :

I have the following in my custom template

let <%= camelize(className) %>: <%= className %>;
<%params.sort((a,b) => a.type > b.type ? 1 : -1).forEach(p => {%>let <%= camelize(p.type.replace(new RegExp('\\[\\]', 'g'), 'List')) %>Spy: Spy<<%= p.type %>>;<% }) %>   // this is for create
// scuri:lets    // this is for update

// Below the template definition for lets update
/** scuri:template:lets:<%params.sort((a,b) => a.type > b.type ? 1 : -1).forEach(p => {%>let <%= camelize(p.type.replace(new RegExp('\\[\\]', 'g'), 'List')) %>Spy: Spy<<%= p.type %>>;
<% }) %>*/

and I also have custom helper function defined in the custom template :

<%
sortAsc = function(params) {
   return params.sort((a,b) => a.type > b.type ? 1 : -1)
}

getLetVarname = (pType) {
   return camelize(replaceArrayWithList(pType))
}

replaceArrayWithList = function(pType) {
  return pType.replace(new RegExp('\\[\\]', 'g'), 'List')
}
%>

So I now want to use these functions in both directly from the custom template for the create (which already work), but also from all the scuri:template:* (does not work) :

let <%= camelize(className) %>: <%= className %>;
<%sortAsc(params).forEach(p => {%>let <%= getLetVarname(p.type) %>Spy: Spy<<%= p.type %>>;<% }) %>   // this is for create
// scuri:lets    // this is for update

// Below the template definition for lets update
/** scuri:template:lets:<%sortAsc(params).forEach(p => {%>let <%= getLetVarname(p.type) %>Spy: Spy<<%= p.type %>>;
<% }) %>*/

One way to acheive this could be to give the schematic the ability for user to provide not only the custom template, but also another custom template dedicated to helpers functions. This one could be provided in both from a new argument of the schematic or in the config file.

For this to work, you would need to provide all ejs with the template helper that contains custom functions, if that is possible ?
This way, all these function would be accessible from anywhere.

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