views:

18

answers:

2

I develop a jQuery plugin which produces HTML output, currently something like

<ul>
  <li><img></img></li>
  ...
</ul>

But actually, I would like to let the user of my plugin chose the HTML markup which gets generated, so for instance he passes a template string from which my plugin produces the output. But I don't know how.

Are there any best practices / "patterns" on how to achieve such a functionality?

+1  A: 

The easiest way is to allow for an optional rendering function param and pass it the element(s). This way you have maximum control with minimal effort. Example:

$('#id').my_action(param1, function (e1, e2) {
    $('#target1').append(e1);
    $('#target2').append(e2);
});
Emil Ivanov
Yeah, that is a great idea... Anyway I'll leave the question open for a bit, maybe there are some more ideas...
Mark Hamilton
A: 

Take a look at the Fluid Renderer. I believe they're doing something very similar to what you describe.

Drew Wills