Phoenix different title per page with root module - phoenix-framework

This question is similar to (but not an exact duplicate of) Phoenix Framework - page titles per route.
Ideally, i want to create titles like in the described question, but I am using a root layout since my project uses Phoenix LiveView. The HTML skeleton including the head and title HTML tag are part of the root template (root.html.eex). The app template extends on that from my understanding. I implemented the code from the above question
<title>
<%= if Kernel.function_exported?(#view_module, :title, 2) do %>
<%= #view_module.title(Phoenix.Controller.action_name(#conn), assigns) %> - StHub
<% else %>
StHub
<% end %>
</title>
and created a title function inside of my specific page view
defmodule StHubWeb.WowsView do
use StHubWeb, :view
def title(_action, _assigns) do
"Dashboard"
end
end
but the else branch of the code is triggered. Upon further inspection, I think that the issue is with using a root template, because the #view_module while rendering the root template is StHubWeb.LayoutView, and only inside of the LayoutView/app.html.eex template, the #view_module is my actual view (StHubWeb.WowsView).
I am not sure how to solve this other than removing the root template, but then my LiveView will have to contain the entire HTML skeleton all the time.
Maybe there is a way for me to define a title function in my LayoutView that will grab the title from StHubWeb.WowsView, but I am not sure how to do that.
Thanks for the help!

Related

Rails aways renders the application.html.erb instead of wanted views

My Ruby on Rails application always renders the layouts/application.html.erb view instead the views I want it to. Has anyone an Idea why that might be so?
My routes file looks like this:
Rails.application.routes.draw do
root 'startup#index'
resources :users
end
And my application_cotroller.rb is pretty much empty:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
By default, a controller action in Rails renders the view template for your action, wrapped up into a layout (which is application/layout)
ActionView::TemplateHandlers manages the lookup for the extension (.html.erb, .html.haml, .json.erb, etc ...)
so, in an action called index, you will get this implicit call unless you call render yourself :
def edit
# your code
render action :'edit', layout: 'application/layout' # implicitly called
end
Rails will then start processing your layout and put the content of your edit template in place of any yield within your layout. Thus, a typical layout will look like this :
<!doctype html>
<head>
</head>
<body>
<!-- layout content before view -->
<%= yield %>
<!-- layout content after view -->
</body>

Middleman Blog: Custom layout not loading

In Middleman, I am trying to set up a blog site, using custom layout for the blog. My problem is that the main layout is loading, but the blog layout for my articles is not. The article files are being served in with their plain body.
In source/layouts/ I have two files: layout.erb and article_layout.erb.
My intent is to use article_layout.erb for my blog articles.
In config.rb I have the following:
activate :blog do |blog|
blog.sources = "articles/{category}/{year}-{month}-{day}-{title}.html"
blog.layout = "article_layout"
end
I have also tried moving article_layout.erb to source/articles/ as well as prepending the config.rb file like this: blog.layout = "layouts/article_layout"
Another failed approach is to comment out the above option and configure the layout by adding this line instead: page "/articles/*", layout: "article_layout".
So far none of these approach show a difference. Since the default layout is not being rendered I would expect some sort of error message if the path to the layout cannot be found, but nothing shows up.
I managed to replicate your problem with my own Middleman blog setup.
The docs are unclear on this because there is a broken link in the layout section of Blogging.
You need to use the nested layout feature of Middleman and wrap your custom layout in:
<% wrap_layout :layout do %>
<% end %>
So your article_layout.erb would look something like this:
<% wrap_layout :layout do %>
<div class="article-container">
<article>
<h2 class="article-title"><%= current_page.title %></h2>
<%= yield %>
</article>
</div>
<% end %>
And keep your custom layout in the source/layouts folder.
Here are the docs for Nested Layouts for your reference.
I hope this helps.

Frontmatter in Padrino

In Middleman, there is a feature called frontmatter. Basically, you can put some YAML in front of your view and access it in the view and the layout like this:
---
my_list:
- one
- two
- three
---
<h1>List</h1>
<ol>
<% current_page.data.my_list.each do |f| %>
<li><%= f %></li>
<% end %>
</ol>
Is there a similar thing in Padrino?
Such feature does not come with padrino, where you usually would let these values be filled by your controllers.
Depending on your use case, you might use the content_for output helper (http://www.padrinorb.com/guides/application-helpers).
This might help if e.g. want to define the title (as in html/head/title) -- which is usually set by the layout template -- in the "nested" template of your page.

rendering Ruby statements inside of rails views?

So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>

Globalize3 module translation for database

I got an issue with Globalize3, i have build a module to add translations in Admin for Preferences User
I display each translation by her ID and her locale. But i don't understand why the locale is not define when i want to display the page.
The gist for better show : https://gist.github.com/266562670cd8dab28548#gistcomment-43681
Thanks for your help
Fixed.
If you have a look at the params you are getting in the update action for the perference topic translation (just raise params[:preference_topic_translation]). You'll notice you probably have preference_topic_option_translation coming through as one of the attributes which doesn't exist on the model.
You need to update line #12 in the form:
<%= f.fields_for preference_topic_option_translation do |translate_form| %>
It should read:
<%= f.fields_for :preference_topic_option_translations, preference_topic_option_translation do |translate_form| %>

Resources