#1009 ✓invalid
Dan Dean

Enable enumerable data as argument in Template#evaluate and String#interpolate

Reported by Dan Dean | March 6th, 2010 @ 08:48 AM

I've patched Template#evaluate so that you can now provide array data as the argument, not simply a single object hash. If the team is interested in it, I'd love for it to be added to a future version of Prototype.

It's a simple patch: it checks if the argument is an Array. If it is, it recursively calls evaluate on each item in the array and returns the concatenated result.

Since the path is within Template#evaluate, String#interpolate inherits the same functionality.

http://github.com/dandean/prototype/commit/f97a93f83e47fb8c6993c65d...

I've added docs and examples to the Template#evaluate docs, but here's the gist of it:

Create a template for a single list item:

var itemTemplate = new Template('<li data-type="#{type}">#{name}</li>');

Evaluate a single piece of data against the template:

var singleItem = itemTemplate.evaluate(
  { type: "fruit", name: "Apple" }
);
//-> <li data-type="fruit">Apple</li>

Evaluate a collection of data against the template:

var manyItems = itemTemplate.evaluate([
  { type: "fruit", name: "Apple" },
  { type: "fruit", name: "Orange" },
  { type: "appliance", name: "Wii" },
  { type: "clothing", name: "Pants" },
  { type: "person", name: "Richard" }
]);
//-> <li data-type="fruit">Apple</li><li data-type="fruit">Orange</li>...

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel March 6th, 2010 @ 12:36 PM

    • State changed from “enhancement” to “invalid”

    Is is really worth it given the below?

    var template = new Template('<li data-type="#{type}">#{name}</li>');
    var manyItems = [
      { type: "fruit", name: "Apple" },
      { type: "fruit", name: "Orange" },
      { type: "appliance", name: "Wii" },
      { type: "clothing", name: "Pants" },
      { type: "person", name: "Richard" }
    ].map(template.evaluate, template).join('');
    

    Won't we open a can of worm doing this?

    Foreseeable upcoming requests:

    1. A second argument to Template#evaluate for a separator.
    2. Template#evaluate to handle object with a toTemplateArray method differently.
    3. Template#evaluate to handle nested arrays.
    4. etc…

    I think that the Template class was misnamed from the very start. It's purpose never was to be used as a templating engine (I'm really digging mustache for that purpose). It's meant to fill in a perceived gap in the language: there's no string interpolation.

    I also completely forgot the following, documented behaviour:

    '#{0}#{1}'.interpolate(['foo', 'bar']);
    // => 'foobar'
    

    Your suggestion would completely break backwards-compatibility and is therefore obviously out of the question. Closing this as invalid.

  • Dan Dean

    Dan Dean March 6th, 2010 @ 04:41 PM

    Tobie, thanks for taking the time to review this patch.

    I as well realized the significance of #{0}#{1} while writing this patch up. -- maybe that's something that can be added to the docs.

    I'm going to go put my can of worms back on the shelf now.

  • Tobie Langel

    Tobie Langel March 6th, 2010 @ 06:43 PM

    I'm going to go put my can of worms back on the shelf now.

    Haha!

    And yes! +1 for documenting this.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Pages