<STYLE>
.example {
background-color: #DEA;
display:block;
padding: .25em;
margin: .25em;
}
code.result { background-color: #ADA; }
</STYLE>
<H1>Learn Yote by example</H1>
<h2>Level 1 - template commands not using Yote objects </h2>
<p>
This is a simple walkthrough that touches on all parts of the client templates that don't involve
Yote objects. Only the <code>BODY</code> section and the template definitions are included here. The surrounding HTML tags,
javascript includes and template init code is omitted.
<p>
<h3>Using a template in a page.</h3>
<P>The simplist template example. This just places the text of the template in the <code>div</code> calling the template.</P>
<pre><code class="example">
<script type="text/template" class="yote_template_definition" template_name="HelloWorld">
Hello <i>World</i>
</script>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld"></div>
</BODY>
</code></pre>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld">
Hello <i>World</i>
</DIV>
</BODY>
</pre></code>
<h3>Calling a template from an other template.</h3>
<P>
The directive <code><$$ <i>templatename</i> $$></code> gets replaced by the filled out
content of the named template.
</P>
<code class="example"><pre>
<script type="text/template" class="yote_template_definition" template_name="HelloTemplate">
Hello Template
</script>
<script type="text/template" class="yote_template_definition" template_name="HelloWorld">
<$$ HelloTemplate $$> <BR>
Hello World
</script>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld"></div>
</BODY>
</pre></code>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld">
Hello Template <BR>
Hello World
</DIV>
</BODY>
</pre></code>
<h3>Function building templates.</h3>
<p>
The directive <code><? <i>registered-function-name</i> ?></code> incidates that
a function is to be called and its return value replaces the directive. The function
must be registered with the templating system.
</p>
<code class="example"><pre>
<SCRIPT>
$.yote.util.register_function( "hello", function() { return "Hello Functional World" } );
</SCRIPT>
<script type="text/template" class="yote_template_definition" template_name="HelloWorld">
<? hello ?>
</script>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld"></div>
</BODY>
</pre></code>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld">
Hello Functional World
</DIV>
</BODY>
</pre></code>
<h3>Buttons and Links</h3>
<p>Buttons and links can be quickly placed in a template using <code><$ button <i>registered-function-name</i> <i>Button Text</i> $></code>
and <code><$ action_link <i>registered-function-name</i> <i>Link Text</i> $></code>.
The templating system automatically attaches a click listener that calls the named registered function.
</P>
<code class="example"><pre>
<SCRIPT>
$.yote.util.register_function( "hello", function() { return "Hello CLICK" } );
</SCRIPT>
<script type="text/template" class="yote_template_definition" template_name="HelloWorld">
<$ button hello Clickme for Hello $> <BR>
<$ action_link hello Click Link $>
</script>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld"></div>
</BODY>
</pre></code>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld">
<BUTTON type="BUTTON" id="__yidx_<i>n</i>" class="yote_button" template_id="__yidx_<i>m</i>">Clickme for Hello</BUTTON> <BR>
<A HREF="#" id="__yidx_<i>o</i>" class="yote_action_link" action="hello" template_id="__yidx_<i>m</i>">Click Link</A>
</DIV>
</BODY>
</pre></code>
<h3>Variables</h3>
<p>
A variable set with the <code>var</code> directive and accessed with the <code>val</code> directive.
Variables are passed up from template to template.
</p>
<code class="example"><pre>
<script type="text/template" class="yote_template_definition" template_name="HelloTemplate">
Hello <b><$ val name $></b>.
</script>
<script type="text/template" class="yote_template_definition" template_name="HiAgatha">
<$$$ var name Agatha $$$> <BR>
<$ val name $>
<$$ HelloTemplate $$> <BR>
</script>
<BODY>
<DIV class="yote_template side_panel" template="HiAgatha"></div> <BR>
</BODY>
</pre></code>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld">
Hello Agatha
</DIV>
</BODY>
</pre></code>
<h3>Reassigning Variables</h3>
<p>
A variable can be reassigned in a template. The new value will remain for any templates
called by the template where the value was reassigned.
</p>
<code class="example"><pre>
<script type="text/template" class="yote_template_definition" template_name="HelloTemplate">
Hello <b><$ val name $></b>.
</script>
<script type="text/template" class="yote_template_definition" template_name="HiAgatha">
<$$$ var name Agatha $$$> <BR>
<$ val name $> <BR>
<$$ HelloSomeoneElse $$>
</script>
<script type="text/template" class="yote_template_definition" template_name="HiSomeoneElse">
<$$$ var name Bart $$$> <BR>
<$$ HelloTemplate $$> <BR>
</script>
<BODY>
<DIV class="yote_template side_panel" template="HiAgatha"></div> <BR>
</BODY>
</pre></code>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloWorld">
Hello Agatha <BR>
Hello Bart
</DIV>
</BODY>
</pre></code>
<h3>Variables and functions</h3>
<p>
Assigned variables are automatically passed to registered functions when they are called by the template system.
A context object is automatically passed to all functions, and its vars dictionary keying the
var values to their name.
</p>
<code class="example"><pre>
<SCRIPT>
function sayhello( context ) {
alert( "Greetings " + context.vars[ 'name' ] );
}
$.yote.util.register_function( "hello", sayhello );
function puthello( context ) {
return "Hello : " + context.vars[ 'name' ];
}
$.yote.util.register_function( "hello", sayhello );
</SCRIPT>
<script type="text/template" class="yote_template_definition" template_name="HelloTemplate">
<$$$ var name Dude $$$>
<$ button hello Clickme $> <BR>
<? sayhello ?$>
</script>
<BODY>
<DIV class="yote_template side_panel" template="HelloTemplate">
<BUTTON type="BUTTON" id="__yidx_<i>n</i>" class="yote_button" template_id="__yidx_<i>m</i>">Clickme</BUTTON> <BR>
Hello : Dude
</div>
</BODY>
</pre></code>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloTemplate">
<BUTTON type="BUTTON" id="__yidx_<i>n</i>" class="yote_button" template_id="__yidx_<i>m</i>">Clickme</BUTTON> <BR>
Hello : Dude
</div>
</BODY>
</pre></code>
<h3>Controls as variables and after rendering functions</h3>
<p>
The directive <$$$ control <i>name</i> <i>html control</i> $$$> assigns a serialized
id to the html control and replaces the directive with this control. The assigned id is
stored in the controls dictionary of the context; the name is the key and the control id is
the value. The control can be looked up by the ID.
</P>
<code class="example"><pre>
<script type="text/template" class="yote_template_definition" template_name="HelloTemplate">
<$$$ control text <INPUT TYPE="TEXT"> $$$> <BR>
<$$$ control button <BUTTON type="BUTTON">Clickme</BUTTON> $$$>
<?? setup_hello ??>
</script>
<SCRIPT>
$.yote.util.register_function( "setup_hello",
function( context ) {
var button_id = context.controls[ 'button' ];
var text_id = context.controls[ 'text' ];
$( '#' + button_id ).click( function() {
alert( "Greetings " + $( '#' + text_id ).val() );
} );
} );
</SCRIPT>
<BODY>
<DIV class="yote_template side_panel" template="HelloTemplate"></div>
</BODY>
</pre></code>
HTML Result
<code class="example result"><pre>
<BODY>
<DIV class="yote_template side_panel" template="HelloTemplate">
<INPUT TYPE="TEXT" id="__yidx_<i>m</i>"> <BR>
<BUTTON type="BUTTON" id="__yidx_<i>n</i>" class="yote_button" template_id="__yidx_<i>m</i>">Clickme</BUTTON> <BR>
</div>
</pre></code>