ryloric
Hi!,
What’s the best way to sprinkle bits of javascript for use inside EEx templates, just simple things like handling button clicks, hiding tags etc. I’ve been using a <script> tag at the bottom of the template with some common functions in a global object created in assets/js/app.js.
Is this a reasonable pattern? What are recommended/best-practices around this?
Sorry if this question is a bit naive, I’m pretty new to web-dev and js in general.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 12 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
The big difference between
render_existingand what most templating engines allow for is in the possibilities in nesting.In phoenix you can only use
render_existingfor view modules you know about. So e.g. the outer layout html is aware of the view_module the controller told it to render (might be selected explicit or implicit). It’s however not aware of arbitrary nested partial templates, which are used somewhere deeper down the call stack of templates rendered within other templates.So
render_existingdoesn’t play well when you want a partial to add something to the layout.sfusato
I’m not aware of any library doing this like in Laravel, but it’s easy enough to set it up on your own.
Take a look at
render_existing:https://hexdocs.pm/phoenix/Phoenix.View.html#render_existing/3
You would have a bunch of these in your
app.html, or wherever you define your top-level layout files:and then a
page_layoutfolder structure per view template where you would create and place thesehead_scripts_css.htmlas needed.syukronrm
Hi everyone, newcomer here
Is there any pattern or library that has a similar pattern to Laravel’s blade templating [1]?
I think it is very straightforward when we want to add css or js on a single page.
[1] Blade Templates | Laravel 5.8 - The clean stack for Artisans and agents
ryloric
Thanks! It makes sense now, I was naively assuming browsers download it everytime.
peerreynders
Also bundling makes it possible to use modular JavaScript in the browser.
The bundler organizes the code in such a way that the various modules only interact with one another in a well defined manner. Browserify started doing this in 2011. It’s only since about 2017 that browsers started supporting ECMAScript modules natively - and even then using them tends to lead to a lot of additional requests because the aren’t bundled.
NobbZ
You are not “sending”, you only specify the resources source. The browser then chooses to cache or not to cache this resource and is free to not download it again in the future.
This way you can massively decrease the number of requests made to your server. Also a single large request is usually more effecient regarding the payload/header ratio than many small ones.
Of course you are totally free to use many entry points and separate your scripts that way you describe.
ryloric
Thanks for taking the time to reply!
I think I understand webpack a little better now, but what I’m a little unclear about is why we need to use a bundler at all with phoenix apps.
If my understanding is correct, SPA apps send a big blob of JS to the client, which once parsed and initialized creates markup, and handles everything client-side. So, it makes sense for them to pack everything into a single
app.jsand send when the page first loads.In phoenix though, everything is handled on the server, so I’m confused as to why we’re sending the whole
app.jsbundle on each page load. It’s not like we send all our HTML each page load, we only send the layout and what’s required for that page to render, why not do the same with JS? Load common libraries in thelayouttemplate and load page specific js in their respective templates.I feel like I’m missing something obvious, but not sure what it is.
sync08
I think this is a pretty reasonable way to start for your use case. The only possible issue is that your JS doesn’t go through the webpack pipeline if you do it page by page so any transformation / optimisation / minification will have to be done manually.
There is also the option of adding more entrypoints to the webpack config file. I tend to do this when I have shared JS relating to a feature spanning multiple pages. So instead of just app.js or sprinkles at the bottom of the page I’ll have blog.js, store.js, admin.js e.t.c.
sfusato
Take a look at Stimulus. This is exactly its use case.
peerreynders
And
deferseems to be well supported these days.The problem with a plain or
asyncheadscript is that it delays FCP (first contentful paint).You still have to be careful in some situations:
Use async or defer