JavaScript中的dwoo模板变量?

时间:2010-05-19 04:07:45

标签: php javascript templates variables 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}之间扭曲我的代码,以便它可以工作。

3 个答案:

答案 0 :(得分: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"
}

答案 1 :(得分:-1)

您可以{/literal}{$the_varible}{literal}在文字标记内插入变量。

答案 2 :(得分:-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}

只是......丑陋。