ext.xtemplate.html.svn-base

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 376 行 · 第 1/2 页

SVN-BASE
376
字号
        <div class="body-wrap">        <div class="top-tools">            <a class="inner-link" href="#Ext.XTemplate-props"><img src="../resources/images/default/s.gif" class="item-icon icon-prop">Properties</a>            <a class="inner-link" href="#Ext.XTemplate-methods"><img src="../resources/images/default/s.gif" class="item-icon icon-method">Methods</a>            <a class="inner-link" href="#Ext.XTemplate-events"><img src="../resources/images/default/s.gif" class="item-icon icon-event">Events</a>                        <a class="bookmark" href="../docs/?class=Ext.XTemplate"><img src="../resources/images/default/s.gif" class="item-icon icon-fav">Direct Link</a>        </div>                <div class="inheritance res-block"><pre class="res-block-inner"><a ext:cls="Ext.Template" ext:member="" href="output/Ext.Template.html">Template</a>  <img src="resources/elbow-end.gif"/>XTemplate</pre></div>                <h1>Class Ext.XTemplate</h1>        <table cellspacing="0">            <tr><td class="label">Package:</td><td class="hd-info">Ext</td></tr>            <tr><td class="label">Defined In:</td><td class="hd-info">XTemplate.js</td></tr>            <tr><td class="label">Class:</td><td class="hd-info">XTemplate</td></tr>                                    <tr><td class="label">Extends:</td><td class="hd-info"><a ext:cls="Ext.Template" ext:member="" href="output/Ext.Template.html">Template</a></td></tr>                    </table>        <div class="description">            <p>A template class that supports advanced functionality like autofilling arrays, conditional processing withbasic comparison operators, sub-templates, basic math function support, special built-in template variables,inline code execution and more.  XTemplate also provides the templating mechanism built into <a ext:cls="Ext.DataView" href="output/Ext.DataView.html">Ext.DataView</a>.</p><p>XTemplate supports many special tags and built-in operators that aren't defined as part of the API, but aresupported in the templates that can be created.  The following examples demonstrate all of the supported features.This is the data object used for reference in each code example:</p><pre><code>var data = {    name: <em>'Jack Slocum'</em>,    title: <em>'Lead Developer'</em>,    company: <em>'Ext JS, LLC'</em>,    email: <em>'jack@extjs.com'</em>,    address: <em>'4 Red Bulls Drive'</em>,    city: <em>'Cleveland'</em>,    state: <em>'Ohio'</em>,    zip: <em>'44102'</em>,    drinks: [<em>'Red Bull'</em>, <em>'Coffee'</em>, <em>'Water'</em>],    kids: [{        name: <em>'Sara Grace'</em>,        age:3    },{        name: <em>'Zachary'</em>,        age:2    },{        name: <em>'John James'</em>,        age:0    }]};</code></pre><p><b>Auto filling of arrays and scope switching</b><br/>Using the <tt>tpl</tt> tag and the <tt>for</tt> operator,you can switch to the scope of the object specified by <tt>for</tt> and access its members to populate the template.If the variable in <tt>for</tt> is an array, it will auto-fill, repeating the template block inside the <tt>tpl</tt>tag for each item in the array:</p><pre><code>var tpl = <b>new</b> Ext.XTemplate(    <em>'&lt;p>Name: {name}&lt;/p>'</em>,    <em>'&lt;p>Title: {title}&lt;/p>'</em>,    <em>'&lt;p>Company: {company}&lt;/p>'</em>,    <em>'&lt;p>Kids: '</em>,    <em>'&lt;tpl <b>for</b>="kids">'</em>,        <em>'&lt;p>{name}&lt;/p>'</em>,    <em>'&lt;/tpl>&lt;/p>'</em>);tpl.overwrite(panel.body, data);</code></pre><p><b>Access to parent object from within sub-template scope</b><br/>When processing a sub-template, for example whilelooping through a child array, you can access the parent object's members via the <tt>parent</tt> object:</p><pre><code>var tpl = <b>new</b> Ext.XTemplate(    <em>'&lt;p>Name: {name}&lt;/p>'</em>,    <em>'&lt;p>Kids: '</em>,    <em>'&lt;tpl <b>for</b>="kids">'</em>,        <em>'&lt;tpl <b>if</b>="age &amp;gt; 1">'</em>,  <i>// <-- Note that the &gt; is encoded</i>            <em>'&lt;p>{name}&lt;/p>'</em>,            <em>'&lt;p>Dad: {parent.name}&lt;/p>'</em>,        <em>'&lt;/tpl>'</em>,    <em>'&lt;/tpl>&lt;/p>'</em>);tpl.overwrite(panel.body, data);</code></pre><p><b>Array item index and basic math support</b> <br/>While processing an array, the special variable <tt>{#}</tt>will provide the current array index + 1 (starts at 1, not 0). Templates also support the basic math operators+ - * and / that can be applied directly on numeric data values:</p><pre><code>var tpl = <b>new</b> Ext.XTemplate(    <em>'&lt;p>Name: {name}&lt;/p>'</em>,    <em>'&lt;p>Kids: '</em>,    <em>'&lt;tpl <b>for</b>="kids">'</em>,        <em>'&lt;tpl <b>if</b>="age &amp;gt; 1">'</em>,  <i>// <-- Note that the &gt; is encoded</i>            <em>'&lt;p>{#}: {name}&lt;/p>'</em>,  <i>// <-- Auto-number each item</i>            <em>'&lt;p>In 5 Years: {age+5}&lt;/p>'</em>,  <i>// <-- Basic math</i>            <em>'&lt;p>Dad: {parent.name}&lt;/p>'</em>,        <em>'&lt;/tpl>'</em>,    <em>'&lt;/tpl>&lt;/p>'</em>);tpl.overwrite(panel.body, data);</code></pre><p><b>Auto-rendering of flat arrays</b> <br/>Flat arrays that contain values (and not objects) can be auto-renderedusing the special <tt>{.}</tt> variable inside a loop.  This variable will represent the value ofthe array at the current index:</p><pre><code>var tpl = <b>new</b> Ext.XTemplate(    <em>'&lt;p>{name}\'</em>s favorite beverages:&lt;/p>',    <em>'&lt;tpl <b>for</b>="drinks">'</em>,       <em>'&lt;div> - {.}&lt;/div>'</em>,    <em>'&lt;/tpl>'</em>);tpl.overwrite(panel.body, data);</code></pre><p><b>Basic conditional logic</b> <br/>Using the <tt>tpl</tt> tag and the <tt>if</tt>operator you can provide conditional checks for deciding whether or not to render specific parts of the template.Note that there is no <tt>else</tt> operator &mdash; if needed, you should use two opposite <tt>if</tt> statements.Properly-encoded attributes are required as seen in the following example:</p><pre><code>var tpl = <b>new</b> Ext.XTemplate(    <em>'&lt;p>Name: {name}&lt;/p>'</em>,    <em>'&lt;p>Kids: '</em>,    <em>'&lt;tpl <b>for</b>="kids">'</em>,        <em>'&lt;tpl <b>if</b>="age &amp;gt; 1">'</em>,  <i>// <-- Note that the &gt; is encoded</i>            <em>'&lt;p>{name}&lt;/p>'</em>,        <em>'&lt;/tpl>'</em>,    <em>'&lt;/tpl>&lt;/p>'</em>);tpl.overwrite(panel.body, data);</code></pre><p><b>Ability to execute arbitrary inline code</b> <br/>In an XTemplate, anything between {[ ... ]}  is consideredcode to be executed in the scope of the template. There are some special variables available in that code:<ul><li><b><tt>values</tt></b>: The values in the current scope. If you are using scope changing sub-templates, youcan change what <tt>values</tt> is.</li><li><b><tt>parent</tt></b>: The scope (values) of the ancestor template.</li><li><b><tt>xindex</tt></b>: If you are in a looping template, the index of the loop you are in (1-based).</li><li><b><tt>xcount</tt></b>: If you are in a looping template, the total length of the array you are looping.</li><li><b><tt>fm</tt></b>: An alias for <tt>Ext.util.Format</tt>.</li></ul>This example demonstrates basic row striping using an inline code block and the <tt>xindex</tt> variable:</p><pre><code>var tpl = <b>new</b> Ext.XTemplate(    <em>'&lt;p>Name: {name}&lt;/p>'</em>,    <em>'&lt;p>Company: {[values.company.toUpperCase() + '</em>, <em>' + values.title]}&lt;/p>'</em>,    <em>'&lt;p>Kids: '</em>,    <em>'&lt;tpl <b>for</b>="kids">'</em>,       <em>'&lt;div class="{[xindex % 2 === 0 ? "even" : "odd"]}">'</em>,        <em>'{name}'</em>,        <em>'&lt;/div>'</em>,    <em>'&lt;/tpl>&lt;/p>'</em>);tpl.overwrite(panel.body, data);</code></pre><p><b>Template member functions</b> <br/>One or more member functions can be defined directly on the configobject passed into the XTemplate constructor for more complex processing:</p><pre><code>var tpl = <b>new</b> Ext.XTemplate(    <em>'&lt;p>Name: {name}&lt;/p>'</em>,    <em>'&lt;p>Kids: '</em>,    <em>'&lt;tpl <b>for</b>="kids">'</em>,        <em>'&lt;tpl <b>if</b>="<b>this</b>.isGirl(name)">'</em>,            <em>'&lt;p>Girl: {name} - {age}&lt;/p>'</em>,        <em>'&lt;/tpl>'</em>,        <em>'&lt;tpl <b>if</b>="<b>this</b>.isGirl(name) == false">'</em>,            <em>'&lt;p>Boy: {name} - {age}&lt;/p>'</em>,        <em>'&lt;/tpl>'</em>,        <em>'&lt;tpl <b>if</b>="<b>this</b>.isBaby(age)">'</em>,            <em>'&lt;p>{name} is a baby!&lt;/p>'</em>,        <em>'&lt;/tpl>'</em>,    <em>'&lt;/tpl>&lt;/p>'</em>, {     isGirl: <b>function</b>(name){         <b>return</b> name == <em>'Sara Grace'</em>;     },     isBaby: <b>function</b>(age){        <b>return</b> age < 1;     }});tpl.overwrite(panel.body, data);</code></pre>        </div>                <div class="hr"></div>                <a id="Ext.XTemplate-props"></a>        <h2>Public Properties</h2>        <div class="no-members">This class has no public properties.</div>        <a id="Ext.XTemplate-methods"></a>        <h2>Public Methods</h2>                <table cellspacing="0" class="member-table">            <tr>                <th class="sig-header" colspan="2">Method</th>                <th class="msource-header">Defined By</th>            </tr>                <tr class="method-row expandable">
        <td class="micon"><a class="exi" href="#expand">&nbsp;</a></td>
        <td class="sig">
        <a id="Ext.XTemplate-XTemplate"></a>
            <b>XTemplate</b>(&nbsp;<code>String/Array/Object parts</code>&nbsp;)            <div class="mdesc">
                        <div class="short"></div>
            <div class="long">
                    <div class="mdetail-params">
        <strong>Parameters:</strong>
        <ul><li><code>parts</code> : String/Array/Object<div class="sub-desc">The HTML fragment or an array of fragments to join(""), or multiple argumentsto join("") that can also include a config object</div></li>        </ul>
        <strong>Returns:</strong>
        <ul>
            <li><code></code></li>
        </ul>
    </div>
                </div>
                        </div>
        </td>
        <td class="msource">XTemplate</td>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?