mustache

Mustache templates: How to output a block only once for non-empty lists

耗尽温柔 提交于 2019-12-18 05:43:04
问题 If my list is empty, I want to output this: <div id="some-id"> </div> If my list is non-empty, I want to output this: <div id="some-id"> <ul> <li>Item 1</li> <li>Item 2</li> <li>etc</li> </ul> </div> Note that I output the <ul> and </ul> tags at most once , and only if the list is non-empty. The following code is close to how I would do this in PHP, but is obviously wrong: <div id="some-id"> {{#items}} <ul> {{/items}} {{#items}} <li>{{name}}</li> {{/items}} {{#items}} </ul> {{/items}} </div>

Using Moustache as a templating language in Symfony 2

霸气de小男生 提交于 2019-12-17 23:08:26
问题 I'm starting to use symfony 2 but I'd like to use moustache as the templating language instead of Twig or PHP. I wan't to use moustache because it's totally logicless and because I can also use it in javascript if I decide to handle the rendering of the template clientside. How to do that? 回答1: Some extra info extending @m2mdas answer. If you are not yet familiar with Symfony templating systems and bundle configuration take a look at these before you start coding: How to expose a Semantic

Mustache.js - display key instead of value

…衆ロ難τιáo~ 提交于 2019-12-17 20:47:49
问题 I am using this data here: http://pastie.org/3231052 - How can I display the key instead of the value using Mustache or Handlebars? [{"interval":"2012-01-21", "advertiser":"Advertisers 1", "offer":"Life Insurance", "cost_type":"CPA", "revenue_type":"CPA", ... etc ... }] 回答1: If you want to display key-value pairs, you can write a helper in Handlebars. Handlebars.registerHelper('eachkeys', function(context, options) { var fn = options.fn, inverse = options.inverse; var ret = ""; var empty =

PHP Mustache 2.1 partial loading NOT based on the filename

删除回忆录丶 提交于 2019-12-17 19:04:05
问题 Is there a way to load partials based on an array of filename values? Currently if I write this {{> sidebar}} it will look for views/sidebar.mustache . (based on the template loader class where I can specify where to look for the templates) Ideally I want that {{> sidebar}} would be a variable name and not a file name. What I want to achieve is to look for the sidebar partial not based on the filename, if I pass to the loader: $partials = array( 'sidebar' => 'folder1/somefile' ); which would

How to change the template engine in Pyramid?

99封情书 提交于 2019-12-14 03:42:45
问题 In particular I want to use pystache but any guide for another template engine should be good enough to set it up. If I understood correctly, I have to register the renderer factory in the __init__.py of my pyramid application. config = Configurator(settings=settings) config.add_renderer(None, 'pystache_renderer_factory') Now I need to create the renderer factory and don't know how. Even though I found the documentation about how to add a template engine, I didn't manage to set it up. 回答1:

Mustache templating with an array of objects

ⅰ亾dé卋堺 提交于 2019-12-13 11:36:28
问题 I am trying to template the following array of objects: var arr = [{name:"Ryan Pays", url:"http://www.ryanpays.com"}, {name:"foo", url:"http://www.google.com"}]; I convert that array to an object like so: arr = $.extend({}, arr); Which gives me the following object: { 0:{name:"Ryan Pays", url:"http://www.ryanpays.com"}, 1:{name:"foo", url:"http://www.google.com"} } Using Mustache i want to enumerate over that object with the following template: var template = "<h4>Your friends' choices</h4>"

Mustache Java: Iterating over an anonymous/keyless/top-level array

淺唱寂寞╮ 提交于 2019-12-13 07:58:28
问题 Question: How to iterate over an array read from json input, that does not have a variable name/key. I did not want to restructure the json file as I'd have to edit the service which generates this json and also other services rely on this file and would have also been affected. A solution for javascript has already been posted using "." as array name in the Mustache template: Can mustache iterate a top-level array? and here Iterate over keyless array with mustache? I had the same question

Mustache doesn't handle events properly

馋奶兔 提交于 2019-12-13 06:31:15
问题 I want to use mustache to insert some templates in my html file. The template, jquery code and html code are in three separate files. This is mandatory cause I want my project to get as much organized as possible. When I write my 'nav' directly into html file the click event works fine, but if try to insert it with mustache it stops working. Any idea why? Thank you. test1.php file <html> <head> <title>World News</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs

Mustache_Autoloader missing with Composer

Deadly 提交于 2019-12-13 05:17:00
问题 I dowload the last versión of Mustache (2.7) with Composer, "require": { "mustache/mustache" : "2.7.*", // etc... } but when I try: use Mustache\Mustache_Autoloader; abstract class BaseController { public function __construct() { Mustache_Autoloader::register(); /... } /... } the error.log said: PHP Fatal error: Class 'Mustache\\Mustache_Autoloader' not found in Although, Mustache_Autoloader hasn't namespaces. Composer has: composer/autoload_namespaces.php : return array( 'Mustache' => array(

Populating a hidden form field

你。 提交于 2019-12-13 03:38:27
问题 The GET operation on my application returns the data in an array: Value":[{"Id":"6b7","Notes":"testing","CreatedBy":"User1"},{"Id":"6b7","Notes":"Testing 1","CreatedBy":"User2"}] I use the above to populate a template: <div class="create-note"> <form> **<input type="hidden" id="Id" name="Id" value="{{this.Value.Id}}" />** <textarea id="Notes" name="Notes"></textarea> <button for="" type="submit" class="btn btn-primary ">Add Note</button> <button type="reset" class="btn">Cancel</button> </form