I wonder if the Regex is really required. For better readability (maintainability) and maybe better performance:

function template(value) {
     return '<li><strong>My value:</strong>' + value + '</li>';
}
var myElement = $(template('This is a test')).appendTo('#body');


-- Klaus
I don't think it's more readable or maintainable but it's definitely faster (even so that shouldn't matter for what I think this will be used for). I didn't think of this when I was coding my solution tomorrow, but what I don't like about using callback functions is that they seem to be more verbose to me.

In the plugin skeleton I've done <http://demos.thinkingphp.org/jselect/> I can define an options hash like this:
--------------------------------------------------------------------
var options =
{
   container: '<ul>%s</ul>'
   , wrapper: '<li class="%s">%s</li>'
   , properties: ['className', 'text', 'value']
}
--------------------------------------------------------------------

Now of course this could be done via callback functions as well:
--------------------------------------------------------------------
var options =
{
   container: function(){return '<ul>'+arguments[0]+'</ul>'}
, wrapper: function(){return '<li class="'+arguments[0]+'">'+arguments[1]+'</li>'}
   , properties: ['className', 'text', 'value']
}
--------------------------------------------------------------------

But personally I prefer the placeholder method. One drawback however is that the JS regex engine doesn't support lookaround <http://www.regular-expressions.info/lookaround.html>(negative lookbehind is what I'd need) in order to allow for a '%s' placeholder to be escaped at the beginning of a String. But since I wrote this for data that needs to be wrapped the negative character class workaround should do the trick just as well.

Do you see any other drawbacks to my regex/placeholder approach?

-- Felix
--------------------------
http://www.thinkingphp.org
http://www.fg-webdesign.de


Klaus Hartl wrote:
Felix Geisendörfer schrieb:
Hey folks,

I've been reading about this plugin this morning and got inspired to play around with the basic functionality a <select> based (unobtrusive) plugin like this would need to have. I figured that in order to allow a maximum of flexibility from the design point, this plugin would need to be capable to transform any given <select> element (and it's children) to almost any other given Html markup. Not too long ago I've written a tabs plugin for a client (no OS right now : /) and needed some flexible markup transformation for styling as well. The solution I came up with was to provide html templates with placeholders in them like you often do with strings in PHP that are going to be populated using sprinft. Now JS doesn't have this functionality but I was able to bypass this using a little regex. The basic concept looks like this:

------------------------------------------------------------------------------------------
var myTemplate = '<li><strong>My value:</strong> %s</li>'
var myValue = 'This is a test';

var myElement = $(myTemplate.replace(/([^\\])%s/, "\$1"+myValue)).appendTo('#body');
------------------------------------------------------------------------------------------

This would replace the %s placeholder in the myTemplate string with the contents of myValue and then turn it into a DOM element using the mighty power of jQuery in order to attach it to an element with the id body later on. I think this is a technique that many plugins that need to transform html could benefit from. Now I don't have time to write an entire select plugin right now, but I've completed the basic transformation algorithm such a plugin would need. You can see a demo of it right here:

http://demos.thinkingphp.org/jselect/

I'd love to hear some feedback from the JS gurus in here what they think about this and if somebody feels like turning this into a complete plugin - feel free to do so ; ).

-- Felix Geisendörfer aka the_undefined
--------------------------
http://www.thinkingphp.org
http://www.fg-webdesign.de


I wonder if the Regex is really required. For better readability (maintainability) and maybe better performance:

function template(value) {
     return '<li><strong>My value:</strong>' + value + '</li>';
}
var myElement = $(template('This is a test')).appendTo('#body');


-- Klaus




_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to