Giter VIP home page Giter VIP logo

fragment.js's Introduction

fragment.js

A minimalistic tool for easily loading html fragments

Examples

Example

Fragment.js allows you to load html fragments into any element, by just adding a data-fragment attribute.

<div data-fragment="fragment.html"></div>

Templating example

You can also use it for loading templates with the data-fragment-json attribute. Mustache, Handlebars and Underscore are supported by default and will automatically be used if they're available in the global scope. But you can just override the window.fragment.render function if you want to use something else.

<div data-fragment-json="fragment.json">This is {{adjective}}!</div>

HTML as JSON

If the element already has an innerHTML, you only use the data-fragment attribute, and don't provide a custom renderer; fragment.js will attempt to render Mustache, Handlebars then Underscore with the innerHTML as input.

<div data-fragment="mustache-fragment.html">{"adjective":"fantastic"}</div>

Combining

Of course, combining the two attributes also works.

<div data-fragment="mustache-fragment.html" data-fragment-json="fragment.json"></div>

Media queries

To only load certain fragments dependning on media queries, use the data-fragment-media attribute.

<div data-fragment="fragment.html" data-fragment-media="(max-width: 250px)"></div>

Configuring fragment.js

Overriding

To override the attribute names, just change fragment.html and fragment.json

fragment.html = 'src';
fragment.json = 'json';
<div data-src="mustache-fragment.html" data-json="fragment.json"></div>

JavaScript interface

To manually evaluate an element, or the complete document manually, set the manual toggle to true (before including the script).

fragment = { manual: true };

And the following (after including the script)

fragment.evaluate(element); // evaluate just one element
fragment.evaluate(); // evaluate the whole document

Install

Simply use bower.

bower install fragment.js

Contact

If you have any questions or suggestions that doesn't fit GitHub, send them to @DanielRapp

fragment.js's People

Contributors

danielrapp avatar foopq avatar anthonyshort avatar

Stargazers

Morteza Geransayeh avatar Nuno Bentes avatar  avatar zhaoran avatar Hoe-Kit CHEW avatar xw avatar  avatar Steven C Lange avatar Kevin Segal avatar Gokhan Celik avatar Alex Larioza avatar Gaurav Singh avatar  avatar  avatar Georgi avatar Rui Pedro Silva avatar Dirco avatar Danielka avatar Fabian Mersch avatar  avatar Vu Tran avatar A B H I N A V / K R avatar Abhijit avatar  avatar 陈丽 avatar  avatar wangerniu avatar Milan Košir avatar Rajkumar Magar avatar Shiva Manhar avatar lixinliang avatar Javier Diaz avatar Amit Kapoor avatar Udi Talias ⚛️ avatar Oleg Utkin avatar  avatar Chavin avatar William Dibbern avatar jaceechan avatar Mohammad Dayeh avatar 践行者 avatar Gabe Montalvo avatar Angus H. avatar Mark Steve Samson avatar Malachi Simonyan avatar Fredrik Jonsson avatar liuxy avatar JT5D avatar Gérald Spettel avatar Lucas Paolotti avatar James Gardner avatar Michael Anthony avatar  avatar Shaun Trennery avatar Long Chavous avatar Magicshui avatar Dmitry avatar apan avatar Basti Becker avatar  avatar Aswini S avatar Péter Maróti avatar Jamall Harris avatar Natanael Maldonado avatar Leonardo Cabral avatar Panagiotis Tigas avatar Wolmar Nyberg Åkerström avatar Zach Green avatar Matthias Max avatar roxstyle avatar Hugo Henrique avatar Milo Luca avatar kingston avatar 茗梓 avatar Pavel avatar Vishal Shah avatar Jan Viehweger avatar Crew Moss avatar Ville V. Vanninen avatar  avatar  avatar David Mihalcik avatar Simon Vandereecken avatar Michal Wozniak avatar Owais Lone avatar Vitaly Rotari avatar Safa Yasin YILDIRIM avatar Nikos M. avatar Christian Nennemann avatar Andreas Klein avatar Gonçalo Morais avatar Fotis Evangelou avatar Sönke Kluth avatar Joe Johnston avatar Cristian Yáñez avatar Jeremy McDuffie avatar Serg Nesterov avatar  avatar Gaëtan Ark avatar Helmy Giacoman avatar

Watchers

arden avatar  avatar Vitaly Rotari avatar Flávio H. Ferreira avatar Adriano Fernandes avatar RJ avatar Dominic Martineau avatar Paulo dos Santos avatar  avatar Ramin Mohammadi avatar  avatar bingdian avatar Ville V. Vanninen avatar Korn Kutan avatar Johnny avatar  avatar Wen Yi Chu avatar Dmitry Seredinov avatar Yamini avatar Michael Anthony avatar  avatar Kohei Otsuka avatar  avatar mobiletonster avatar  avatar

fragment.js's Issues

JSONP support

JSONP support should be added, but I can't think of an elegant API implementation. I originally just wanted to detect if the data-fragment-json url contained callback, but that leaves out APIs, like reddit, that uses jsonp instead.

Adding a whole new attribute just for that feels unnecessary and inelegant.

Simplify loops

At line 46 there's three loops that I'm convinced could be simplified to just one, without compromising code readability. Perhaps a js-based finite state machine is appropriate?

Doesn't work for me, i am using Mustache.js in Chrome browser

i created a simple example using fragment.js and mustache.js. But the line of code
var fragments = document.querySelectorAll('[data-'+fragment.json+']:not([data-'+fragment.html+'])');

doesn't give any fragments.

Testing fragment by {{ name }}

Change the way fragment.manual is evaluated

As at line 3 , a local fragment variable is defined, if you want to make line 139 efficient, you would better use

if (!window.fragment.manual)

instead of

if (!fragment.manual)

or save a local variable of the actual window.fragment in the first place.

Improve exposure

The way we expose the fragment variable at line 1 is not optimal since it requires the user to modify the variable after the script has been loaded. Maybe there's another way?

Load new fragment with ajax

Hi, is it possible to reload a json or html fragment with ajax?
I tried to change the attribute then load the script again but it didn't work. Any suggestion? Anyway I think this is a good function.

Support for nested templates

would be sweet:

layout.html:

<div data-fragment="partial1.html"></div>
<script src="fragment.js"></script>

partial1.html:

<div data-fragment="partial2.html"></div>

partial2.html:

<strong>you made it</strong>

and receive:

<div data-fragment="partial1.html">
  <div data-fragment="partial2.html">
    <strong>you made it</strong>
  </div>
</div>

at the end.

Callback mechanism

a callback mechanism for once a fragment/all fragments are loaded seems necessary, as neither $(document).ready() nor $(element).ready() would work for obvious reasons.

Error fetching package from Bower

Just FYI, I think the package registry in Bower is wrong, you have:

fragment git://github.com:DanielRapp/fragment.js

I registered it again as fragment.js with this URL:

fragment.js git://github.com/DanielRapp/fragment.js.git

Add IE support

Add IE support, by providing wrappers for XMLHttpRequest, Array.prototype.forEach and document.querySelectorAll

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.