0

is there a way I can set a variable/ do something (anything??) in javascript jquery, and detect it with template toolkit?

I want to use it as a way to change the content based on if the browser is running javascript.

My code would flow something like:

<scipt type="text/javascript">
variable_name = 1;
</script>

[% IF variable_name == 1 %]
do stuff here, js turned on
[% ELSE %]
//do stuff here, js turned off
[% END %]

when I say anything, I mean is there anything I can do in the JS that template toolkit can pick up? like show/hide a div with JS and detect it with template toolkit?

rpsep2
  • 3,061
  • 10
  • 39
  • 52
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin Mar 21 '14 at 16:49
  • Short answer: No. Follow the principles of [Progressive Enhancement](http://en.wikipedia.org/wiki/Progressive_enhancement) and [Unobtrusive JavaScript](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript) instead. – Quentin Mar 21 '14 at 16:50

1 Answers1

0

It would be unwise to even try to develop an application in that direction.

Since variable_name is a variable, as a programmer, you should expect it to vary, you should expect that it'll change. (And if you don't, use a constant.)

Since the Template Toolkit runs FIRST on the server, and the Javascript runs SECOND on the client, if that variable changes to 0 in the JS, you won't have the content available in the ELSE block of the TT code (which is probably what you're expecting).

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133