1

私はこのコードを持っています。

{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} の間でワープする必要があります。

4

3 に答える 3

4

{literal}タグを削除します。Dwooは、1行でオブジェクトリテラルを使用しない限り、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"
}
于 2010-05-19T08:34:21.590 に答える
-1

{/literal}{$the_varible}{literal}リテラルタグ内に変数を挿入するだけです。

于 2010-05-19T04:14:08.043 に答える
-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}

ただ.. 醜い。

于 2010-05-19T04:20:57.733 に答える