Skip to content

Commit

Permalink
add Hello World example
Browse files Browse the repository at this point in the history
  • Loading branch information
nicocube committed Jul 23, 2015
1 parent 906a119 commit 5fcb0a2
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/helloword/README.md
@@ -0,0 +1,8 @@
# "Hello world" example

First install :

npm install
npm start

Then launch `http://localhost:3000/` to test
33 changes: 33 additions & 0 deletions 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)
23 changes: 23 additions & 0 deletions 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": {}
}
Binary file added examples/helloword/public/favicon.ico
Binary file not shown.
39 changes: 39 additions & 0 deletions 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/')
24 changes: 24 additions & 0 deletions 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"},'')
)
)
}
}

0 comments on commit 5fcb0a2

Please sign in to comment.