bminer / node-blade

Blade - HTML Template Compiler, inspired by Jade & Haml
Other
320 stars 28 forks source link

Please write some tutorial, meteor with blade. #44

Closed crapthings closed 12 years ago

crapthings commented 12 years ago

it's really hard to get things working. some examples are good. if i make some new Meteor.Collection and find() how to list all results in blade.

how to bind events?

bminer commented 12 years ago

To be perfectly honest, I haven't spent much time using Meteor, so I don't really have much experience using it. You are welcome to revise this wiki page as you see fit: https://github.com/bminer/node-blade/wiki/Using-Blade-with-Meteor

Anyway, to answer some of your questions...

If you have any other questions, please feel free to let me know.

crapthings commented 12 years ago
div this is a demo

chunk(board)
    div= board.name
!= Meteor.ui.listChunk(Boards.find({}), __.chunk.last)

Your app is crashing. Here's the latest log.

Errors prevented startup:
Exception while bundling application:
ReferenceError: Meteor is not defined
    at /Users/crapthings/Desktop/jobboard/views/body.blade:5:1

3 | chunk(board)
4 |     div= board.name
5 > != Meteor.ui.listChunk(Boards.find({}), __.chunk.last)
6 | 

    at Function.template (eval at <anonymous> (/usr/local/lib/node_modules/blade/lib/compiler.js:140:23))
    at /usr/local/lib/node_modules/blade/lib/compiler.js:134:12
    at /usr/local/meteor/packages/blade/package.js:48:4
    at /usr/local/lib/node_modules/blade/lib/blade.js:40:4
    at Compiler.compile (/usr/local/lib/node_modules/blade/lib/compiler.js:184:2)
    at compile (/usr/local/lib/node_modules/blade/lib/blade.js:36:12)
    at Object.compileFile (/usr/local/lib/node_modules/blade/lib/blade.js:56:3)
    at /usr/local/meteor/packages/blade/package.js:29:8
    at [object Object].add_file (/usr/local/meteor/app/lib/bundler.js:196:5)
    at /usr/local/meteor/app/lib/bundler.js:97:16
Your application is crashing. Waiting for file change.
crapthings commented 12 years ago
chunk else
    .empty! No records were found
chunk(post)
    - var x = Session.equals("selected", post._id) ? "selected" : "";
    div(class=x)= post.name
!= Meteor.ui.listChunk(Posts.find({tags: "frontpage"}),
    __.chunk.last, __.chunk.else, {
        events: {
            'click': function (event) {
                Session.set("selected", this._id);
            }
        }
    });

chunk(post) where is this post from ?

bminer commented 12 years ago

To answer your first post, you cannot reference Meteor from the body.blade file. See the wiki page.

The Blade smart package renders the head and body templates during the initial page load, placing the rendered content into the initial HTML document. head and body templates may not contain references to Session or Meteor, as these variables are not available until after the initial page load. You can always load another template at runtime if you want your initial page to contain dynamic content. All Blade files/templates will be defined under the Template global variable, as normal.

To answer your second post, this is only an example. The post object is related to the Posts.find() call... see the example in this section of the Meteor documentation.

crapthings commented 12 years ago

i put it to another file then use body inlcude 'test', i got same error.

if i have a collection like...

var Apples = Meteor.Collection('apples');

how to use Meteor.ui.listChunk with blade Template.apples ?

use code from meteor doc, it works using return '

' + apple.name + '
';

but i can't make it working with blade.

bminer commented 12 years ago

@crapthings - You can't reference Meteor or Session from within body.blade or any files included in body.blade. If you need to do this, you need to render another template at runtime (i.e. use Meteor.ui.render inside of Meteor.startup).

to answer your second question...

if you have an Apples collection and you want to render each Apple using Meteor.ui.listChunk and apples.blade, do the following:

apples.blade

chunk(apple)
    p The color of the apple is #{apple.color}
!= Meteor.ui.listChunk(Apples.find({}), __.chunk.last);

Or... in Blade 2.0 (not yet released), you could do this:

apple.blade

p The color of the apple is #{apple.color}

apples.blade

chunk(apple)
    include "apple" exposing apple
!= Meteor.ui.listChunk(Apples.find({}), __.chunk.last);
crapthings commented 12 years ago

these code seems working

chunk(board)
    div= board.name
!= Meteor.ui.listChunk(Boards.find({}), __.chunk.last, __.chunk.else);

now i can get Template and Collection from console use Template.test();

but use another js file to append template there is no data at all.

if (Meteor.is_client) {

    Meteor.startup(function() {

        console.log(Template.test());

    });

}
bminer commented 12 years ago

@crapthings - I'm sorry, but I don't understand the problem. Could you please explain in more detail?

crapthings commented 12 years ago

i'm sorry about my english first.

how to attach *.blade to dom when domready ?

i want to append a list to dom after domready.

users.blade

chunk(user)
    div= user.name
!= Meteor.ui.listChunk(Users.find({}), __.chunk.last, __.chunk.else);

how to attach this template to dom when dom is ready ? $(Template.users()).appendTo('body'); don't work after domready. but when page is fullly loaded, i type Template.users(); i can see teamplate with proper data from collection, but only work in console.

is that js file order matter ?

crapthings commented 12 years ago

server\server.js

Users = new Meteor.Collection('users');

client\client.js

Users = new Meteor.Collection('users');

views\head.blade

title this is a demo

views\body.blade

p this is a demo

views\users.blade

chunk(user)
    div= user.name
!= Meteor.ui.listChunk(Users.find({}), __.chunk.last, __.chunk.else);

then i start meteor. use console insert some data.

Users.insert({ name: 'random name' });

now if i type Template.users(); in console i can see console log a html string with data i have insert before.

but i put a init.js file at the last line of body.blade like this

views\body.blade

p this is a demo
script(src='init.js')

public\init.js

$(function() {

    console.log(Template.users());

});

console.log show a blank line. there's no data. why ?

is that order matter ?

when page fully loaded there's a log

injectScript startLiveReload init

but all scripts seems load before this line.

bminer commented 12 years ago

To attach code to the DOM when ready, place code in Meteor.startup(function() {...}). Then, if you are using jQuery, for example, do something like $("body").replaceWith(Meteor.ui.render(Template.users) );. That will put the rendered content from users.blade into the <body> tag of your DOM.

To answer your second question, when you put a script in body.blade, you need to utilize Meteor.startup(). Otherwise, as documented in the Blade + Meteor wiki, your code may run before the templates are loaded. For example, look at the HTML source code in the browser. You'll notice that the order of the script tags matter, and your code is loaded alphabetically. To avoid this issue, wrap all code in Meteor.startup().

crapthings commented 12 years ago

wow i see the key point is i have to wrap Template.users with Meteor.ui.render

if the script is in proper order i don't have to use Meteor.startup()

$('#users').html( Meteor.ui.render(Template.users) );

thanks you always respond fast.

thanks lol

bminer commented 12 years ago

You're welcome. Happy coding.