Attached are four extensions to jquery.

One is a
$.text('Test text')  utility function, that simplifies cases like
 $('div.content').append($.text('Test test <[email protected]> here'));
Second the same thing with
 $.html('some <div>html</div> with &nbsp; everything')
Third the combination:
 $.toHtml('some < special')
which for example encodes the entities. Possible use case: "nl2br()":
 $.html($.toHtml(text).replace('\n','<br/>\n'));

I think there should be *some* way in jquery to accomplish these (without
completely replacing the div's content), and *I* haven't found them in the
API.
Their implementation is simple - but improvements are welcome.

Fourth, a simple (but powerful) template engine (less than 2K):
E.g. with
  <div id="#templates" style="display: none">
    <div class="tdata1">
      Hello <span class="tname"></span>.
      <div class="trep">
        <span class="val1"></span><span class="val2"></span>
      </div>
      <div class="sub2">
        <span class="twrong">This Text wants to be removed.</span>
        <div><div class="deep"><a>First</a><a href="#">Link</a></div></div>
      </div>
    </div>
  </div>
you can do:

$('#templates .tdata1').template({
  '.tname': 'World <[email protected]>',
  '.trep': [
    {'val1': 5, val2: ' beer'},
    'Throw the old inner stuff away',
    $('<span>NEW</span>')
  ],
  '.sub .twrong': null,
  '.sub div .deep': {
    'a[href]': function (e) {
      e.click(function () { alert('Yes!'); return false;});
    }
  }
}).insertAfter('...');

Or the simple examples:
$('<span>Test</span>').template('New text');   => long way to write
.text('New test')
$('<span>Test</span>').template($('<del>X</del>'));   => another way to
write .empty().append('<del>X</del>')
$('<span>Test</span>').template(['A','B']);   => short way to write ...

I'm not sure, whether a template engine should be part of core jquery. But
maybe the name is a bit misleading: think about  it as $.fn.contents
/*_on_steroids*/ (args);

Comments, improvements, ... are welcome.

- Tobias

--

You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en.


Attachment: jquery.thobi.js
Description: JavaScript source

Reply via email to