thanos

thanos

Environment

Erlang/OTP 21 [erts-10.2.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]
IEx 1.8.1 (compiled with Erlang/OTP 21)
{:phoenix, "~> 1.4.2"},
{:phoenix_html, "~> 2.11"},

The Problem

I’m trying to use Vue templates in a Phoenix template.
Given this

 <template>
     <a href="job/{{ job.id }}">{{ job.name }}</a></div>
</template>

So instead of

/job/1234

We get this

/job/%7B%7B%20job.id%20%7D%7D

Trying

<a href='<%= raw "{{ job.id }}" %>'>{{ job.name }}</a>

and another other ideas from Phoenix.Template.HTML does not solve the problem.

In other systems such Django I can do things like

{% verbatim %}  
<a href="{{ job.id }}">{{ job.name }}</a>
{% endverbatim %}

I understand Phoenix template is a different beast but there must be a way…

Showing Posts 1 to 10

kokolegorille

kokolegorille

In Phoenix template, that should be

"job/#{ job.id }"

But I have never used Vue.

peerreynders

peerreynders

Aside:

The template tag is part of Vue’s SFC spec and is expected to be in a .vue file which is an asset intended to be compiled before deployment by the front end build system (the Vue compiler turns it into pure JavaScript).

Any template tag rendered by EEx and served by Phoenix is going to be interpreted by the browser as a native content template element which is an entirely different beast.

Even if Vue can use the template content client side - you should aim to avoid any approach that requires that you use the Full build:

  • The full build includes the template compiler - i.e. the client needs to download more JavaScript delaying FCP and TTI.
  • More importantly that compiler has to run client-side to transform all the Vue templates in the page to Vue render (JavaScript) functions before the resulting DOM can be rendered - delaying FCP and TTI even more.

thanos

thanos OP

That might be so but my issue is neither how I’m using Vue nor my FCP and TTI requirements but something more simple: How can I emulate

{% verbatim %}  
some javascript
{% endverbatim %}

I just used the above as an example.

Anyways thanks @peerreynders for your answer.

thanos

thanos OP

thanks @Kokolegorille, using an elixir comment - nice !

peerreynders

peerreynders

string interpolation actually.

String.Chars protocol.

thanos

thanos OP

Quite right thanks for the correction but out of desperation I also tried to use a comment to avoid evaluation but without success.

peerreynders

peerreynders

One of the issues is that in fact you aren’t trying this at all:

That case is covered by Phoenix.HTML.raw/1.

This makes it sound like you want to supply job.id from Elixir on the server side - but the mustache syntax suggests that job.id (and job.name) are supposed to be handled client-side by Vue.

Which begs the question - is job.id (and job.name) a server side Elixir value (to be handled by EEx/Phoenix) or a client-side JavaScript value (to be handled by Vue/browser)?


In a vanilla Phoenix app in index.html.eex I inserted both:

  </article>
  <a href="job/{{ job.id }}">{{ job.name }}</a>
</section>

and

  </article>
  <template>
    <a href="job/{{ job.id }}">{{ job.name }}</a>
  </template>
</section>

and they rendered in the browser as expected verbatim - there is no collision.


Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> import Phoenix.HTML
Phoenix.HTML
iex(2)> job =  %{id: 1234, name: "research"}
%{id: 1234, name: "research"}
iex(3)> content = raw("{{ job.id }}")
{:safe, "{{ job.id }}"}
iex(4)> ~E(<a href="job/<%= job.id %>"><%= job.name %></a>) |> safe_to_string() |> IO.puts()
<a href="job/1234">research</a>
:ok
iex(5)> ~E(<a href="job/{{ job.id }}">{{ job.name }}</a>) |> safe_to_string() |> IO.puts()
<a href="job/{{ job.id }}">{{ job.name }}</a>
:ok
iex(6)> ~E(<a href="job/<%= content %>">{{ job.name }}</a>) |> safe_to_string() |> IO.puts()
<a href="job/{{ job.id }}">{{ job.name }}</a>
:ok
iex(7)>

Possibly the missing piece of the puzzle? Django template variables use the mustache syntax.

The EEx template tags are documented here.

If on the other hand you are trying to bind a client side value then maybe you need this:

<a v-bind:href="`job/${job.id}`">{{ job.name }}</a>
jeremyjh

jeremyjh

Phoenix templates use eex by default. Interpolation is done like this:

<a href="<%= @job.id %>"><%= @job.name %></a>

This assumes you passed an assign called job to the template ala render(conn, :show, job: job)

thanos

thanos OP

@peerreynders I’ve gone through the same process and arrived at the same point as you but I was offline for a while and didn’t see your subsequent post.
Many thanks for your help.

Thanks to this problem and everybody’s help I have a much better understanding of the Phoenix.Template.EExEngine which very different (and cool ) from others I’ve used.

Many thanks to all.

peerreynders

peerreynders

One reason why I brought up those issues is that Vue.js is typically used to build an SPA.

In that case it is common to serve the SPA completely decoupled from Phoenix - which also leaves the option open to use Nuxt.js for SSR to pre-render Vue.js to improve client-side performance. Phoenix then acts simply as a JSON or GraqhQL server (with Absinthe) for the SPA.

Going down the Vue.js SPA road there really isn’t any use for EEx templating. EEx is more useful for building dynamic web pages. Now it is conceivable to use dynamic pages that include a component that uses Vue.js but that use case doesn’t seem to be all that common.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews