Home

Awesome

Kenobi

Render external objects and array in view engines

NPM Version Build Status

Install

npm install kenobi

How it works

In this example we are using the product hunter's API.

The options variable sets the request.

var options = {
	url: "https://api.producthunt.com/v1/posts",
	auth: {
  		bearer: "XXXXXXX"
	},
	headers: {
    	'User-Agent': 'request'
	},
	method: "GET",
	json: true,
	timeout: 10000
};

After it is sent as the first parameter, after is set the page view path.

kenobi(options, '/views/index.ejs', function(page){
	res.end(page); // Response
});

Detail: You can sent only the url string instead request object. However the request body act as default (see default properties), ex:

kenobi('https://api.producthunt.com/v1/posts', '/views/index.ejs', function(page){
  res.end(page); // Response
});

So, then we can treat the object in view. Then a global object is returned. Accessed by the name of body. See how it happens in ejs example:

<body>

<% if (_.posts.length) { %>
	<ul>
		<% _.posts.forEach(function(post){ %>
			<li><%= post.name %></li>
		<% }) %>
	</ul>
<% } %>

</body>

Get response without view

For return only response object:

kenobi(options, function(response, err){
    if (err) res.end(err);
    res.send(response);
 });

Send only a local object (without external request) and render in template:

Use request false

var object = {name: 'luke', request: false};

kenobi(object, pathTofile, function(page, response, err){
    // For local objects cases, response always be null

    if (err) res.end(err);
    res.end(page);
});

Using HTML instead template option:

This option is still very limited, the current version is still not possible to make operations and comparisons with the variables passed to the html. Ex:

anything.js

var object = {name: 'Obi Wan'};

kenobi(object, 'index.html', function(page, response, err){
    if (err) res.end(err);
    res.send(page);
});

index.html

...
<body>
  <h1>{{name}}</h1>
</body>
...

Callback Return

page String = result of rendering

response Object = response from request

error Object = error from operation, if not exist must be null

kenobi(options, pathTofile, function(page, response, err){
  		console.log("Response: " + response);
  		res.send(page);
});

In Response Object, you can access some data like statusCode, body...

Template Examples

It sent an object to the template with the primary key accessed by an underscore. Examples:

Ejs:

<% _.posts.forEach(function(post){ %>
	<li><%= post.name %></li>
<% }) %>

Jade:

each post in _.posts
	li= post.name

Request Object (optional params)

The first argument can be either a url or an object. The only required option is uri, all others are optional.

View Engines

Who's using Kenobi

You're using Kenobi too? Let me know :)

History

See Changelog for more details.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT License © Raphael Amorim