Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to enable Smarty variables inside JS files?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
toplisek
Smarty Regular


Joined: 17 Sep 2009
Posts: 48

PostPosted: Fri Feb 17, 2017 11:21 am    Post subject: How to enable Smarty variables inside JS files? Reply with quote

I understand using tpl files and variables inside such files.

Issue is how to manage variables also inside js file.
If I add JS file like the following line:

<script src="js/javascript.js"></script>

How to add also variables inside JS file. Is there good example to do this?
<script>
jQuery(document).ready(function()
{
MyfunctionJQuery.init();
}
);

In this case it will not work like tpl file // {/literal}{$variablesmng.myvariable1}{literal}, {/literal}{$variablesmng.myvariable2}{literal}),
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Feb 17, 2017 2:25 pm    Post subject: Reply with quote

What you must understand first is that the result of your code execution is a text.
When your page is rendered in a browser, your code is long since finished execution and all that remains is a stream of bytes.
If you want your variables to appear in JS code, you have to generate JS code and send it over to the client in hopes for the best.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Mon Feb 27, 2017 7:41 pm    Post subject: Reply with quote

I have seen this:
Code:
<script type="text/javascript" src="{$STORE_URL}/js/plugins.php"></script>

where plugins.php is code that will do whatever you need it to do, assign whatever values to template variables in a template, render the template, and return what should look like a valid javascript file.
Back to top
View user's profile Send private message
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Wed Mar 01, 2017 5:52 am    Post subject: same issue - variables for js Reply with quote

Hello,

I think i’ve got a question similiar to this here. I would like to integrate a Google Map into my Smarty website, which displays all entries from the database with a marker.

My solution works and I have solved it like this: By PDO the data is queried and then passed as JSON to Smarty. In the TPL, the data is passed as „var locations“ to the javascript, but not very ellegantly.

index.php:
Code:


$allCoords = $pdo -> getAllCoords();
$smarty->assign('allCoords', json_encode($allCoords));


map.tpl:
Code:

<script type="text/javascript">
   var locations = {$allCoords};
</script>


 
{fetch file="./js/googlemapmarker.js"}

<div id="map"></div>


googlemapmarker.js Corresponds to the example here: http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example

My question to you: Is there another way to assign the data for the markers to the JavaScript? Viewing source code of the website the complete JSON can be seen, which I find unpleasant.

Regards,
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Mar 01, 2017 6:13 am    Post subject: Reply with quote

That's how I would do it, and, as you say, the data can be seen in the page's HTML source.

I'm not sure you can get away from the data being seen by Smarty template assign, or by an ajax call to an API listener on your site, or whatever.

You could base64 encode it, then decode it when giving it to the locations variable. This way the data will never be seen in its raw form unless someone is willing to go the extra step.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Mar 01, 2017 1:21 pm    Post subject: Re: same issue - variables for js Reply with quote

robbo2000 wrote:

index.php:
Code:


$allCoords = $pdo -> getAllCoords();
$smarty->assign('allCoords', json_encode($allCoords));


map.tpl:
Code:

<script type="text/javascript">
   var locations = {$allCoords};
</script>



My question to you: Is there another way to assign the data for the markers to the JavaScript? Viewing source code of the website the complete JSON can be seen, which I find unpleasant.

There's no other way to do it.
For your PHP code, JS is just an output, no different than HTML or any other output.
Only when your script is finised and the data is sent to the browser, your output grow a different meaning.
Back to top
View user's profile Send private message
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Wed Mar 01, 2017 8:36 pm    Post subject: Reply with quote

OK - Thank you, especially for the quick replies. Perhaps I'll give ROT13 a try Wink
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Mar 02, 2017 12:35 am    Post subject: Reply with quote

You could always load the data dynamically via a separate call.
Which would require only "print json_encode();" and no additional support.
Back to top
View user's profile Send private message
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Thu Mar 02, 2017 7:55 am    Post subject: Reply with quote

Hello AnrDaemon,

thats new to me. Could you please give me some more Details or an example?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Mar 02, 2017 2:48 pm    Post subject: Reply with quote

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest for example.
Back to top
View user's profile Send private message
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Tue Mar 07, 2017 8:49 pm    Post subject: Reply with quote

Good Evening

thank you for your hint! I solved it this way, thought don’t know if this is very elegant.

I wrote a new file helper.php which i use for calling the pdo query:

Code:

<?php
require_once('pdo.php');
$meinpdo = new pdo();
$allCoords = $meinpdo -> getAllCoordsJSON();
echo $allCoords;
?>


And in my JS for generating the map i do a ajax-call on this file

Code:

<script type="text/javascript">
   
    $(document).ready(function() {
            $.ajax({
                type: "GET",
                url: './helper.php',
                async: true,
                dataType: "json",
                success: function(locations){

//… build map and do other fancy stuff….

}
});
});


If you’ve got any suggestions - please let me know!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP