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

arrays/external template #35

Closed
ghost opened this issue Jul 17, 2012 · 3 comments
Closed

arrays/external template #35

ghost opened this issue Jul 17, 2012 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 17, 2012

Hello,

Im having some issues when using array iteration and external templates. I have the following code:

main_template:

{{#def.tpl_brother1 = def.load('templates/brother.tpl')}}
{{##def.tpl_brother2: <div>{{=brother.name}}</div>#}}

<div>
<div>{{=it.name}}</div>
<div>
    {{~it.brothers :brother}}
        {{#def.tpl_brother2 || 'not found'}}
    {{~}}
</div>
</div>

data for the main_template:

{
"name" : "Jake",
"brothers" : [ { "name" : "John" }, { "name" : "Brad" } ]  
}

when im using tpl_brother2 snippet works ok, i have access to "name" property of each brother. but when i use the tpl_brother1 snippet i get a error that i cant access "name" property...

any suggestions to solve this?

thanks
aveiros

@olado
Copy link
Owner

olado commented Jul 17, 2012

There is no difference in the way doT treats external or inline templates.
Could you post your external template too, or better yet, all of the code to reproduce what you see.

Btw, this may help you to find the problem: you can get the source of the compiled function doT generates with:
doT.template(text).toString()

On 2012-07-17, at 4:23 AM, aveiros wrote:

Hello,

Im having some issues when using array iteration and external templates. I have the following code:

main_template:
{{#def.tpl_brother1 = def.load('templates/brother.tpl')}}
{{##def.tpl_brother2:

{{=brother.name}}
#}}

{{=it.name}}
{{~it.brothers :brother}} {{#def.tpl_brother2 || 'not found'}} {{~}}

data for the main_template:
{ "brothers" : [ { "name" : "John" },
{ "name" : "Brad" }
],
"name" : "Jake"
}

when im using tpl_brother2 snippet works ok, i have access to "name" property of each brother. but when i use the tpl_brother1 snippet i get a error that i cant access "name" property...

any suggestions to solve this?

thanks
aveiros


Reply to this email directly or view it on GitHub:
#35

@ghost
Copy link
Author

ghost commented Jul 18, 2012

Hello again,

I guess what im looking for is the following code (snippet), for external files:

#1 file: advancedsnippet.txt

Here is a def block that will be used later.
This snippet can be referenced from external templates too:
{{##def.snippet1:
    Some snippet that will be included {{#def.a}} later {{=it.f1}}
#}}

Following i post my current code...

#2 file: main.tpl
In this file i define a tp1 so i can use later as #1.

{{##def.tpl1: <div>{{=brother.name}}</div>#}}

<div style="background: green">
    {{~it.brothers :brother}}
    {{#def.tpl1 || 'not found'}}
    {{~}}
</div>

<div style="background: yellow">
    {{~it.brothers :brother}}
    {{#def.tpl2 || (def.tpl2 = def.load('templates/secundary.tpl'))}}
    {{~}}
</div>

<div style="background: purple">
    {{~it.brothers :brother}}
    {{#def.tpl2 || (def.tpl2 = def.load('templates/secundary.tpl'))}}
    {{~}}
</div>

<div style="background: blue">
    {{~it.brothers :brother}}
    {{#def.tpl1 || 'not found'}}
    {{~}}
</div>

#3 file: secundary.tpl

<div>{{=brother.name}}</div>

#4 data to apply in main.tpl

{
"name" : "Jake",
"brothers" : [ { "name" : "John" }, { "name" : "Brad" } ]
}

The above example runs and i get the expected results.
The example demonstrates, its possible create def blocks for inline templates (line 1 of #2) and reference them later as many times we want. Can we do the same for external templates?

thank you
aveiros

@ghost
Copy link
Author

ghost commented Jul 18, 2012

managed to get it working :)

file: main.tpl

<!-- for later referecing -->
{{##def.tpl1: <div>{{=brother.name}}</div>#}}
{{##def.tpl2 = def.load('templates/secundary.tpl')#}}

<div style="background: green">
    {{~it.brothers :brother}}
    {{#def.tpl1 || 'not found'}}
    {{~}}
</div>

<div style="background: yellow">
    {{~it.brothers :brother}}
    {{#def.tpl2 || 'not found'}}
    {{~}}
</div>

<div style="background: purple">
    {{~it.brothers :brother}}
    {{#def.tpl2 || 'not found'}}
    {{~}}
</div>

<div style="background: blue">
    {{~it.brothers :brother}}
    {{#def.tpl1 || 'not found'}}
    {{~}}
</div>

aveiros

@ghost ghost closed this as completed Jul 27, 2012
This issue was closed.
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

1 participant