diff --git a/examples/helloword/README.md b/examples/helloword/README.md new file mode 100644 index 0000000..862a66e --- /dev/null +++ b/examples/helloword/README.md @@ -0,0 +1,8 @@ +# "Hello world" example + +First install : + + npm install + npm start + +Then launch `http://localhost:3000/` to test diff --git a/examples/helloword/client.js b/examples/helloword/client.js new file mode 100644 index 0000000..84bdf9d --- /dev/null +++ b/examples/helloword/client.js @@ -0,0 +1,33 @@ +/* + * Copyright 2015 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +var t = require('llama')() + , main = require('./template.js')(t) + , name = t.$(data) + , page = main(name) + , data = document.getElementById('data') + +//console.log(document.getElementsByTagName('html').item(0).id) + +page.render() +page.listen(function(id, html) { + console.log('id', id, html) + document.getElementById(id).innerHTML = html +}) + +function onevent() { + console.log('event', data.value) + name(data.value) + history.pushState(null, null, '/'+data.value) +} + +data.addEventListener('keyup', onevent) +data.addEventListener('change', onevent) diff --git a/examples/helloword/package.json b/examples/helloword/package.json new file mode 100644 index 0000000..0b0916d --- /dev/null +++ b/examples/helloword/package.json @@ -0,0 +1,23 @@ +{ + "author": "Nicolas Lochet", + "name": "LlamaHelloWorld", + "description": "", + "version": "0.0.1", + "engines": { + "node": "0.12.*" + }, + "scripts": { + "start": "node server.js", + "prod": "NODE_ENV=production node server.js", + "watch": "nodemon server.js" + }, + "dependencies": { + "browserify-middleware": "^6.0.0", + "express": "^4.13.1", + "llama": "^0.2.2", + "morgan": "^1.6.1", + "serve-favicon": "^2.3.0" + }, + "devDependencies": {}, + "optionalDependencies": {} +} diff --git a/examples/helloword/public/favicon.ico b/examples/helloword/public/favicon.ico new file mode 100644 index 0000000..c11ecef Binary files /dev/null and b/examples/helloword/public/favicon.ico differ diff --git a/examples/helloword/server.js b/examples/helloword/server.js new file mode 100644 index 0000000..d2e9d8e --- /dev/null +++ b/examples/helloword/server.js @@ -0,0 +1,39 @@ +/* + * Copyright 2015 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +var express = require('express') + , app = express() + , router = express.Router() + , morgan = require('morgan') + , browserify = require('browserify-middleware') + , favicon = require('serve-favicon') + , t = require('llama')() + , main = require('./template.js')(t) + , options = {startIds:0, indent: 2} + +app.use(favicon(__dirname + '/public/favicon.ico')) +app.use(morgan('dev')) +app.use(router) + +router.get('/client.js', browserify('./client.js')); + +router.get('/', function (req, res) { + var name = t.$('World') + res.send(main(name).render(options)) +}) + +router.get('/:name', function (req, res) { + var name = t.$(req.params.name) + res.send(main(name).render(options)) +}) + +app.listen(3000) +console.log('Server is running.\nOpen http://localhost:3000/') diff --git a/examples/helloword/template.js b/examples/helloword/template.js new file mode 100644 index 0000000..a889b81 --- /dev/null +++ b/examples/helloword/template.js @@ -0,0 +1,24 @@ +/* + * Copyright 2015 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +module.exports = function (t) { + return function (name) { + //console.log(name() instanceof t.LlamaVar) + return t.html( + t.head(t.title('Hello ', name(), '!')), + t.body( + t.h1('Hello ', name(), '!'), + t.input({$:'data', value: name().val()}), + t.script({src: "./client.js"},'') + ) + ) + } +}