All the examples you've seen thus far have had their attributes set at design time. This is not very useful in the majority of the cases; most often, you'd prefer to alter attributes like the loop counter or the visibility at run time, on the basis of logic in your script. Luckily, patTemplate knows this - and it allows you to accomplish this goal via the very cool setAttribute() method, which allows you to dynamically set template attributes on the fly. Consider the following example, which demonstrates: As you can see, there are two templates in the file above; the second one is initially hidden from view. However, I can turn it on in certain cases - such as, for example, when a user visits the site for the first time. Here's the script that takes care of this for me. Depending on the value of a particular variable, I can turn the second template on or off, via a call to setAttribute(). You can use the setAttribute() method to manipulate other template attributes as well - try it with the "loop" or "varscope" attributes to see how it works.
blog comments powered by Disqus |