【问题标题】:dwoo template variables inside JavaScript?JavaScript中的dwoo模板变量?
【发布时间】:2011-02-21 05:04:12
【问题描述】:

我有这个代码。

{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} 之间扭曲我的代码,以便它可以工作。

【问题讨论】:

  • 对dwoo一无所知,但假设它是服务器端的东西,可能类似于val somevar = "{$somevar}";

标签: php javascript templates variables dwoo


【解决方案1】:

删除 {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"
}

【讨论】:

    【解决方案2】:

    您可以通过 {/literal}{$the_varible}{literal} 在文字标签中插入变量。

    【讨论】:

    • 没有错,但也不是完整的故事,而且绝对不理想,所以我想把我的答案推到最前面,因为问的人不在网格上。我希望没有冒犯。
    【解决方案3】:

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

        {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}
    

    只是……丑陋。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-02
      • 2021-06-14
      • 2016-04-22
      • 2021-06-19
      相关资源
      最近更新 更多