-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
}
6
  • what is the difference between rendering html and javascript? Commented Apr 12, 2017 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 Commented Apr 12, 2017 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 Commented Apr 12, 2017 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? Commented Apr 12, 2017 at 18:30
  • Lashane, I assume you mean something like this: stackoverflow.com/questions/531311/…. 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. Commented Apr 12, 2017 at 21:14

1 Answer 1

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.