Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
870b73ce
Commit
870b73ce
authored
11 years ago
by
Thomas Tanghus
Browse files
Options
Downloads
Patches
Plain Diff
Add octemplate jQuery plugin
parent
248e097f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/js/octemplate.js
+96
-0
96 additions, 0 deletions
core/js/octemplate.js
with
96 additions
and
0 deletions
core/js/octemplate.js
0 → 100644
+
96
−
0
View file @
870b73ce
/**
* jQuery plugin for micro templates
*
* Strings are automatically escaped, but that can be disabled by setting escapeFunction to null.
*
* Usage examples:
*
* var str = 'Bake, uncovered, until the {greasystuff} is melted and the {pasta} is heated through, about {min} minutes.'
* $(str).octemplate({greasystuff: 'cheese', pasta: 'macaroni', min: 10});
*
* var htmlStr = '<p>Welcome back {user}</p>';
* $(htmlStr).octemplate({user: 'John Q. Public'}, {escapeFunction: null});
*
* For anything larger than one-liners, you can use a simple $.get() ajax request to get the template,
* or you can embed them it the page using the text/template type:
*
* <script id="contactListItemTemplate" type="text/template">
* <tr class="contact" data-id="{id}">
* <td class="name">
* <input type="checkbox" name="id" value="{id}" /><span class="nametext">{name}</span>
* </td>
* <td class="email">
* <a href="mailto:{email}">{email}</a>
* </td>
* <td class="phone">{phone}</td>
* </tr>
* </script>
*
* var $tmpl = $('#contactListItemTemplate');
* var contacts = // fetched in some ajax call
*
* $.each(contacts, function(idx, contact) {
* $contactList.append(
* $tmpl.octemplate({
* id: contact.getId(),
* name: contact.getDisplayName(),
* email: contact.getPreferredEmail(),
* phone: contact.getPreferredPhone(),
* });
* );
* });
*/
(
function
(
$
)
{
/**
* Object Template
* Inspired by micro templating done by e.g. underscore.js
*/
var
Template
=
{
init
:
function
(
vars
,
options
,
elem
)
{
// Mix in the passed in options with the default options
this
.
vars
=
vars
;
this
.
options
=
$
.
extend
({},
this
.
options
,
options
);
this
.
elem
=
elem
;
var
self
=
this
;
if
(
typeof
this
.
options
.
escapeFunction
===
'
function
'
)
{
$
.
each
(
this
.
vars
,
function
(
key
,
val
)
{
if
(
typeof
val
===
'
string
'
)
{
self
.
vars
[
key
]
=
self
.
options
.
escapeFunction
(
val
);
}
});
}
var
_html
=
this
.
_build
(
this
.
vars
);
return
$
(
_html
);
},
// From stackoverflow.com/questions/1408289/best-way-to-do-variable-interpolation-in-javascript
_build
:
function
(
o
){
var
data
=
this
.
elem
.
attr
(
'
type
'
)
===
'
text/template
'
?
this
.
elem
.
html
()
:
this
.
elem
.
get
(
0
).
outerHTML
;
try
{
return
data
.
replace
(
/{
([^
{}
]
*
)
}/g
,
function
(
a
,
b
)
{
var
r
=
o
[
b
];
return
typeof
r
===
'
string
'
||
typeof
r
===
'
number
'
?
r
:
a
;
}
);
}
catch
(
e
)
{
console
.
error
(
e
,
'
data:
'
,
data
)
}
},
options
:
{
escapeFunction
:
function
(
str
)
{
return
$
(
'
<i></i>
'
).
text
(
str
).
html
();}
}
};
$
.
fn
.
octemplate
=
function
(
vars
,
options
)
{
var
vars
=
vars
?
vars
:
{};
if
(
this
.
length
)
{
var
_template
=
Object
.
create
(
Template
);
return
_template
.
init
(
vars
,
options
,
this
);
}
};
})(
jQuery
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment