Is there a functionality to render partial templates in the Beego? I used this functionality in RoR.
Ruby on Rails partials:
Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
Example from RoR:
<!-- html.erb -->
<h1>New zone</h1>
<%= render partial: "form", locals: {zone: #zone} %>
and
<!-- _form.html.erb -->
<%= form_for(zone) do |f| %>
<p>
<b>Zone name</b><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Yes, though they are not explicitly referenced as partials in the documentation, you can compose templates like you described.
From the documentation for views.
{{template "header.html"}}
Logic code
{{template "footer.html"}}
Because Beego is an MVC framework, it will automatically look for your "partials" in the views directory.
If you had another subdirectory called views/base then your partial would look like this:
{{template "base/header.html" .}}
A good reference implementation that will provide more examples in the context of a real world app, check out the wetalk project on github
Related
I'm inexperienced with middleman and ruby, but I've been trying to get Slate working so it generates a side navigation/list of header during build time instead of client side using javascript. The problem I am running into is getting the code to include the headers from partials.
An example of the directory structure:
Source
+--config.rb
+--includes
+--file.md
+--otherfile.md
+--index.html
+--layouts
+--layout.erb
Gist of layout and config.rb
Config.rb snippet for this:
require 'nokogiri'
helpers do
def toc_data(page_content)
html_doc = Nokogiri::HTML::DocumentFragment.parse(page_content)
# get a flat list of headers
headers = []
html_doc.css('h1, h2, h3').each do |header|
headers.push({
id: header.attribute('id').to_s,
content: header.content,
level: header.name[1].to_i,
children: []
})
end
[3,2].each do |header_level|
header_to_nest = nil
headers = headers.reject do |header|
if header[:level] == header_level
header_to_nest[:children].push header if header_to_nest
true
else
header_to_nest = header if header[:level] == (header_level - 1)
false
end
end
end
headers
end
end
Layout snippet for this:
<ul id="toc" class="toc">
<% toc_data(page_content).each do |h1| %>
<li>
<%= h1[:content] %>
<ul class="toc-section">
<% h1[:children].each do |h2| %>
<li>
<%= h2[:content] %>
<ul class="toc-submenu">
<% h2[:children].each do |h3| %>
<li>
<%= h3[:content] %>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
...
<div class="page-wrapper">
<div class="content">
<%= page_content %>
<% current_page.data.includes && current_page.data.includes.each do |include| %>
<%= partial "includes/#{include}" %>
<% end %>
</div>
</div>
Currently, only headers from the index.html file are getting populated and nothing from the included partials. I believe I may need the existing helper to occur post build similar to what is described in the Middleman docs for sitemaps using a ready helper. I believe I have to make another change to the config code so that it captures additional content outside of the page_content, but I'm not sure what that is due to lack of familiarity. Any pointers would be appreciated.
Edit: After looking into the middleman basics docs, there appear to be two helpers from the Padrino framework that I could make use of: capture_html and concat_content. I'm trying to find where the helper page_content is defined to get extra context for the specific changes I'm making.
Not familiar with that framework but looks like toc_data(page_content) only looks at the main content but not at the current_page.data.includes partials.
So guess you need to pass the partial to your toc_data function as well.
Maybe this works?
<%
full_content = page_content
current_page.data.includes && current_page.data.includes.each do |include|
full_content += partial("includes/#{include}")
end
toc_data(full_content).each do |h1|
%>
...
<% end %>
Hope that helps.
In order to concatenate the current page data with partials with the page_content, use the code below. This also changes what all is needed to yield a complete page.
<%
if current_page.data.includes
current_page.data.includes.each do |include|
page_content += partial("includes/#{include}")
end
end
%>
...
<%= page_content %>
I think I have the same issue with this.
Using multiple yields to insert content
And I tried the solution. I tried to have <%= yield :content %> in my application.html.erb and have content_for :content and the yield inside, in my view. But it is not working on my app. Can you please explain more or give sample scenario on my problem?
The links inside, must not reload, so I will not use render partial on every template I will display.
I just recently started learning Rails so it was all a little bit confusing for me. Thank you.
I tried this; this is just an example I will fix the connection of sidebar later.
in my applicaion.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Clinks</title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<section id="container" >
<%= render 'layouts/header' %>
<%= render 'layouts/sidebar' %>
<section id="main-content">
<section class="wrapper">
<div class="row">
<!-- %= yield % -->
<%=yield(:content) %>
</div><!--/row -->
</section><!--/wrapper -->
</section><!--/main-content -->
</section><!--/container -->
</body>
</html>
in my menu_tables.html.erb inside layouts folder.
<% content_for(:content) do %>
<%= render 'menu_tables/sidemenu' %>
<%= yield %>
<% end %>
<%= render template: "layouts/application" %>
in my routes.rb
Rails.application.routes.draw do
root :to => 'pages#home'
resources :menu_tables, except: [:show]
end
then in _sidemenu.html.erb under menu_tables controller is the code for the links, so each links from sidebar have different sidemenu.
May be you need to tell your controller to use layout 'menu'. In the link you provided names of the controller and layout matched, so it worked 'magically'.
See guides:
2.2.14 Finding Layouts
To find the current layout, Rails first looks for a file in
app/views/layouts with the same base name as the controller. For
example, rendering actions from the PhotosController class will use
app/views/layouts/photos.html.erb (or
app/views/layouts/photos.builder). If there is no such
controller-specific layout, Rails will use
app/views/layouts/application.html.erb or
app/views/layouts/application.builder. If there is no .erb layout,
Rails will use a .builder layout if one exists. Rails also provides
several ways to more precisely assign specific layouts to individual
controllers and actions.
(Note: you can check what layout is used in logs, look for line similar to Rendered menu_items/index.html.erb within layouts/application)
I have this (and other) navigation links:
<li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> |
These need to be put into the header part of my templates and they change based on the view template.
So in application.html.erb I have this:
<div class='submenu'>
<%= #app_menu %>
</div> <!-- end submenu -->
And in the view template I have:
#app_menu =
The question is, how do I put the top line (with the link_to, etc) into that instance variable and have Rails parse it properly with the HTML and without errors. Right now if I include <ul><li> it prints them out literally. And, of course, if I include <%= link_to... I get all sorts of errors.
Or is there perhaps a better way?
What I ended up doing was creating partials for each menu, so one partial _actors_menu.html.erb might look like this:
<%= image_tag "submenu-separation.png", :alt => 'separator' %> <li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
<li><%= link_to "Actors with Ampersand (&)", actor_path('search')+'/acute' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
Then I put #app_menu = "actors_menu" on each actor view. And so on for the other pages.
But I'm wondering if there is a smarter way to do all this?
Also, where and how would I set a global variable for the app_menu so as not to repeat it in every template?
Hello Guys I've a request for you.I've Drinks, Menus and Users routes.
I've this code in views/layout/application.html.erb:
<div>
<%= render "shared/navigation_bar"
</div>
How to make the navigation_bar to render in all the pages except in users pages?
I finally work around many methods, but I finally come up with a solution(not really stunnin', I must confess) , that works for me.
<div>
<% if controller.controller_name == "drinks" %>
<%= render "shared/navigation_bar" %>
<% elsif controller.controller_name == "menus" %>
<%= render "shared/navigation_bar" %>
<% end %>
</div>
The flow control doesn't include Users's controller, so the navigation_bar layout won't appear on User's pages.
That's it.If you guys have a better alternative just let me know.
I am sorry for my bad english. I just try to description my question. :)
I have an application layout that have a yield for display post in body. I have another yield :footerpost3 for display title of recent post on the footer.
When I in localhost:3000, the yield :footerpost3 display a recent of title correctly. but when i am click a post link, which is the url is localhost:3000/posts/3, the yield :footerpost3 display nothing.
Here is my code:
app/views/layout/application.html.erb
<!-- begin footer comment widget -->
<div class="footer_list widget_recent_comments">
<div class="title"><h5>Artikel Terkini</h5></div>
<%= yield :footerpost3 %>
</div>
<!-- end footer comment widget -->
app/views/store/index.html.erb
<% content_for :footerpost3 do %>
<% #postsMain.each do |dopostAll| %>
<div class="entry">
<ul>
<li class="recentcomments"><%= link_to dopostAll.title, dopostAll %></li>
</ul>
</div>
<% end %>
<% end %>
i hope my question is easy to understand.. :)
Looks like your root url is stores#index .
You must be initializing #postsMain in the stores#index action and generating the content_for footerpost3 in stores/index.html.erb.
When you click on a post, you will be taken to posts#show page. So you have to initialize #postsMain even in posts#show action and generate the content for footerpost3 even in posts/show.html.erb
The answer is there in your question. You are defining the "content for" footerpost3 in that block, which exists in index.html.erb. When you're on /posts/3, index.html.erb is not rendered, but rather show.html.erb is.
To solve this, you'd need to add the content in the show.html.erb template as well.
You could solve this in multiple ways. Using nested layouts would be one. For example, you might create a posts layout at app/views/layout/posts.html.erb, like so:
<% content_for :footerpost3 do %>
<% #postsMain.each do |dopostAll| %>
<div class="entry">
<ul>
<li class="recentcomments"><%= link_to dopostAll.title, dopostAll %></li>
</ul>
</div>
<% end %>
<% end %>
<%= render :file => 'layouts/application' %>
In this way, all the views of your PostsController would use this layout, which simply adds your footer content, then renders the application_layout.