onload="init();" 的 TemplateSyntaxError

TemplateSyntaxError with onload="init();"

我找到了一个 example how to copy text to clipboard with ZeroClipboard. However, I tried to convert the original code 烧瓶,我遇到了 TemplateSyntaxError: expected token 'end of statement block', got 'onload' 因为 {% block body onload="init();"%}。如何将 onload="init();" 与 Jinja 一起使用?

这里是app.py

@app.route("/table/", methods=["GET"])
def table():
    return render_template('table.html')

这里是base2.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>flask-bootstrap example</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='bootstrap/css/bootstrap.css')}}" >
    <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.min.css">
    <link rel="stylesheet" type="text/css" href="https://gitcdn.github.io/bootstrap-toggle/2.1.0/css/bootstrap-toggle.min.css" >
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='web.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='tooltip.css') }}">
    <style>
      .clip_button {
              background:#063;
              padding:4px;
              width:100px;
      }
    </style>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="{{ url_for('index') }}">test</a>
          </div>
          <!--/.navbar-header -->
          <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li id="home-url"><a href="{{ url_for('index') }}">Home</a></li>
                <li id="about-url"><a href="#about">About</a></li>
                <li id="contact-url"><a href="#contact">Contact</a></li>
            </ul>
            <!--/.navbar-nav -->
          </div>
          <!--/.nav-collapse -->
      </div>
      <!--/.container -->
    </div>
    <!--/.navbar -->
    <div class="container">
    {% block body %}{% endblock %}
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script type="text/javascript" src="{{url_for('static', filename='bootstrap/js/bootstrap.js')}}"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.min.js"></script>
    <script type="text/javascript" src="https://gitcdn.github.io/bootstrap-toggle/2.1.0/js/bootstrap-toggle.min.js"></script>
    <script type="text/javascript" >
      $("a.my-tool-tip").tooltip();
      $('.selectpicker').selectpicker();
      $("#reference_select").selectpicker("refresh");
    </script>  
    <script type="text/javascript" src="{{url_for('static', filename='ZeroClipboard/ZeroClipboard.js')}}"></script>
    <script language="JavaScript">
    ////copy to clip
            var clip = null;

      function $(id) { return document.getElementById(id); }

      function init() 
      {
          clip = new ZeroClipboard.Client();
          clip.setHandCursor( true );
      }

      function move_swf(ee)
      {    
          copything = document.getElementById(ee.id+"_text").value;
          clip.setText(copything);

            if (clip.div)
                    {        
                clip.receiveEvent('mouseout', null);
                clip.reposition(ee.id);
            }
            else{ clip.glue(ee.id);   }

            clip.receiveEvent('mouseover', null);

      }    

    </script>    

    <script type="text/javascript">
      {% block js %}{% endblock %}
    </script> 
  </body>
</html>

这是 (table.html):

{% extends 'base2.html' %}

{% block body onload="init();"%}


    <div class="row clearfix">
            <div class="col-md-12 column">
                    <table class="table">
                            <tbody>
                                    <tr>
                                      <td><input type='text' id='textid5_text' value='your text 5' /></td>
                                      <td><div id='textid5' onload="init();" onMouseOver='move_swf(this)' class='clip_button'>COPY</div></td>
                                    </tr>

                            </tbody>
                    </table>
            </div>
    </div>
{% endblock %}

onload 是一个 HTML 属性。跟晋江没关系。您只需将其放在您的 <body> 标签上即可。

<body onload="init();">

如果您不需要在使用此基本模板的每个页面中使用它,则需要在 <body> 标记内添加一个块

<body {% block body_attrs %}{% endblock %}>

然后在您的子模板中覆盖它

{% block body_attrs %} onload="init();" {% endblock %}