smarty assign to javascript external - javascript

here in index.tpl
here in javas.js
var currentTS = "{literal}{$userid}{/literal}";
alert(currentTS);
but there will be alert {literal}{$userid}{/literal} not the $userid.
any idea?

Smarty only works under php, you can't run it in .js , unless you add .js to php extensions in apache configruations.
On top of that it seems to me that you are trying to access the {$userid} variable from your index.php. That is never gonna happen! unless you include the file server side like karvonen explained.
And your {literal} tags are unnecessary you start literal when you are gonna use { and } that are not smarty tags but for javascript, css, etc..
and the only time you see them around smarty tags is the other way around as karvonen explained
here's my suggestion: in your index.tpl right before including the java.js file do this:
<!--index.tpl-->
<script type='text/javascript'>UserID = '{$userid}';</script>
<script type='text/javascript' src='pathto/java.js'></script>
/*java.js*/
var currentTS = UserID;
alert(currentTS);

Include the javascript file in your index.tpl. If you have it outside your template directory you must use the file:/... notation (and use your own path, of cours):
<html>
<head
<script type='text/javascript'>
{include file='file:/home/www/mydomain/public_html/js/javas.js'}
</script>
if you have it in your template diretory simply:
<html>
<head
<script type='text/javascriptä>
{include file='javas.js'}
</script>
Now Smarty should parse and compile it.
Moreover, it seems to me that you {literal}{/literal} are the wrong way around. If you are using curly braces in your js file you should start the js with a {literal} tag and "unliteralize" the smarty variables:
{literal}
function test() {
var name = '{/literal}{$name}{literal}';
// do something
}
{/literal}

Don't use {literal}
You don't need it here.
{literal} forces to display all { as they are and don't parse smarty code. Therefore {$userid} will be displayed, as it is.
There is no point in displaying it at the place you are.

Related

Move javascript files into inline html

I have an application that allows the use of inline javascript, but not javascript from source files. I'm trying to modify a webpage to open on this browser, and need to know how to put the javascript files from the webpage inline.
Use <script> tags in your HTML. They can go anywhere - I prefer inside the <head> tag:
<head>
<script type="text/javascript">
// put anything here - any type of valid javascript works
// if you import jquery, you can use jquery here too!
</script>
</head>
You should place your imported code into the script tag in your HTML page (application in your case), look at the following example:
<html>
<head>
<script type="text/javascript">
//Import your JS script here, e.g.:
function doSomething(){
..
}
</script>
</head>
<body>...</body>
</html>

How to put "</script>" in a javascript string?

I've been trying some tricks in javascript and came to a ridiculous problem: I can't use <script> as a substring in a javascript string! Here is an example:
<html>
<head>
<script>
alert("<script></script>");
</script>
</head>
</html>
It supposed to print out <script></script>, but instead, I get this:
");
Printed out on the page, as HTML.
Question: How can I use <script> followed by </script> substrings in Javascript, and why is it acting that way?
Here is JSFiddle of it.
What's tripping you up is the </script>. The HTML parser doesn't recognize Javascript strings or nested <script> tags, so it's interpreting that as the closing tag for the initial <script>. That is, this part of the document is parsed as:
<script> (open tag)
alert("<script> (text node - contents of the script)
</script> (close tag)
"); (text node - plain text)
The second </script> is ignored, as there's no other <script> tag for it to close.
To work around this, break up </script so that the HTML parser doesn't see it. For instance:
alert("<script><\/script>");
or:
alert("<script><" + "/script>");
or just put the code in an external Javascript file. This issue only arises for inline scripts.
it is because of the \ I believe. i have no concrete explanation since I am a newbie to Javascript but this code should work:
alert("<script><\/script>");
came up with it using Java knowledge.. Haha since the \ is an escape key in many languages.
Alert(\<script>\</script>\)

can't embed some text in js file

My problem is, that I have created a js file. In this file are some text defined.
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
}
Now I want to embed categorie_one in a other js file. That I'm doing with that code:
channel.categorie_one;
But it shows in console: Cannot read property 'categorie_one' of undefined, logical I have linked the file...
Im including the js files in the index.html
<script src="javascripts/default.js" type="text/javascript" rel="javascript"></script>
<script src="javascripts/resources.default.js" type="text/javascript"></script>
In the default.js I have a methode to load the site.
function ChannelLoad(listview) {
//there should be cateogrie_one
}
could you help me pls. Thanks in advance
add a semicolon to your variable definition:
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
};
syntactically your variable definition is a statement; if placed in a row with other statements (i assume that's where it will end up after inclusion of your js files), they have to be separated by semicolons.
In your html file include the scripts in the right order:
<script type="text/javascript" src="defines_object.js"></script>
<script type="text/javascript" src="uses_property.js"></script>
Make sure the object isn't defined in a function or the scope of the object will be limited to that function.
If you defined the channel variable in the resources.default.js file and you are trying to access it in the default.js file, you have to reverse the order in which you link the JS files.
Try:
<script src="javascripts/resources.default.js" type="text/javascript"></script>
<script src="javascripts/default.js" type="text/javascript" rel="javascript"></script>
This code should work.
The only thing you must be aware of is the order you include the files, all files will be executed in the order of their includes, so you should include first the file declaring the object, containing
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
}
then include the second file, using channel.categorie_one;
If this still doesn't work, please post your whole code, there's probably a scope issue (channel is declared in a local scope)
You should include your scripts in the right order:
<script type="text/javascript" src="defines_object.js"></script>
<script type="text/javascript" src="uses_property.js"></script>

How to correctly include javascript code in chameleon/zpt template (pyramid)?

I'm trying to embed some code between <script> </script> tags, pyramid however doesn't like it and gives me
ExpatError: not well-formed (invalid token)
Probably because i have && in my code. I tried using & instead, but then it didn't get interpreted in the browser.
The same thing happens when i try to put it in CDATA block.
When I move the code to a separate js file it works. I'd like to keep it in the same file for now, just to enable quick corrections.
So, how should I do it?
EDIT:
I get the same error even for templates as simple as this one:
<html
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal">
<head>
</head>
<body>
<span onclick="alert(true && false);">test</span>
</body>
</html>
I think you're supposed to put && (i.e. two times the HTML entity code).
This should work:
<script type="text/javascript">
//<![CDATA[
// my javascript
//]]>
</script>
Have you tried adding a type attribute to your script tag?:
<script type="text/javascript">
...
</script>
It looks like xhtml issue, as w3c validator reported the same error.
I was thinking if there's a switch to change the document type parsed by chameleon to html, but then it wouldn't be possible to include tal and metal namespaces.
Hence it is expected behavior

Javascript templates

Does anyone know if is there any way to create javascrit templates in Genshi? I mean, I need a .js file where I can use directives like <py:for> and so.
Any idea? Thanks!
You can write it directly in html like this:
<script type="text/javascript">
<py:if test="func1">
/*<![CDATA[*/
function func1() {
}
/*]]>*/
</py:if>
</script>
note: code inside CDATA will be right escaped, so you can use '>','<' as usual.

Categories