Drab: problem with handlers and templates

I have one controller, for example: LessonController.
Few Views: StudentView, TeacherView, ModeratorView.
And of course, I have different folders for templates for every view. These folders contain templates with equal names but different content.

I want to keep this structure and use Drab with some of the templates.
As I can have only one Commander per Controller I faced a problem:
handlers which are not from current template throw a generic error: “An error occurred. Please contact the System Administrator.”
I know that Drab usage means that I can fully generate everything. But I want to keep this structure.
How can I maybe clarify that I want to use this particular handler only in some particular template?

I’m not sure I understand your issue fully, but would shared commanders help here?

Sharing with public [:handler_name] will make handlers accessible from everywhere.
But I want to use some handlers only on specific templates.
For example some handler has this:
peek(socket, "show.html", :lesson)

It’s a show.html template and I want to make this handler work only on this template.

I am not sure if I understood the issue.
Handlers and commanders are the entities similar to actions and controllers in the plain Phoenix. The “normal” (not shared, public) commander is bound to the controller, so they are not bound to views or templates.

Could you please share the code and the full error from iex/log?

poke/3 and poke/4 works on the partials. When you generate the page with “index.html”, and have a partial “users.html” in there, you can use

poke socket, UsersView, "users.html", assign: "value"

to update the assign only in the specific partial.

https://hexdocs.pm/drab/Drab.Live.html#module-partials

Yes, I know. I’m sorry I realized that it’s another problem.
I just need to get current template name. It’s enough for solving my problem.

Btw can you suggest how to restrict functions like set_prop and insert_html to use only in some particular templates? Like with poke and peek.

You need to distinguish the template with CSS selector. Like cover it with a <div id=template1> and use the selector:

set_prop socket, "#template1 button",  style: %{"backgroundColor" => "red", "width" => "200px"}

It is possible to extend the functionality of those functions to work with partials, just like Drab.Live does:

set_prop socket, "users.html", "button",  style: %{"backgroundColor" => "red", "width" => "200px"}
set_prop socket, UserView, "users.html", "button",  style: %{"backgroundColor" => "red", "width" => "200px"}

But in this case it would work only if the template and partials are generated with Drab.Live.EExEngine (has .drab extension). I don’t know if it would not be confusing, but by the other hand, good error message with explanation (can’t find the partial “users.html”. Try to add .drab extension instead of .eex) would do the trick.

Do you guys think it is worth to do it?

2 Likes

Thanks!
Logs already provide all the information about errors. So I think it’s not necessary.

By the other hand it would add the unnecessary complication to Drab.Element (which is supposed to be the simplest Drab’s module) and make it dependent on Drab.Live.