I am having trouble pulling in end-of-page scripts (or other content) from my layout file after making the call for '@content.' I am using coffeekup templating and have the following default.html.coffee layout file.
doctype 5
html ->
head ->
meta(charset:"utf-8")
title("Docpad")
if @document.description?
meta(name:"description", content:@document.description)
stylesheets = ['/styles/app.css']
@getBlock('styles').add(stylesheets).toHTML()
body ->
div class:"row", ->
div class:"large-12 columns", ->
h1(class:"docs header", "Header")
hr()
@content
@getBlock('scripts').toHTML()
The problem I'm running in to is that '@content' correctly yields and renders page-specific content only if nothing follows it (if the @getBlock('scripts') line above, for example, is removed or commented out). With the above code, however, the getBlock call for scripts succeeds but '@content' does not insert content. Any help appreciated, Thanks.
Let's take a look at the compiled javascript of your code there. We can use the compiler on coffeescript.org to do this:
Notice how
this.content
is just a non-actionable statement. Just as if I did this:"a"; "b"; "c"; "d"
nothing would do anything.The usage, or intention of the code you've povided seems to imply a misunderstanding of the way either CoffeeKup or CoffeeScript works, so let me evaluate what's going on and why sometimes it works and sometimes it doesn't.
When we do
div -> "blah"
it comiles todiv(function(){return "blah";})
which says passdiv
a function that when called will return the string blah. Now CoffeeKup knows that any strings returned to it it should render for convenience. But as we can't return multiple things (as the first return exists the block), what are we to do?CoffeeKup provides the
text
function, allowing us to do:Which when compiled looks like:
Which is just what we want, as the
text
call, just likediv
and all the other element calls, doesn't return a string and outputs the content directly.So all in all, the solution is to prefix:
With the
text
call, so it becomes:If you ever want to escape the HTML entities (so convert
<
to<
) then you'll want to add theh
call as well like soh "content to be escaped"
and combined withtext
will look liketext h "content to be escaped"
- however that is just something to note, not something you need right now or here.