mooml模板在尝试渲染时返回null

时间:2011-03-04 05:59:19

标签: javascript templates mootools

我正在尝试渲染我即时创建的mooml模板,并在调用render时变为null。

这是我的代码:

var myTpl = Mooml.Template('tpl', function() {
  div({class: 'new-container'},
    div({class: 'new-title'},
      div({class: 'new-text'},
        'Put your title here:'
      ),
      div({class: 'new-input'},
        input({type: 'text', name: 'title', class: 'required', title: 'Title'})
      )
    ),
    div({class: 'new-content'},
      form({method: 'post', action: '#'},
        textarea({name: 'content', style: 'width: 100%'})
      )
    )
  );
});

// el is null after executing render
var el = myTpl.render(); 

我查看了firebug中的一些变量,myTpl var不为null,也不是render方法。我不太了解mooml在幕后做了什么,但我会认为我应该使用的是以下示例here

var template = new Mooml.Template('mytemplate', function() {
    div('Template on the fly');
});

template.render(); // returns <div>Template on the fly</div>

与往常一样,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

嘿,我有一个可怕的时间看着这个,主要是因为你的嵌套标签写得太乱了,并且寻找不匹配/语法错误,这让我忽略了你没有正确地分类这个课程的事实。

var foo = new Mooml.Template('tpl', function() {
    div({"class": 'new-container'},
        div({"class": 'new-title'},
            div({"class": 'new-text'}, 'Put your title here:'),
            div({"class": 'new-input'},
                input({
                      type: 'text',
                      name: 'title',
                      "class": 'required',
                      title: 'Title'
                })
            )
        ),
        div({"class": 'new-content'},
            form({method: 'post', action: '#'},
                textarea({
                    name: 'content',
                    style: 'width: 100%'
                })
            ) // form
        ) // new-content
    ); // end div function
}).render();

foo.inject(document.body);
这是有效的。 http://jsfiddle.net/YKTyL/2/

注意new Mooml.Template而不是传递对类本身的引用。它正是从正确的例子中盯着我们......

另外,我改变了你说类的实例:因为类是IE中的保留关键字,需要引用“”