Load environment variable in template file in Phoenix Framework - templates

In a controller, I can load environment variables defined in config.exs using Application.get_env/3.
Is it possible to do load an environment variable directly inside a template file, aside from passing it through a controller? In particular, I'd like to load it in one of the layout files.

This q/a from 2015 will be helpful. As Dogber said, Application.get_env/3 will do exactly what you want even in a template. Take a look at the template docs. Variables take the form:
<p><%= #key %></p>
So, you can do the following:
<div class="jumbotron">
<h3>This is h3 text</h3>
<%= Application.get_env(:sheldonkreger_app, :some_env_var) %>
</div>
Phoenix uses Elixir EEx templates so the above gets built at compile time and is safe and ready to use in your app.

Related

WebStorm doesn't recognize AngularJS 'vm' variable

I am currently using the AngularJS plugin. It's really helpful.
The only downside is that it doesn't recognize the vm variable.
I use these settings in my directive to use the vm variable in my view:
controllerAs: 'vm',
bindToController: true
However, WebStorm doesn't recognize it and gives this error:
I have just tested with both WebStorm and IntelliJ with the AngularJS plugin and in both cases the templates that include the ng-controller directive in their root element recognize the variable in the template and those without it do not. I would suggest adding it to your template to solve the problem.
<div ng-controller="commitmentController as cc">
<div class="margin-top-bottom">
<a ui-sref="commitmentList" ng-click="cc.sessionManager.okToContinueWithUnsavedChanges()"
role="button"><span class="glyphicon glyphicon-chevron-left">
</span>Back to All Commitments
</a>
</div>
<!-- Rest of template here -->
</div>
This is a bug in Webstorm. See #tokkov his answer for a possible workaround.
See the bug in the issue tracker here: https://youtrack.jetbrains.com/issue/WEB-20676

Play 2, how to reuse a HTML code with a tag

In Play! Framework v. 1.x there was such thing like a 'tag' where was possible to reuse some thml/template code.
In Play! Framework v 2.x, for me it is not clear still how it's going to be used (here).
For example, I want to use tag to define a header for my site (in order not to repeat myself, but just include the header every in the pages where I need it).
Could someone explain me / show how to use tags, or whatever I should use to include the header or any block of html/template code.
You showed us a sample and you are asking for sample :)
That's easy, create a common view in views.tags package (remember to leave first line empty if you're not gonna to pass any params! also remember to add brackets after tags name):
/app/views/tags/header.scala.html
<div id="header">
<h1>Hello World!</h1>
</div>
So you can 'include' it in any other view just with:
<body>
#tags.header()
Some other content
</body>

compose a page from different resources scala template In play! 2.0

If I have A main page which composed from more than one section (header,main,side,etc..),each Is loaded from different URL say header Is composed from /head side section Is loaded from /side we used to use Include in other frameworks.how can I do this In play! 2.0
I used Jquery to do It but can't this be done natively
$.get("/hello", function(result){
    $("#xxx").html(result);
As said by Marcus, this is detailed in official documentation.
In fact, you do not paste together results from different URL. You define in your template which templates have to be used.
You can include other templates using :
<h1>Home</h1>
<div id="side">
#common.sideBar()
</div>
But if you plan to have a global frame surrounding your pages, you should have a look to extended template :
#otherTemplate("Title"){
//Html content here ...
}
Update :
if what you are trying to do is retrieve an absolute URL from a controller, you have to use the reverse routing:
#routes.Application.hello("Bob").absoluteUrl()
<div id="side">
#common.sideBar()
</div>
BTW it's from official documentation.

KnockoutJs How can I get the `if` binding to work inside of a template block?

So the if binding of KnockoutJs is powerful, but I need to use it in a template block because I have to bind nested content E.G. <UL>'s.
<script id="my-template3" type="text/html">
<span data-bind="if:IsInherited">foobar</span><br />
</script>
This doesn't seem to render no matter what $data.IsInherited is set to. How can one perform an if databind in a tempate block using KnockoutJs?
As posted in the comments.
Are you using jquery.tmpl because if that is included I believe the control flow bindings will not work in script blocks.
Using native only will solve that
Cheers

Foundation 6 flex grid and predefined floats

I like the idea of flex-grid and was wondering if I'm using it correctly. I started a build using Foundation 6 / SASS and did #include foundation-flex-grid;
While building a basic page I noticed in my DevTools that certain items from the Kitchen sink are using styles on items that have their displays set to block and float on them.
Example: .title-bar
<div class="title-bar">
<div class="title-bar-left">
<button class="menu-icon" type="button"></button>
<span class="title-bar-title">Foundation</span>
</div>
<div class="title-bar-right">
<button class="menu-icon" type="button"></button>
</div>
</div>
The elements .title-bar-left and .title-bar-right have corresponding floats on them. I know that floats are unnecessary in flex-grid.
My question(s):
Should I keep building away using flex-grid or should I be doing
something extra to remove/replace those styles ahead of time?
It seems to me that the Kitchen Sink on the Foundation Site is
tailored for the normal grid, is it safe to use these elements in
flex-grid with little modification, or will it require lots of
rewriting of SASS/CSS?
For #2, 'Kitchen-sink' can pass true to turn on flex-grid like so:
#include foundation-everything(true);
They might have added this sometime in F6.1 or F6.2; official docs don't reflect this, but I put in a PR to.
For #1, might be more of a bug question than a best-usage issue, perhaps you should submit it?