Developer forum

Forum » Development » Parsing a JS file through the template engine

Parsing a JS file through the template engine

Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

I have a weird idea. It's common to have some hardcoded string. Could there be a way for us to parse the JS content through DW's template engine and generate the translate tags?

 

We'd probably once need to run this on demand. It would be inneficcient to do it per every request, but if there was a way, would be interesting to do it.

 

Best Regards,

Nuno Aguiar


Replies

 
Martin Vang Dynamicweb Employee
Martin Vang
Reply

Hi Nuno,

Just out of curiosity, could we instead create a js-language-framework for translations, that can be imported where you need it? I think that would be easier than creating a cached JIT parser for the javascript files, and I think it would solve your needs in the same way.

Note: I cannot say if this is going to be a priority. I just wanted to discuss the best solution. :)

BR

Martin

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Martin,

 

That would be a great option, specially considering the new JS trends.

 

In the meanwhile Imar and I came up with a few ideas on how to do this, which we will run with for now:

  • We have a "module" called Template Renderer that just renders a template
    Serves multiple purposes, but in this case act as middleman.
  • We already have Layout templates to return JSON or HTML or something else
  • We add a paragraph (with a clean template) and the Template Renderer which simply returns javascript variables with the @Translate tag
  • We request that page as if it was a JS file
    DW should be able to handle the caching just fine for that "page"
    Our tool handles the minification

 

We'll take this for a spin, but in theory it works just fine.

 

Best Regards,

Nuno Aguiar

 
Lars Larsen
Lars Larsen
Reply

Hi

Was this idea ever implemented?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

You can just do it.

Create a template called "javascript.cshtml":

<script>
var something = '@Translate("add_to_cart")'
</script>

Load it into the renderer and get the output:

var t = new Template("javascript.cshtml");
var parsedScript = t.Output();

 

You must be logged in to post in the forum