GithubHelp home page GithubHelp logo

bobjs's Introduction

bobjs

build HTML templates with Bob

This repository contains the source code for Bob as well as the source code for a series of examples on how to use Bob to create HTML templates. The examples cover topics such as basic templating, conditionals and recursive templates.

Basic template creation

  • Clone repo into your project folder
  • Create a script for your custom template
  • Import Bob into your script
    import Bob from './bobjs/src/Bob';

    export default class MyCustomTemplate extends Bob {

	constructor (data) {
	    super(data);
        }
        
        // {name} is a property of the data object passed in the constructor
        template () {
	    return `<h1>My custom template is {name}</h1>`;
        }
    }
  • In the project root file, import your custom template
    import MyCustomTemplate from './customtemplate';


    // in an index.html file, create an app mount and create a variable for it. the content of the built custom template will be appended there

    const appMount = document.getElementById('app');

    const myCustomTemplate = new MyCustomTemplate({'name': 'Custom Template});

    appMount.innerHTML += myCustomTemplate.build().render();
  • Transpile the project root file webpack [path/to/project_root_file] --output ./dist/main.js
  • Create a script tag declaration in an index.html file <script src="./dist/main.js"></script>
  • Open the index.html file to view the built template

Viewing examples

  • After cloning the repo, cd ./bobjs/src
  • Run npm run build to generate the compiled examples script
  • Open ./bobjs/src/index.html to view examples
  • After editing examples, rerun npm run build and refresh the webpage
  • Note that the src/index.js file contains all the data used in the Examples files

Composing templates

Simple templates

import Bob from './bobjs/src/Bob';


export default class MyCustomTemplate extends Bob {
    
    constructor (data) {
        super(data);
        this.childTemplates = { TemplateHeader };
    }
   
    template () {
	return `
            {{TemplateHeader}}
            <p>Custom template name: {name}</p>     
        `;
    }


    class TemplateHeader extends Bob {

        template () {
            return `
                <h1>My custom template header</h1>
            `;
        }
    }
}

Data-bound templates

// Assume the following data has been passed into the root template MyCustomTemplate in the root project file

const data = [
    {
	'name': 'Bob',
        'job': 'builds'
    },
    {
	'name': 'Slash',
	'job': 'shreds'
    }
];
import Bob from './bobjs/src/Bob';

export default class MyCustomTemplate extends Bob {

	constructor (data) {
	    super(data);
	    this.childTemplates = { JobCard };
        }
	
        /*
	   using 'each' here since the data passed to the root template is an array. notice how the template JobCard is bound to each item index.
           if it is necessary to bind an object to the template, use {{TemplateName|objectKey}} where objectKey is a property in data. note that only
	   objects, arrays or array indices (when using 'each') can be bound to a template.
	*/
        template () {
	    return `
                <each>
		    {{JobCard|(index)}}	
                </each>	
            `;
        }
}

class JobCard extends Bob {

	constructor (data) {
     	    this.data = data;
        }


        template ()  {
	    return `
		<div>My name is {name} and I {job}.</div>
            `;
        }
}

The Examples folder contains examples on how to use conditions in templates and how to create templates to consume recursive data. The examples can be viewed and modified locally by following instructions in the Viewing examples section above.

Todo

  • Add tests
  • npm

bobjs's People

Contributors

wyyz514 avatar

Stargazers

 avatar  avatar

Watchers

 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.