mustache

How does one use a literal {{ in a Mustache template?

可紊 提交于 2019-11-26 22:00:32
问题 How does one use a literal "{{" in a Mustache template? On a side note, if I'm using custom tags, like <% and %> , is there a way to write "<%"? Theoretically, I could use different tags, but I have too much code written using {{ and }} to change it all. 回答1: Just change the delimiters temporarily: {{=<% %>=}} {{Look at the curlies!}} <%={{ }}=%> 回答2: Assuming you are outputting HTML you could use an HTML entity to avoid it (mustache doesn't have any way to escape the opening tag built in).

Index of an array element in Mustache.js

混江龙づ霸主 提交于 2019-11-26 20:56:43
问题 This is what I'd like to do in Mustache.js but not seeing how with the documentation. var view = {items:['Mercury','Venus','Earth','Mars']}; var template = "<ul> {{#items}}<li>{{i}} - {{.}}</li>{{/items}} </ul>"; var html = Mustache.to_html(template,view); Desired output: <ul> <li>0 - Mercury</li> <li>1 - Venus</li> <li>2 - Earth</li> <li>3 - Mars</li> </ul> 回答1: An alternative solution, without fooling around with Mustache.js Instead of fooling around with mustache you might as well use a

calling function with arguments in mustache javascript

人走茶凉 提交于 2019-11-26 20:51:16
问题 Is it possible to call a function with arguments with Mustache.js {{somefunction(somevalue)}} thank you 回答1: Check out the section on Lambdas at http://mustache.github.com/mustache.5.html Mustache template block: {{#someFunction}}someValue{{/someFunction}} Function block: someFunction : function () { return function(val, render) { return "I passed in this value: " + render(val); }; } Output: I passed in this value: someValue 回答2: If you want the script contents to be executed after the markup

Can mustache iterate a top-level array?

亡梦爱人 提交于 2019-11-26 18:53:14
问题 My object looks like this: ['foo','bar','baz'] And I want to use a mustache template to produce from it something like this: "<ul><li>foo</li><li>bar</li><li>baz</li></ul>" But how? Do I really have to munge it into something like this first? {list:['foo','bar','baz']} 回答1: You can do it like this... Mustache.render('<ul>{{#.}}<li>{{.}}</li>{{/.}}</ul>', ['foo','bar','baz']); It also works for things like this... var obj = [{name: 'foo'}, {name: 'bar'}]; var tmp = '<ul>{{#.}}<li>{{name}}</li>

How do I accomplish an if/else in mustache.js?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 18:20:49
It seems rather odd that I can't figure how to do this in mustache. Is it supported? This is my sad attempt at trying: {{#author}} {{#avatar}} <img src="{{avatar}}"/> {{/avatar}} {{#!avatar}} <img src="/images/default_avatar.png" height="75" width="75" /> {{/avatar}} {{/author}} This obviously isn't right, but the documentation doesn't mention anything like this. The word "else" isn't even mentioned :( Also, why is mustache designed this way? Is this sort of thing considered bad? Is it trying to force me to set the default value in the model itself? What about the cases where that isn't

Handlebars Template rendering template as text

ε祈祈猫儿з 提交于 2019-11-26 17:41:54
问题 I created a helper in Handlebars to help with logic, but my template parses the returned html as text rather than html. I have a quiz results page that is rendered after the quiz is completed: <script id="quiz-result" type="text/x-handlebars-template"> {{#each rounds}} {{round_end_result}} {{/each}} <div class="clear"></div> </script> For each of the rounds, I use a helper to determine which template to render a round's result: Handlebars.registerHelper("round_end_result", function() { if

What are the differences between Mustache.js and Handlebars.js?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 14:58:31
问题 Major differences I've seen are: Handlebars adds #if , #unless , #with , and #each Handlebars adds helpers Handlebars templates are compiled (Mustache can be too) Handlebars supports paths Allows use of {{this}} in blocks (which outputs the current item's string value) Handlebars.SafeString() (and maybe some other methods) Handlebars is 2 to 7 times faster Mustache supports inverted sections (i.e. if !x ... ) (Please correct me if I'm wrong with the above.) Are there any other major

What&#39;s the advantage of Logic-less template (such as mustache)?

半腔热情 提交于 2019-11-26 12:49:45
问题 Recently, I ran into mustache which is claimed to be Logic-less template . However, there is no explaining why it is designed in Logic-less way. In another word, what\'s the advantage of Logic-less template? 回答1: In other words, it prevents you from shooting yourself in the foot. In the old JSP days, it was very common to have JSP files sprinkled with Java code, which made refactoring much harder, since you had your code scattered. If you prevent logic in templates by design (like mustache

linux mustache bash 实现mo 做为docker容器运行动态配置工具数组的处理

Deadly 提交于 2019-11-26 10:15:49
前面有说过关于使用mo 工具的简单配置使用,但是实际中我们可能存在比较复杂的数据处理,比如数组,mo 可以进行数组的处理,但是在测试的过程中,一直失败,查看了官方的demo以及帮助命令发现可以通过参数 -s 引入变量文件,可以方便的注入到模版引擎中,所以通过重定向环境变量到文件,然后注入,之后删除即可 环境准备 docker-compose.yaml 此处引入了三个环境变量content DATA DATA2 version: "3" services: app: build: ./ ports: - "8080:80" environment: - "content=dalong demo web page" - "DATA=(apple orange pear demo app rong)" - "DATA2=(rongfeng orange pear demo app rong)" Dockerfile FROM nginx:alpine RUN apk add --no-cache wget bash && \ # install mustache as script wget -O /usr/bin/mo https://git.io/get-mo && \ chmod a+x /usr/bin/mo && \ apk del wget && \ mkdir -p

How do I accomplish an if/else in mustache.js?

老子叫甜甜 提交于 2019-11-26 05:18:40
问题 It seems rather odd that I can\'t figure how to do this in mustache. Is it supported? This is my sad attempt at trying: {{#author}} {{#avatar}} <img src=\"{{avatar}}\"/> {{/avatar}} {{#!avatar}} <img src=\"/images/default_avatar.png\" height=\"75\" width=\"75\" /> {{/avatar}} {{/author}} This obviously isn\'t right, but the documentation doesn\'t mention anything like this. The word \"else\" isn\'t even mentioned :( Also, why is mustache designed this way? Is this sort of thing considered bad