Parcourir la source

Changed helper syntax to {{tag|helper[|helper2|...]}}

helpers
Jan Lehnardt il y a 14 ans
Parent
révision
55cce253d5
6 fichiers modifiés avec 33 ajouts et 19 suppressions
  1. +1
    -1
      examples/helpers.html
  2. +2
    -2
      examples/helpers_error.html
  3. +2
    -0
      examples/helpers_pipe.html
  4. +9
    -0
      examples/helpers_pipe.js
  5. +2
    -0
      examples/helpers_pipe.txt
  6. +17
    -16
      mustache.js

+ 1
- 1
examples/helpers.html Voir le fichier

@@ -1,2 +1,2 @@
Hi {{name}}.
Hi {{ucase name}}.
Hi {{name|ucase}}.

+ 2
- 2
examples/helpers_error.html Voir le fichier

@@ -1,3 +1,3 @@
Hi {{name}}.
Hi {{ucase name}}.
Hi {{lcase name}}.
Hi {{name|ucase}}.
Hi {{name|lcase}}.

+ 2
- 0
examples/helpers_pipe.html Voir le fichier

@@ -0,0 +1,2 @@
Hi {{name}}.
Hi {{name|ucase|bangify}}.

+ 9
- 0
examples/helpers_pipe.js Voir le fichier

@@ -0,0 +1,9 @@
var helpers_pipe_helpers = {
ucase: function(s) {return s.toUpperCase(); },
bangify: function(s) {return "!" + s + "!"}
};

var helpers_pipe = {
name: "Chris"
};


+ 2
- 0
examples/helpers_pipe.txt Voir le fichier

@@ -0,0 +1,2 @@
Hi Chris.
Hi !CHRIS!.

+ 17
- 16
mustache.js Voir le fichier

@@ -270,20 +270,13 @@ var Mustache = function() {
}

var value;
var helper;

// check for helper e.g. name = "helperfun name"
var helper_name = name.split(" ");
if(helper_name.length == 2) { // we have a helper
helper = helper_name[0];
name = helper_name[1];
if(!this.helper_functions[helper]
|| typeof this.helper_functions[helper] != "function") {
throw {message:
"Helper '" + helper + "' is not a registered helper"};
} else {
helper = this.helper_functions[helper];
}
var helpers;

// check for helper e.g. name = "name|helperfun"
var helper_name = name.split("|");
if(helper_name.length > 1) { // we have at least one helper
name = helper_name[0];
helpers = helper_name.slice(1);
}

// check for dot notation eg. foo.bar
@@ -305,8 +298,16 @@ var Mustache = function() {
value = value.apply(context);
}

if(helper) {
value = helper.apply(context, [value]);
if(helpers) {
for(var idx in helpers) {
var helper = helpers[idx];
if(!this.helper_functions[helper]
|| typeof this.helper_functions[helper] != "function") {
throw {message:
"Helper '" + helper + "' is not a registered helper"};
}
value = this.helper_functions[helper].apply(context, [value]);
}
}

if(value !== undefined) {


Chargement…
Annuler
Enregistrer