What is the standard method to require a defined JavaScript module inside of a single Phoenix Template?
I don't want the module required anywhere but inside this one template.
Here is a snippet of the files I am using.
let TrendChart = {
//... some JS module code here
}
This has the standard app load/require.
...
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
<script>require("web/static/js/app")</script>
...
<!-- what do i put in this template to require / load the TrendChart module code? -->
<!-- i don't want it required globally, so i don't want to put it in the app.html.eex file -->
I'm really looking for a way to have two @inner
blocks in the main layout. One for the content, and one for additional JavaScript items to be loaded after the content.
Something like sections in ASP.NET MVC. (I know, I know!)
So the app.html.eex
would end up something like this:
...
@inner
...
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
<script>require("web/static/js/app")</script>
*something here to load page/template specific javascript*
You can save the file to web/static/assets/trend_chart.js
then it will be copied to priv/static/trend_chart.js
and available from <script src="<%= static_path(@conn, "/trend_chart.js") %>"></script>
.
All files saved to the web/static/assets
directory are directly copied to priv/static
without going through the build phase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With