[Solved] assigning a javascript value to Smarty?
[Solved] assigning a javascript value to Smarty?
Has anyone tried assigning a javascript variable's value to smarty using ajax or something similar?
For example, if I need to test the browser's viewport width using JS, can I somehow assign the result (a number) to a Smarty var using an ajaxy request from a _GET or something?
I realize that assigning a JS var directly to Smarty is not possible because JS is client-side. But I thought using ajax might be a work around - thoughts?
For example, if I need to test the browser's viewport width using JS, can I somehow assign the result (a number) to a Smarty var using an ajaxy request from a _GET or something?
I realize that assigning a JS var directly to Smarty is not possible because JS is client-side. But I thought using ajax might be a work around - thoughts?
Last edited by JohnnyB on Thu Feb 27, 2014 12:10 am, edited 1 time in total.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
Re: Has anyone tried assigning a javascript value to Smarty?
Via ajax would be easy but how would that be helpful?
Page is already loaded at least partially.
Page is already loaded at least partially.
Re: Has anyone tried assigning a javascript value to Smarty?
Well, I'm thinking about hiding something from the viewport for smaller screens, but not exclusive to mobile. Otherwise I would just test for the device type. So, if I can test the viewport width, then assign it to smarty, I can do something like,
{if $viewport > 1024} ... {/if}
I might not be able to reset the smarty var as the user resizes the viewport but I can at least get an initial value.
{if $viewport > 1024} ... {/if}
I might not be able to reset the smarty var as the user resizes the viewport but I can at least get an initial value.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
-
calguy1000
- Support Guru

- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
Re: Has anyone tried assigning a javascript value to Smarty?
typically this is done by using a session cookie.
a landing page sets the cookie via javascript, then you can read the cookie in PHP/smarty on subsequent pages.
If the cookie doesn't exist, redirect to the landing page.
a landing page sets the cookie via javascript, then you can read the cookie in PHP/smarty on subsequent pages.
If the cookie doesn't exist, redirect to the landing page.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Re: Has anyone tried assigning a javascript value to Smarty?
Perfect! I've done something similar before and forgot - thanks!calguy1000 wrote:typically this is done by using a session cookie
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
Re: [Solved] assigning a javascript value to Smarty?
Solved but not really solved.
Setting a cookie via JS and reading the cookie with Smarty works well. But, not when the user visits for the first time, on the first load because smarty can't read the cookie until after the template is parsed again (page reload). The value is empty until smarty parses again.
I could immediately force a header refresh is the Smarty var is empty so that it can read the cookie value set by JS when the page reloads. But that would be a bad user experience, I think.
So, I guess I'm back to seeing if there is a doable ajax method of posting the cookie value set by JS to PHP (maybe a UDT) so I can assign it to a smarty var. I have my doubts that it is possible.
Why can't we just have some kind of viewport detection with PHP!? ugh
Setting a cookie via JS and reading the cookie with Smarty works well. But, not when the user visits for the first time, on the first load because smarty can't read the cookie until after the template is parsed again (page reload). The value is empty until smarty parses again.
I could immediately force a header refresh is the Smarty var is empty so that it can read the cookie value set by JS when the page reloads. But that would be a bad user experience, I think.
So, I guess I'm back to seeing if there is a doable ajax method of posting the cookie value set by JS to PHP (maybe a UDT) so I can assign it to a smarty var. I have my doubts that it is possible.
Why can't we just have some kind of viewport detection with PHP!? ugh
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
-
calguy1000
- Support Guru

- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
Re: [Solved] assigning a javascript value to Smarty?
I did some googling... and there is no good way of doing this.
Because viewport size can change (users can resize windows), but for smaller screens (tablets, mobiles), you may want to have limited display so that you can improve performance and limit database queries etc).
the best method is probably some kind of UserAgent based detection for a default value, then a cookie mechanism to remember it.
i.e (pseudocode)
browser type = desktop
if( cookie does not exist )
if( user agent says browser is mobile ) {
/* cgextensions has this builtin */
set cookie to mobile
browser type = mobile
}
if( user agent says browser is tablet ) {
set cookie to tablet
browser type = tablet
}
}
else {
browser type = cookie value
}
Now you have a variable that has 3 states that you can use in your templates.
if your templates get overly complex because of this variable you may want to consider redirecting to a mobile friendly version of the page.
Because viewport size can change (users can resize windows), but for smaller screens (tablets, mobiles), you may want to have limited display so that you can improve performance and limit database queries etc).
the best method is probably some kind of UserAgent based detection for a default value, then a cookie mechanism to remember it.
i.e (pseudocode)
browser type = desktop
if( cookie does not exist )
if( user agent says browser is mobile ) {
/* cgextensions has this builtin */
set cookie to mobile
browser type = mobile
}
if( user agent says browser is tablet ) {
set cookie to tablet
browser type = tablet
}
}
else {
browser type = cookie value
}
Now you have a variable that has 3 states that you can use in your templates.
if your templates get overly complex because of this variable you may want to consider redirecting to a mobile friendly version of the page.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Re: [Solved] assigning a javascript value to Smarty?
Yep. I think I'll just have to be happy with device detection alone.
The main goal is not loading resources that won't be used because they are not small screen friendly. So, that will accomplish the goal, but since there are some mobile devices with a 1024px orientation, I was hoping to not exclude them. Ah well...
Thanks!
The main goal is not loading resources that won't be used because they are not small screen friendly. So, that will accomplish the goal, but since there are some mobile devices with a 1024px orientation, I was hoping to not exclude them. Ah well...
Thanks!
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
-
calguy1000
- Support Guru

- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
Re: [Solved] assigning a javascript value to Smarty?
Have you checked out deviceAtlas ? that's the service I use for CGSmartImage (that along with caching to minimze requests).
it returns a 'resolution' for anything that it identifies as a mobile device.... may fug up with funky browsers on android though (really haven't tested).
it returns a 'resolution' for anything that it identifies as a mobile device.... may fug up with funky browsers on android though (really haven't tested).
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Re: [Solved] assigning a javascript value to Smarty?
Heard of it and checked it before but never used it because the non-commercial version is a bit out dated now I think.calguy1000 wrote:Have you checked out deviceAtlas
It might be worth using if the thing I was trying to do was a dealer breaker.
hmm, although, I suppose I could do some device detection logic to allow some of the popular tablets using a 1024px orientation to see the full content. Just found, http://viewportsizes.com/
Thanks again!!
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
-
calguy1000
- Support Guru

- Posts: 8169
- Joined: Tue Oct 19, 2004 6:44 pm
Re: [Solved] assigning a javascript value to Smarty?
I dunno if deviceAtlas is out of date...
I just tested with my android phone running chrome, it successfully found my resolution at 800x480.
I just tested with my android phone running chrome, it successfully found my resolution at 800x480.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Re: [Solved] assigning a javascript value to Smarty?
You COULD choose to not include the optional parts of the webpage and use js and ajax to load them dynamically only if the viewport has the desired, minimum width.JohnnyB wrote: ...
So, I guess I'm back to seeing if there is a doable ajax method of posting the cookie value set by JS to PHP (maybe a UDT) so I can assign it to a smarty var. I have my doubts that it is possible.
Then you can even check for resizes of the viewport and dynamically hide/show(load) those parts.
Not sure if that's a reliable method though.
Re: [Solved] assigning a javascript value to Smarty?
Ah, right. That would be a better approach. Thanks!You COULD choose to not include the optional parts of the webpage and use js and ajax to load them dynamically only if the viewport has the desired, minimum width.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--

