A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/michaelchance/tlc below:

GitHub - michaelchance/tlc: Tag Line Commands

A simple way to execute javascript functions and transform+display json data within HTML.

<h1 data-tlc="command --parameter; othercommand --with=$variable">Hello!</h1>

An example of usage with jQuery
//Include the tlc.js file from this repo in your html file
var tlc = new TLC();
tlc.run($('#someElement'), {'message':'Hello World'});

in your html:

<div id="someElement">
	<h1 data-tlc="bind $var '.message'; apply --append;"></h1>
</div>

Outputs

<div id="someElement">
	<h1 data-tlc="bind $var '.message'; apply --append;">Hello World</h1>
</div>
An example of usage with Express
var express = require('express');
var app = express();

var http = require('http');

var fs = require('fs');

var TLC = require('tlc');
var tlc = new TLC();
var cheerio = require('cheerio); // jQuery-alike library for DOM parsing

app.get('/', function(req,res,next){
	var $document = fs.readFileSync('index.html', 'utf-8);
	tlc.run($document, { "message" : "Hello World" });
	res.send($document.html());
});

http.createServer(app).listen(3000);

the contents of index.html:

	<!DOCTYPE html>
	<html>
	<head>
	<meta charset="utf-8">
	<title>test</title>
	</head>
	<body>
		<!-- 
		note here '.message' is the JSONPath string to 
		a member of the JSON object being translated 
		-->
		<h1 data-tlc="bind $msg '.message'; apply --append=$msg;"></h1>
	</body>

	</html>

When running the app, localhost:3000 will serve

The above example introduces 2 of the core commands in tlc, bind and apply. Commands are functions, executed with arguments, within a context. Each tag creates a new context, and before any commands are executed, it consists solely of the JSON data object being translated.

bind is one of the most important commands in tlc- it creates+links a variable within the tlc's context to a component of a JSON data object. For example: bind $msg '.message'; from above create's the variable $msg, and writes "Hello World" to it from the JSON object. This command takes 2 arguments- first, a variable reference to bind into, and second, a JSONPath formatted string to reference the binding.

Our second command, apply --append=$msg;, de-reference's our variable $msg. The apply command is used for applying changes to the tag we are executing commands on, in this case, appending to it.

Note that we can shorten our tlc statement to bind $msg '.message'; apply --append; and it will still apply the contents of the msg variable. This is because by binding it, msg has become the focus variable. All core commands will use the focus variable if none is provided. This is both for syntactic convenience, and also to allow simple chaining on the focus variable, for example, if we were rendering a VERY simple product page:

var productData = {
	"name" : "pie"
	"price" : "3.14"
	}

<div class="price" data-tlc="
	bind $cents '.price';
	math --mult='100' --mod='100';
	bind $dollars '.price';
	math --precision='0';
	format --prepend='$' --append='.';
	focus $cents;
	format --prepend='<span class=cents>' --append='</span>';
	apply --append=$dollars;
	apply --append=$cents;
	"</div>

Holy new commands, batman! A few things to note here:

The full list of core commands:

Modules allow developers to extend the command set usable in tlc:

var tlc = require('tlc');
var moduleObject = require('./mymodule.js');
tlc.addModule('mymodule',moduleObject);

This module can be referenced in templates:

<div data-tlc="bind $var '.some.var'; mymodule# --arg=$var; apply --append;"></div>

One module that is not-quite-core but you might find awfully useful is template

For more information, please visit the Module API

<div data-tlc="
	bind $new '.user.is_new';
	if(is $new --eq='1';){{
		bind $user '.user';
		template#translate --templateid='newUserWelcomeMessage' --data=$user;
	}};
	"></div>
<ul data-tlc="
	bind $arr '.path.to.array';
	foreach $item in $arr {{
		template#translate --templateid='listItemTemplate' --data=$item;
		apply --append;
	}};
	"></ul>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3