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

Text Insertion in script Files
Goto page 1, 2  Next
 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Wed Jan 15, 2014 3:04 pm    Post subject: Text Insertion in script Files Reply with quote

Dear All,
If I insert at runtime a javascript file like follows:

<script type="text/javascript" src="../js/Colorinter.js"></script>

Will it be possible to insert with Smarty text into this file?
Or is it necessary to cancel the above HTML command and insert the contents of the javascript file directly like:

<script type="text/javascript">

:: the contents of the javascript file with the Smarty tags::

</script>
I tried both and I got only the second method working.
With regards
Siegfried
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Jan 15, 2014 9:31 pm    Post subject: Re: Text Insertion in script Files Reply with quote

Maybe I don't understand "Will it be possible to insert with Smarty text into this file?"

But, the first method doesn't "insert"any text. The browser requests the file ../js/Colorinter.js separately from the html.

Maybe the browser couldn't find it. Are you sure ../js/Colorinter.js is correct? Did your browser get a file not found error?
Back to top
View user's profile Send private message
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Thu Jan 16, 2014 2:40 am    Post subject: Text insertion in javascript Reply with quote

Dear douglassdavis,
Thank you for your reply.
The situation is as follows:
I had in former times created 3 .html files, one for English, one for Thai and one for German language. These files still work perfectly. Now I want to convert one of these html files to a Smarty .tpl file to insert the individual language at runtime. The old html files contain a javascript file that will be inserted at execution of the html file. As this works perfectly in the html files, there is no doubt that the spelling of the name, etc. is correctly done.
This javascript file includes a sequence that requires the insertion of a text in the specific language. The sequence looks at follows:
(function() {
var $, Lightbox, LightboxOptions;
$ = jQuery;
LightboxOptions = (function() {
function LightboxOptions() {
this.fileLoadingImage = '../images/loading.gif';
this.fileCloseImage = '../images/close.png';
this.resizeDuration = 700;
this.fadeDuration = 500;
this.labelImage = "Image ";
this.labelOf = " of ";
}
return LightboxOptions;
})();
The terms 'Image ' and ' of ' I need to insert in the selected language. If I do that after I inserted the about 300 lines of the javascript file (with {/literal} {literal} of course) directly into the header of the html file, the language insertion works perfectly. But if I insert only the html tag <script type="text/javascript" src="../js/Colorinter.js"></script> that loads the js file at runtime, no language is inserted.
This is my problem.
With regards
Siegfried
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Thu Jan 16, 2014 3:34 am    Post subject: Re: Text insertion in javascript Reply with quote

still sounds like what I said before. The browser can't find the js file.

The URL should be relative to the location of the html file requested by the browser (not relative to the tpl file).

If I can make a guess, perhaps it should be

<script type="text/javascript" src="js/Colorinter.js"></script>


Open your debugger (get firebug if you don't have a debugger) and make sure that the browser is loading the javascript file.
Back to top
View user's profile Send private message
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Thu Jan 16, 2014 6:21 am    Post subject: Reply with quote

Hello douglassdavis,
At the runtime of the .tpl file the browser obviously has loaded the javascript file in which I want to insert language-dependent text. The image is displayed after clicking on the corresponding thumbnail. The display of the image is achieved by the javascript in question. If I block the insertion call for the javascript, no image will be shown enlarged after clicking on a thumbnail.
Also, the footer line of the picture is shown as follows:

Die ersten drei Schwimmer haben unser Tretboot erreicht
{/literal}{$text.gallery7} 14 {$text.gallery8}{literal} 91

Smarty inserts the German text correctly elsewhere in the .tpl document, but the part of text that should be inserted by Smarty in the javascript shows still the original Smarty tags which are not compiled.
As I interpret that is that the javascript file will not be loaded at the time of the compilation of the .tpl file but only at the runtime of the .tpl file at the browser. At that time there are only the not compiled Smarty tags and the required language text cannot be inserted.
With regards
Siegfried
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jan 16, 2014 3:19 pm    Post subject: Reply with quote

If a browser loads a text file directly:

<script type="text/javascript" src="../js/Colorinter.js"></script>

And it is sent directly to the browser, there is no way to get Smarty tags compiled. Think of it this way: Is your file (Colorinter.js) generated by PHP? Same problem. What you might do, something like:

<script type="text/javascript" src="foo.php?page=Colorinter.js"></script>

And foo.php would instantiate smarty and send back a page generated by a Smarty template.
Back to top
View user's profile Send private message Visit poster's website
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Fri Jan 17, 2014 12:48 am    Post subject: Insertion of text in javascript files Reply with quote

Dear mohrt,
I tried the term as you suggested:
<script type="text/javascript" src="foo.php?page=Colorinter.js"></script>
and also:
<script type="text/javascript" src="foo.php?page=../jsn/Colorinter.js"></script>
Both do not wok. At runtime in both cases the .js file is not available. The images are not shown in enlarged form as it should be if the .js file is loaded correctly.
If I use the term
<script type="text/javascript" src="../jsn/Colorinter.js"></script>
with the same .js file the function works, but as we know no text will be inserted because the .js file is not accessible for Smarty at compilation time.
With regards
Siegfried
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jan 17, 2014 3:36 am    Post subject: Reply with quote

Of course it won't just work, you have to write php code to load a template with the javscript source in it. if you did that, post your source to foo.php.
Back to top
View user's profile Send private message Visit poster's website
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Fri Jan 17, 2014 7:46 am    Post subject: Smarty insertion in embedded javascript Reply with quote

Dear mohrt,
Many thanks for your support.
What do you think? is it in such a case not easier to paste the javascript code completely into the .tpl file and put the appropriate Smarty tags in?
I did that already and it worked. The disadvantage is only that I have then about 300 additional lines inside my .tpl file making the file unclear (I do not want to say confusing, because I'm not confused by that).
Your suggestion means that I should prepare a Colotinter.tpl file from my javascript and than I should extend my already existing ms-gallery.tpl file with a php include instruction (something like <?php include PATH.'Colorinter.tpl'; ?> so that the javascript will be included and compiled.
But how do I post it into foo.php to insert the file later on at the runtime of the ms-gallery.tpl file?
With regards
Siegfried
Back to top
View user's profile Send private message
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Fri Jan 17, 2014 7:47 am    Post subject: Smarty insertion in embedded javascript Reply with quote

Dear mohrt,
Many thanks for your support.
What do you think? is it in such a case not easier to paste the javascript code completely into the .tpl file and put the appropriate Smarty tags in?
I did that already and it worked. The disadvantage is only that I have then about 300 additional lines inside my .tpl file making the file unclear (I do not want to say confusing, because I'm not confused by that).
Your suggestion means that I should prepare a Colotinter.tpl file from my javascript and than I should extend my already existing ms-gallery.tpl file with a php include instruction (something like <?php include PATH.'Colorinter.tpl'; ?> so that the javascript will be included and compiled.
But how do I post the compiled javascript file into foo.php to insert the file later on at the runtime of the ms-gallery.tpl file?
With regards
Siegfried
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jan 17, 2014 3:21 pm    Post subject: Reply with quote

So if you do this in the browser:

Code:
<script type="text/javascript" src="../jsn/Colorinter.js"></script>


It gets the javascript file directly (no PHP/Smarty involved in the connection)

If you something like this:

Code:
<script type="text/javascript" src="getJavascript.php?file=../jsn/Colorinter.js"></script>


And getJavascript.php looks something like:

Code:
$smarty = new Smarty();
// do assigns here, sanitize $_GET data
$smarty->fetch($_GET['file']);


Now the javascript is run through the Smarty interpreter, and any tags within will get compiled. Make sense? You could also do apache rewrite hacks to make direct access to .js files run through the php/smarty interpreter to keep urls clean.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jan 17, 2014 3:23 pm    Post subject: Reply with quote

another thought, instead of pasting the javascript into your .tpl file, keep it in a separaate file and include it:

{include file="Colorinter.tpl"}
Back to top
View user's profile Send private message Visit poster's website
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Fri Jan 17, 2014 4:40 pm    Post subject: Smarty and embedded javascript Reply with quote

Dear mohrt,
I get both solutions not working.
The first solution I tried more than an hour in different ways. No success.
For the second solution I put a Colorinter.tpl with the appropriate Smarty tags into the templates directory.
Afterwards I inserted the term {include file="Colorinter.tpl"} with and without PATH one time in the Smarty initiation file after $smarty = new Smarty; and afterwards after deleting in the initiaton file into the original ms-gallery.tpl file. Both did not work. I got in the browser an empty page only.
I think, I surrender. My PHP knowledge is obviously too less although I programmed already a hit counter for my URL and different evaluation programs showing me from where in the world the calls to my web pages are coming from and it works perfectly, but with Smarty I seem to be at my present limits and first I must learn more.
Thanks and regards
Siegfried
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jan 17, 2014 6:08 pm    Post subject: Reply with quote

You probably need more than just

$smarty = new Smarty;

You need to setup your smarty paths. see the install docs.
Back to top
View user's profile Send private message Visit poster's website
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Sat Jan 18, 2014 1:04 am    Post subject: Include javascript template into Smarty template Reply with quote

Dear mohrt,
Thank you for your additional support.
I found in the Smarty manual the following hint for including a .tpl file into another .tpl file:
{* windows absolute filepath (MUST use "file:" prefix) *}
{include file='file:C:/www/pub/templates/header.tpl'}
So I inserted the tag:
{include file='file:/home/www/virtual/moon-and-sun.com/htdocs/booking/makewp/templates/lightboxS.tpl'}
Even if I cancel 'file:' the nresult is the same:
At runtime the html page will be loaded correctly, but no enlarged images will be shown. That means, the javascript file is not included. Instead my include tag will be displayed at the top of the html page of the internet explorer:
{include file='file:/home/www/virtual/moon-and-sun.com/htdocs/booking/makewp/templates/lightboxS.tpl'}
I interpret that so that Smarty does not execute the tag and therefore it is treated from the explorer like text which has to be displayed.
With regards
Siegfried
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 -> Smarty Development All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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