Matrix
Matrix

Reputation: 3369

Execute javascript template to produce HTML by ruby server?

I have JST template (tpl javascript to create HTML). It's normaly used by the client (in JS). But I want my sever ruby be abble to produce himself the views. I don't want remake the JST template, converted to ruby because:

1) twice the same code in 2 languages => more maintain/debug 2) twice more work, for same result is waste my time

You will tell me: "just make a ruby template", but I don't want to. I want client auto-generate his HTML. But, for referencement, I need sometimes the ruby server rendering directly HTML and not just JSON, so my question is:

how can I execute JS template with ruby server? Others solutions?

Upvotes: 0

Views: 117

Answers (1)

WebServer
WebServer

Reputation: 1396

There are multiple ways to tackle this problem:-

1)Use Node Server -Common JS Templates in client and server- HTML rendering as a service from JS template

In your server you can call node service with template name and parameters like to render datepicker you can call http:\localhost/datepicker/date/month/year, since your templates are in JS , Node server will also be able to render those.See dust.js ,it works almost on similar lines.

2)Just maintain 1 server side templates with variable name prefilled

Lets say you template is

<div>My name is ${name}</div>

With this template you can render on server side by providing correct name as "abc" or "def", etc.

When ever client needs template you can send the same template with name parameter as "$$name", and in client js you can replace $$name to the correct name.

Thus maintaining same template on server as well as client.

3)Use some solution similar to google closure

If you were asking the same thing in JSP this was straight forward solution, but you can look for similar thing in ruby world.

Upvotes: 1

Related Questions