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

Pasing argument to partial template (#def) ? #34

Closed
jeme opened this issue Jul 8, 2012 · 3 comments
Closed

Pasing argument to partial template (#def) ? #34

jeme opened this issue Jul 8, 2012 · 3 comments

Comments

@jeme
Copy link

jeme commented Jul 8, 2012

In the advanced examples you define a snippet like:

{{##def.snippet1:
    Some snippet that will be included {{#def.a}} later {{=it.f1}}
#}}

I was wondering if it was at all possible to substitute "it" for that snippet.
Given the following example:

{{##def.row:
{{=it.Title}}
#}}

<ul>
    {{~ it.Nodes :p}}
    <li>
        {{#def.row}}
        <ul>
            {{~ p.Nodes :s}}
                <li>
                {{#def.row}}
                </li>         
         {{~}}
         </ul>
     </li>
     {{~}}
  </ul>

Now the idea would be to print "p.Title" for the first level and then "s.Title" for the second level, but re-use the same template for both elements...

Also would it be possible to make the above recursive somehow?

@olado
Copy link
Owner

olado commented Jul 8, 2012

There is no parameter passing right now for defines. There are 2 other ways to get this kind of functionality though.

  1. You could create a variable like {{ var x; }} and assign it some value before including def, like
    {{ x=s; }} {{#def.row}} and in def row use x instead of it.
  2. Another way is to use function instead of def like this:
    {{ function row(x) { }}
    Some snippet that will be included {{#def.a}} later {{=x.f1}}
    {{ } }}

and then instead of {{#def.row}} call {{ row(s); }}

For recursion you can do this:

{{ function children(it) { }}

{{?it.Nodes && it.Nodes.length}}
<ul>
    {{~ it.Nodes :p}}
    <li>
        {{=p.title}}
        {{children(p);}}
    </li>
    {{~}}
</ul>
{{?}}

{{ } }}

{{ children(it); }}

@jeme jeme closed this as completed Jul 9, 2012
@jeme
Copy link
Author

jeme commented Jul 9, 2012

Hey THANKS!..

I ended up with a solution like you describe in "1." yesterday, where the reused template was also stored else where as it's own template though. But I think I like the idea of using a function better, so ill try to get that into place.

/Jeme

@olado
Copy link
Owner

olado commented Dec 19, 2012

Thanks Tomasz. I will push support for passing arguments to defines very soon.
For using functions, see #34 (comment)

On 2012-12-17, at 5:39 AM, tomasz stryjewski wrote:

well, it would be great to have a support for passing arguments to defines / functions

or is there any other option to use some kind of universal functions that would format the given variable, no matter the variable name ( quick use-case: number 'prettifier' )

otherwise all the templates become non-DRY really quick...


Reply to this email directly or view it on GitHub.

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