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.
Related
Been playing around some with iCanHaz.js templates which i define in the head of my html pages like this.
<script id="test" type="text/html">
<h1>Test</h1>
</script>
I do want to have all my iCanHaz templates in a single file and i dont know how i should import them.
Thanks in advance!
They provide an example for how to pull templates from a server on their site, almost at the bottom, if you use jQuery as well:
$.getJSON('/myserver/templates.json', function (templates) {
$.each(templates, function (template) {
ich.addTemplate(template.name, template.template);
});
});
Then of course you would have to have some server-side code that provide the templates as JSON, and how you do that depends on what server-side language you are using, as pointed out by Poelinca Dorin in his comment.
Like this? (it worked for jsRender templates):
<!--#include virtual="Scripts/RenderTemplates.js" -->
I want to do something like this:
$("#startButton").click(function() {
<?php if(somecondition){ ?>
<script src="some-external-js-srouce"></script>
<noscript><img src="irrelevant"></noscript>
<?php } ?>
});
Meaning that I want to execute some external javascript inside a jquery function if a button has been clicked. Can you help me?
You're putting HTML inside JavaScript. That's not quite possible.
You probably want $.getScript to load and execute a script dynamically at runtime. It does not support <noscript> but you're using JavaScript for it anyway, so that does not really make sense to me...
if you want to execute a javascript function from an external file then you can try like this -
<script type="text/javascript" src="myjs.js">
// code that will refer functions of myjs.js
</script>
use jQuery.getScript( url, function(){});
http://api.jquery.com/jQuery.getScript/
I am trying to write my javascript functions in a separate file functions.js. Right now that file consists of one function:
function noOverlay() {
$('.overlay').css('display','none');
};
In my html I have these two lines:
<script type="text/javascript" src="/functions.js"></script>
<script>
$(document).ready(
noOverlay()
);
</script>
Essentially what I am trying to do is house a function in an external file and call it in my html. Maybe $document.ready isn't the right way to do this. Or maybe I am just making a silly mistake. Thanks!
Make sure you are importing jQuery and try:
<script type="text/javascript" src="/functions.js"></script>
<script>
$(function () {
noOverlay();
});
</script>
$(document).ready(function(){
noOverlay();
});
Check to make sure the js file was loaded with firebug or chrome's dev tools
Also make sure you load jQuery beforehand as well.
I'm using a javascript template like this:
<script id="template" type='text/tmpl'>
<img src="{{=imagesrc}}" />
</script>
The problem is that loading that template the page would try to make a request to http://site.com/{{=imagesrc}}.
My understanding was that using the script element type should prevent what's inside from being interpreted as html?
How do I fix this? Thanks.
Not sure what you're really trying to do.. but you could try wrapping whatever is inside your script tag with CDATA:
<script>
/*<![CDATA[*/
Your stuff here
/*]]>*/
</script>
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.