How to identify the template or controller

Hello, I have a couple of style which I need to load in my site head tag, but I needn’t it to load all of my pages, for this purpose I’m finding the way that it just loads in the controller concerned, for example this code should be loaded when my user load BlogController or view or template, each element can load those.

I have read these link:

https://hexdocs.pm/phoenix/routing.html
https://hexdocs.pm/phoenix/Phoenix.Router.html

and found blog_path but it didn’t work for me because you think I have this router:

name.com/blog/:alias_link

I need it to be loaded in all of the link not just ID 3, when I print blog_path , it shows me like this:

/blog if my user enters link like /blog/ , it won’t work

the code whether this is a BlogController or not?:

    <link rel="stylesheet" href="<%= static_path(@conn, "/css/carouFredSel.css") %>">
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/prettyPhoto.css") %>">
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/sm-clean.css") %>">
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/style.css") %>">

Check out this post I made a while back:

1 Like

I didn’t understand, I want to write a block code in the app.html.eex , but when the router would be for example BlogController or PageController

how can I do this?

ex: app.html.eex

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

   if BlogController do
    <%= metatags(assigns) %>
    <%= ogtags(assigns) %>
   end

You can use Phoenix.Controller.controller_module(conn) to get the controller module and include stuff based on that. You could also move this into a view function and use that instead. https://hexdocs.pm/phoenix/Phoenix.Controller.html#controller_module/1

1 Like

Instead of guessing the controller, I would just assign a variable in the BlogController module, and use that in the template. E.g.

defmodule MyAppWeb.BlogController do
  # ...
  plug :assign_styles

  defp assign_styles(conn, _opts), do: assign(conn, :exclude_styles, true)
end
<%= unless assigns[:exclude_styles] %>
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/carouFredSel.css") %>">
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/prettyPhoto.css") %>">
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/sm-clean.css") %>">
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/style.css") %>">
<% end %>
1 Like

Thank you, I used this:

<%= if Phoenix.Controller.controller_module(@conn) == TrangellHtmlSiteWeb.ScreenshotController do %>