Coder Social home page Coder Social logo

gspettel / fragment.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danielrapp/fragment.js

0.0 1.0 0.0 326 KB

A tiny tool for easily loading html fragments and templates

Home Page: http://danielrapp.github.com/fragment.js/

License: MIT License

HTML 15.77% JavaScript 84.23%

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

Watchers

Gérald Spettel avatar

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.