-3

My use case is that I have portions of js code that I would like to render based on some user permissions, with server side language being PHP.

Now I am currently using the Fat Free Framework, but even in other frameworks like laravel, I have only been able to find templates that template HTML.

I know I can use the templating engines that exist to just have conditional template logic to print a <script> tag given certain user permissions, but I haven't found any examples of how to do the following in a .js file

Example:

PHP:

$visible = ['section1' => true, 'section2' => true];

//this just renders the js file
render('test.js', ['visible' => $visible]);

test.js:

`//this is some kind of templating format`
{{if(isset($visible['section1']))}}
    var a = 'Gin';

    function sectionA(){
        //do something
    }
{{endif}}

{{if(isset($visible['section3']))}}
    var b = 'Rum';

    function sectionB(){
        //do something
    }
{{endif}}

The result would be that test.js would be rendered as

var a = 'Gin';

function sectionA(){
    //do something
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Mr.Meeseeks
  • 21
  • 1
  • 4
  • what is the difference between rendering html and javascript? – Iłya Bursov Apr 12 '17 at 15:49
  • well if I'm understanding the question correctly, i just echo the various scripts with a src of the js file location, then the client requests them. In Fat Free, template files are .htm files, basically a templatable .html file. then when we go to pull down the js file, the whole js file is loaded and I have no way of templating that before it comes back down – Mr.Meeseeks Apr 12 '17 at 15:58
  • js file location can be php file, which renders template, the same way as it works with html, just output correct http headers from php to indicate that output is javascript, not text/html – Iłya Bursov Apr 12 '17 at 16:42
  • Generating a JavaScript in this way will make it difficult to maintain and debug. What problem are you trying to solve by doing this? – Marcos Apr 12 '17 at 18:30
  • Lashane, I assume you mean something like this: http://stackoverflow.com/questions/531311/how-can-i-generate-dynamic-javascript. I'll have to look into that a little more, that looks like it might work for my purposes, although not the templating that i originally envisioned. – Mr.Meeseeks Apr 12 '17 at 21:14
  • Also to Marcos, i have a webpage that requires components to be loaded based on user permissions. I can use templating on the html (to determine which html is rendered), but don't have a good way of templating which js functions are loaded, aside from separating out the js functionality and loading them as needed (via the html templating, where i would include scripts to be loaded along with the html associated with that bit of js) – Mr.Meeseeks Apr 12 '17 at 21:15

1 Answers1

0

Alright, so after some playing around, I've figured out a solution. when building the html, you can echo a script that has a src of whatever resource you want. on the server side, respond to the javascript src route by rendering a js file (using the templating engine) that has the templating markups in it, and things will work as I wanted them to :)

Mr.Meeseeks
  • 21
  • 1
  • 4