Specific question: this code works in a backbone template index.jst.eco, but it doesn't work in index.jst.ejs
<ui>
<% for entry in #entries.models: %>
<li> <%= entry.get('name') %></li>
<% end %>
</ui>
I'd like to know why (i.e. how to fix it for ejs), and, more generally, is there comprehensive documentation for how to write code in ejs templates? I can't find anything that goes into detail. As I've been playing around with ejs and eco, I've noticed the presence or absence of a : or a bracket can make a huge impact but I can't figure out how to know what to use and when.
I'm willing to use jst.eco or jst.ejs depending on which syntax has better documentation.
Just encountered this problem, here's my solution!
<% entries.each(function(entry){%>
<li><%= entry.get('name') %></li>
<% });%>
This is how that eco code would be written in jst.ejs.
<% for (var i = 0; i < entries.length; i++) { %>
<li> <%= entries.models[i].get('name') %></li>
<% } %>
However, I can't get it to work with a for entry in entries.models iterator
From what I've known, jst.eco format is CoffeeScript embedded into jst template, and jst.ejs will only work with JavaScript. That's why in your case it can render this JavaScript iteration:
<% for (var i = 0; i < entries.length; i++) { %>
but not this CoffeeScript interation:
<% for entry in #entries.models: %>
Related
I'm trying to run this code using node.js
This code shows a "Syntax Error" when I run the code below.
<% friends.forEach(function(friend) %>
<% { %>
<li>I have a friend: <strong><%= friend %></strong></li>
<% }); %>
But it seems to work fine if I change the opening curly brace to this syntax.
<% friends.forEach(function(friend){ %>
<li>I have a friend: <strong><%= friend %></strong></li>
<% }); %>
Can someone please explain why this happens? I am using an ejs template engine.
When I try to run rake assets precompile I get this error: Unexpected token: operator (>).
I figured out it was because of index.js.erb, because changing the name of the file to .html.erb made it compile.
This is the content of index.js.erb:
$('#haikus').append('<%= j render(#haikus) %>');
<% if #haikus.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(#haikus) %>');
<% else %>
$('.pagination').remove();
<% end %>
haikus.js.coffee:
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 200
$('.pagination').html('Loading...')
$.getScript(url)
$(window).scroll()
index.html.erb:
<div id="haikus">
<%= render #haikus %>
</div>
<div class="row pagination">
<div class="large-12 columns">
<%= will_paginate #haikus %>
</div>
</div>
How can I fix it and make it compile?
Thanks!
I suspect this has a lot to do with it:
changing the name of the file to .html.erb made it compile
Have you tried removing the logic from your js.erb file? I really thinking about the "will_paginate" stuff not able to be called from a javascript file. Maybe you could just have this line in the file & see if that compiles?
$('#haikus').append('<%= j render(#haikus) %>');
If that doesn't work, then we know it's something to do with this; else it will be something to do with the will_paginate most likely
Sorry about my English. I have migrated plugins to Redmine 2.x, but Javascript functions does not work.
At the top of one of my plugins show.html.erb:
<% content_for :javascript_includes do %>
<%= javascript_include_tag 'hgp_markers.js', :plugin => 'hgp_markers' %>
<% end %>
hgp_markers.js:
function hideEditableMarkers() {
$$('.edit_marker').each(function(el) {
el.hide();
});
$$('.show_marker').each(function(el) {
el.show();
});
}
Event.observe(window, 'load', hideEditableMarkers);
And this line with javascript:
<%= link_to_function l(:button_edit), "hideEditableMarkers(); $('marker_#{m.id}_row').hide(); $('marker_#{m.id}_row_edit').show(); return false;", :class => 'icon icon-edit' %>
What am I doing wrong?
thanks in advance
Redmine 2.x (Rails 3.x) switches from Prototype to jQuery. So you need to migrate too.
Thus, Event.observe is a Prototype function.
See also: http://projects.andriylesyuk.com/projects/base/wiki/Porting-to-Redmine-2
I'm trying to use an array of objects to simplify maintaining a list of HTML buttons. Here's my code:
<% links = [{url:"https://github.com/drguildo", icon:"github"}, %>
<% {url:"http://www.flickr.com/photos/drguildo/", icon:"flickr"}, %>
<% {url:"http://instagram.com/therac25", icon:"instagram"}, %>
<% {url:"http://www.last.fm/user/drguildo", icon:"lastfm"}] %>
<% for link in links: %>
<img src="/img/icons/<%= link.icon %>.png" />
<% end %>
The problem is the resulting array looks like this:
[object Object],16,[object Object],17,[object Object],18,[object Object]
which messes up the output. Why are the objects interspersed with integers and how can I prevent it?
I'm probably doing this in a very sub-optimal way (I'm new to DocPad, ECO and CoffeeScript) so any suggestions on how to improve my code would be appreciated.
You don't want to be creating an array like this within the template. The point of eco templates it to separate the logic/data from the presentation. Declare the data separately, then use it to render the template.
Like this
eco = require "eco"
fs = require "fs"
template = fs.readFileSync __dirname + "/views/test.html.eco", "utf-8"
console.log eco.render template, links: [
{url:"https://github.com/drguildo", icon:"github"},
{url:"http://www.flickr.com/photos/drguildo/", icon:"flickr"},
{url:"http://instagram.com/therac25", icon:"instagram"},
{url:"http://www.last.fm/user/drguildo", icon:"lastfm"}
]
and then just
<% for link in #links: %>
<img src="/img/icons/<%= link.icon %>.png" />
<% end %>
I'm trying to render the creation of a post with jquery ajax but I can't seem to get it to work correctly. It seems to render the post with very strange (I think nonexistent) styling and the flash message appears only after the page is refreshed. The post also get formatted correctly after the page is refreshed.
Here's the javascript:
$("#micropost_form").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice))%></div>');
$("#user_info").html("<%= pluralize(current_user.microposts.count, "micropost") %>");
$("#feed_items").prepend(" <%= escape_javascript(render(:partial => #micropost)) %>")
$("#micropost_form")[0].reset();
The body of my application layout:
<body>
<div class="container">
<%= render 'layouts/header' %>
<section class="round">
<div id="flash_notice"><%= render 'shared/flash_messages'%></div>
<%= yield %>
</section>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
Here's my home page (rendered in the yield of the application layout):
<table class="front" summary="For signed-in users">
<tr>
<td class="main">
<h1 class="micropost">What's happening?</h1>
<%= render 'shared/micropost_form' %>
<%= render 'shared/feed' %>
</td>
<td class="sidebar round">
<%= render 'shared/user_info' %>
<%= render 'shared/stats' %>
</td>
</tr>
</table>
Here's my feed partial:
<% unless #feed_items.empty? %>
<table id="feed_items" class="microposts" summary="User microposts">
<%= render :partial => 'shared/feed_item', :collection => #feed_items %>
</table>
<%= will_paginate #feed_items %>
<% end %>
My flash messages partial:
<% flash.each do |key, value| %>
<div class="<%= key %>"><%= value %></div>
<% end %>
Let me know if there's any other code I should post.
So what am I doing wrong? If you want to look at all the code and run it locally to get a better understanding of what's happening, you can find it here: https://github.com/meltzerj/sample_app
When you're using ajax, you need to use flash.now for flash messages. See docs here. Rails is looking for a page refresh/redirect to push and pop from the flash hash stack, using now will set it immediately, then you can access it as above. You can use flash.now in replace of your regular flash usage in the controller. Something like this is very typical:
if #object.save
flash.now[:notice] = "Object successfully created"
else
...
end
So that will fix your flash issue.
As for the weird content/styling, it's a bit hard to comment without actually seeing the markup. The only thing that looks odd to me is the render :partial => #micropost
I'm not sure if #micropost is just a string, but you'd normally do something like render :partial => 'path/to/partial', :object => #micropost
Again it's almost impossible to say without seeing all the other markup