BorisMoore / jsviews

Interactive data-driven views, MVVM and MVP, built on top of JsRender templates
http://www.jsviews.com/#jsviews
MIT License
857 stars 130 forks source link

How to insert javascript code in templates, using jsviews? (allowCode) #290

Closed joebnb closed 9 years ago

joebnb commented 9 years ago

data: people = {"1":["0004","\u5218","XN"],"2":["0016","\u4e0","XN"],"3":[00198","\u6bdb","XN"]} template: {{for people}} var i=0 i++ {{:i}} {{/for}} but it doesn't work,my English is poor...

joebnb commented 9 years ago

i mean how to use template to make a count fill in elements

BorisMoore commented 9 years ago

You can use #index, or #getIndex():

{{for people}}
    {{:#index}}
{{/for}}

See the sample using {{:#index + 1}} on http://www.jsviews.com/#assigntag

joebnb commented 9 years ago

can i use javascript in script template? i think it can solve many problems. because i need make a determinant and count them,this is my smarty code: image ignore these Chinese - 0-

BorisMoore commented 9 years ago

You can set any template to allow code - (tmpl.allowCode=true) then use {{* //any code }} within the template. But it is better to encapsulate your code in helper functions, and call it in that way:

http://www.jsviews.com/#helpers

For allowCode, see for example:

See also

BorisMoore commented 9 years ago

@joebnb: Did allowCode work for you?

joebnb commented 9 years ago

yes,as you said its works