GithubHelp home page GithubHelp logo

cfv1984 / jsx-templates-in-nodejs Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 56 KB

Small library enabling NodeJS users to draw templates in JSX without most of the hassle required by other alternatives.

JavaScript 93.29% HTML 6.71%

jsx-templates-in-nodejs's Introduction

jsx-templates-in-nodejs

Small library enabling NodeJS users to draw templates in JSX without most of the hassle required by other alternatives.

Requirements

Usage

Setup a .babelrc like so:

{
    "plugins": [
        ["@babel/plugin-transform-react-jsx", {
            "pragma": "JSXTemplates"
          }]
    ]
}

Then setup your register hook like so:

  require("@babel/register");

Then simply require your JSX files like so:

// views/profilePage.jsx

const {Transform} = require('jsx-templates-in-nodejs');
const UserHeader = require('./profile/Header');
const ProfileDetails = require('./profile/Details');
const defaults = {id: -1};

module.exports = ({id} = defaults) => <div>
  <UserHeader for={id} />
  <ProfileDetails for={id} />
  <p>Whaaaaaaaaaaat</p>
</div>;
// express route handler

const { Renderer } = require('jsx-templates-in-nodejs');
const profilePage = require("views/profile");

app.get('/users/:id', (req, res) => {
  res.send(
    Renderer(
      // note: this assumes profilePage is a function that returns the JSX for the requested page
      profilePage({ 
      id: req.params.id
    }), {shouldFormat: true}    
    )
  );
});

Express template engine

There is also an express template engine, developed in order to prevent the boilerplate above.

To use it simply do:

const { Engine } = require('jsx-templates-in-nodejs');

const app = express();

app.set('views', './views');
app.engine('jsx', Engine());
app.set('view engine', 'jsx');

app.get('/users/:id', function (req, res) {
    res.render('profile', { 
      // This will be received in the first argument of the function contained in the JSX template file described above
      id: req.params.id,
    });
});

jsx-templates-in-nodejs's People

Contributors

cfv1984 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.