8000 Passing data to def function · Issue #137 · olado/doT · GitHub
[go: up one dir, main page]

Skip to content

Passing data to def function #137

Closed
Closed

Description

@samuelngs

I have problem accessing the data in the def function.
Just for an example, I have a data of

{"name":"Jake","age":31}

And here's a template file

{{##def.test = function(str) {
     return "My name is: " + str;
}
#}}
{{#def.test(name)}}

As I tested the result will print out "My name is:" instead of "My name is Jake".
Is there a way to passing data to def function?

something like this???:

{{#def.test("{{=it.name}}")}}

or

{{#def.test(it.name)}}

Activity

  1. olado commented on Jan 12, 2015

    @olado
    Owner

    Def functions are invoked right away.
    If you want to use def functions and pass values from your it data you can do:
    option 1 :
    {{##def.mytestparam: {{=it.name}} #}}
    {{#def.test(def.mytestparam)}}
    option 2:
    {{#def.test("{{=it.name}}")}}

    You can also use parameterized def instead of a function in this case like this:
    {{##def.test:myparam:
    My name is: {{=myparam}}
    #}}

    {{#def.test:it.name}}

  2. maxiruani commented on Jan 13, 2015

    @maxiruani

    I can't get it working.
    Option 1 has and unexpected behaviour: The string "{{=it.name}}" not evaluated is passed to test function.
    Option 2 gives me: "Unexpected token ILLEGAL".

    There is another option?

  3. added a commit that references this issue on Jan 13, 2015
  4. olado commented on Jan 13, 2015

    @olado
    Owner

    @maxiruani @ogrape I added an example here 195025f
    You can pull this and run node withdoT.js to see that all 3 options work.

  5. maxiruani commented on Jan 13, 2015

    @maxiruani

    I can see now where is my problem.
    First, it can't be defined inside another {{##def.block}}
    Then, I was trying to evaluate nested def functions like this.

    {{##def.mytestparam: {{=it.value}} #}}
    {{#def.functionOne(def.functionTwo(def.mytestparam))}}

    The string "{{=it.jobsCount}}" is passed to functionTwo instead of the evaluated string.

    This works

    {{#def.functionOne("\{\{=it.value\}\}")}}

    This doesn't

    {{#def.functionOne(def.functionTwo("\{\{=it.value\}\}"))}}

    Any ideas?

    Thanks a lot!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0