-1

I have a large amount of HTML and CSS which contains some PHP (session based content) this PHP is a must. I need that session information (no cookies wont do).

The html and CSS are standard divs I am looking at this previous question: Is there a best practice for generating html with javascript

which gives me the answer I need, if I was using just HTML and CSS, but what about if I need to use JS if statements to chose what part of the template needs to be different and what if I need to use PHP to do the same?

I am moving my code away from heavy server side scripting and moving as much as I can to front end processing, but the issue is I need to have some PHP and if statements (js if statements) within the $.template

can I use PHP variables in a JS templating system and how do I use JS IF statements within the templating function?

var moo = 1
var T = $.template('<div>This is code, but what is moo?. if(moo == 1){moo was 1..}else{moo was not 1}</div>')
Community
  • 1
  • 1
qwertyuiop
  • 27
  • 3
  • It is unclear what you are asking. You have not provided enough information for anyone to really do anything other than make guesses. Please add more detail to your question – RiggsFolly Feb 08 '16 at 10:49
  • @RiggsFolly hopefully that helps clarify. – qwertyuiop Feb 08 '16 at 10:57
  • 1
    Why not `var moo = 1 ? "moo was 1" : "moo was not 1";`? Then you can use `moo` with the right value. Also, you can't use PHP inside a JS file, you have to use a PHP file (which can includes JS). – kosmos Feb 08 '16 at 11:37
  • @kosmos sorry i was trying to say that all the if statments will be javascript. my php is currently working fine... though I am haveing major issues with inserting large amounts of html/CSS into a parent element. – qwertyuiop Feb 08 '16 at 12:27

1 Answers1

1

As I commented, you can declare and check your variables before using it.

Example:

var moo = 2 + 2 == 4 ? "yes moo" : "no moo";
var T = $.template('<div>This is code, but what is moo?. Moo is '+ moo +'</div>');

But if you are working with large data, you should consider to use a string variable and concatenate the new strings as needed:

var str = "<div>";
if( moo == 4 ) str += "moo equals 4";
else str += "moo NOT equals 4";
str += "</div>";

// And when you have your string completed...
var T = $.template(str);

By this way you can concatenate all you need without headaches.

kosmos
  • 4,253
  • 1
  • 18
  • 36
  • yes this is pretty much what I was thinking... on addition to my question though... is there anyway to use JS if statements inside HTML code? if(moo = 1): htmlcodehere; difhtmlcode; that are not in JS functions of course... (just an addition to my question) – qwertyuiop Feb 08 '16 at 13:31
  • Yes, you can do it using `document.write()` but only while the document are loading. Once loaded, its use will clear the document. ([jsfiddle example](https://jsfiddle.net/kmsdev/L50ff0cg/)). Check MDN docs to complete the information: https://developer.mozilla.org/en/docs/Web/API/document/write – kosmos Feb 08 '16 at 20:01