2010-05-19 53 views
1

我有这段代码。JavaScript中的dwoo模板变量?

{if $loginUrl} 
{literal} 
<script type="text/javascript"> 
    var newwindow; 
    var intId; 
    function login() { 
     var screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft, 
      screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop, 
      outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth, 
      outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22), 
      width = 500, 
      height = 270, 
      left  = parseInt(screenX + ((outerWidth - width)/2), 10), 
      top  = parseInt(screenY + ((outerHeight - height)/2.5), 10), 
      features = (
       'width=' + width + 
       ',height=' + height + 
       ',left=' + left + 
       ',top=' + top 
      ); 

     newwindow=window.open('{$loginUrl}','Login by facebook',features); 

     if (window.focus) {newwindow.focus()} 
     return false; 
    } 
</script> 
{/literal} 
{/if} 

这是dwoo模板,我不知道我怎么可以在JavaScript内使用我的dwoo变量?即时尝试只是在你可以看到的代码,但它不工作。我需要在{literal}之间转换我的代码,以便它可以工作。

+0

不知道dwoo什么,但假设这是一个服务器端事情,可能类似于'val somevar =“{$ somevar}”;' – Jonah 2010-05-19 04:16:50

回答

-1

你可以只做{/literal}{$the_varible}{literal}在变量标签内插入变量。

+0

没有错,但也不是完整的故事,也绝对不是理想的,所以我想让自己的答案碰到问题以来的人离开电网。我希望没有冒犯。 – Seldaek 2010-12-25 10:34:00

-2

嗯..发现了一个“修复”,但它是硬编码,并应该有更好的东西:

{if $loginUrl} 
{literal} 
<script type="text/javascript"> 
    var newwindow; 
    var intId; 
    function login() { 
     var screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft, 
      screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop, 
      outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth, 
      outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22), 
      width = 500, 
      height = 270, 
      left  = parseInt(screenX + ((outerWidth - width)/2), 10), 
      top  = parseInt(screenY + ((outerHeight - height)/2.5), 10), 
      features = (
       'width=' + width + 
       ',height=' + height + 
       ',left=' + left + 
       ',top=' + top 
      ); 

     newwindow=window.open({/literal}'{$loginUrl}'{literal},'Login by facebook',features); 

     if (window.focus) {newwindow.focus()} 
     return false; 
    } 
</script> 
{/literal} 
{/if} 

只是..难看。

4

删除{literal}标签。 Dwoo非常聪明,可以避免搞乱你的javascript,除非你在一行中使用对象文字。

确切地说,{后面跟着任何空格,制表符或换行符都不会被Dwoo解析。唯一的问题是,如果你做这样的事情:

{foo: "bar"} 

在这种情况下,你有几个选项,以防止Dwoo解析:

\{foo: "bar"} // escape the { 
{ foo: "bar"} // add a space so it doesn't match it 
{literal}{foo: "bar"}{/literal} // wrap with literal, but then you can't use vars inside again 

// or expand it to use multiple lines, which is usually more readable anyway 
{ 
    foo: "bar" 
}