Opened 15 years ago

Closed 15 years ago

#2484 closed enhancement (wontfix)

[cla] [patch] new feature for Template variable substitution 'modifiers'.

Reported by: [email protected] Owned by: bill
Priority: high Milestone:
Component: Widgets Version: 0.4.1
Keywords: modifier template Cc:
Blocked By: Blocking:


I've put together a feature on templates for the variable substitution, which I call Modifiers. I got the idea from Trimpath's Expression Modifiers.

In dojo, you do variable substitution with ${} and it will be replaced with the value of the property. The problem is, if it doesn't exist, then you get the ${} string back, and not null or something else. Rather than fix this, I've added Modifiers, which allow one to change/alter the value as you see fit.

To use, works just like Trimpath's. ${|modifier}, where modifier is one of the defined ones(you can define your own). So far the ones that I've written are:

eat: always returns an empty string. escape: escapes our common HTML values. captialize: capitalizes the value. lower: lowrcases the value. clean: if it's null it returns an empty string, and also escapes html elements.

so: in your template if you were to say ${|clean}, and is null you WILL get back an empty string, regardless. This is nice when exposing data from an SQL table, where null values are definitely possible.

to use in your template this is the syntax: ${|mod:arg:arg|mod|mod:arg} where arg is string arguments to that mod. where mod is string modifier name (currently eat/escape/capitalize/clean/lower) where is the normal property you want substituted. It runs from outside order in, so for instance: ${|lower|clean:POOH} would call clean(value,'POOH') and then call lower(value) so if property was empty, then it would return 'pooh' (lowercased 'default' value)

Attachments (1)

modifier.diff (5.2 KB) - added by [email protected] 15 years ago.
svn diff against 0.4.1 file: domWidget.js

Download all attachments as: .zip

Change History (4)

Changed 15 years ago by [email protected]

Attachment: modifier.diff added

svn diff against 0.4.1 file: domWidget.js

comment:1 Changed 15 years ago by Adam Peller

this may be superceded by #2350, if we were to use dojo.string.substitute in dijit's new template parser and reuse the logic, we could use the ":" modifiers, they'd just have to reference an existing function by name. With the right scoping, you might even be able to define some functions locally as proposed here, but I'd think we want to keep things simple.

comment:2 Changed 15 years ago by [email protected]

I agree. This was really my first patch into dojo, before I understood much of anything with how dojo works, or where dojo was going(not that I know much more now). I agree the functions should not be defined locally within the template system, but there should be a ready easy way for a user to define them without having to hack into dojo code(something my patch doesn't do either). I'm glad you like the modifier idea. I agree keeping things simple is good. I'm not sure I totally understand what you are proposing. I'd be willing to code it, but I'd have to completely understand it first!

comment:3 Changed 15 years ago by Adam Peller

Resolution: wontfix
Status: newclosed

Cathy, the patch is in place now. Where before you might have had ${} it should now be ${property} and you can add an optional : with a function name like ${property:function} That function can be defined on your widget for special cases where you need it, or I think you ought to be able to reference global functions or use something like dojo.regexp.escapeBlahBlah

Let me know how this works for you. There is some risk that this regexp replacement and the whole templatedwidget thing may be deemed to expensive for Dijit.

Note: See TracTickets for help on using tickets.