,
[ Pobierz całość w formacie PDF ]
.DTML Expression Gotchas 56IntroductionBefore we leave the subject of variable expressions we should mention that there is a deprecated form of theexpression syntax.You can leave out the "expr=" part on a variable expression tag.But please don't do this.Itis far too easy to confuse:with:and get two completely different results.These "shortcuts" were built into DTML long ago, but we do notencourage you to use them now unless you are prepared to accept the confusion and debugging problems thatcome from this subtle difference in syntax.The Var TagThe var tag inserts variables into DTML Methods and Documents.We've already seen many examples ofhow the var tag can be used to insert strings into web pages.As you've seen, the var tag looks up variables first in the current object, then in its containers and finally inthe web request.The var tag can also use Python expressions to provide more control in locating and calling variables.Var Tag AttributesYou can control the behavior of the var tag using its attributes.The var tag has many attributes that help youin common formatting situations.The attributes are summarized in the Appendix A.Here's a sampling ofvar tag attributes.html_quoteThis attribute causes the inserted values to be HTML quoted.This means that and & are escaped.missingThe missing attribute allows you to specify a default value to use in case Zope can't find the variable.For example:fmtThe fmt attribute allows you to control the format of the var tags output.There are many possibleformats which are detailed in Appendix A.One use of the fmt attribute is to format monetary values.For example, create a float property in yourroot folder called adult_rate.This property will represent the cost for one adult to visit the Zoo.Givethis property the value 2.2.You can display this cost in a DTML Document or Method like so:One Adult pass:This will correctly print "$2.20".It will round more precise decimal numbers to the nearest penny.The Var Tag 57IntroductionVar Tag Entity SyntaxZope provides a shortcut DTML syntax just for the simple var tag.Because the var tag is a singleton, it canbe represented with an HTML entity like syntax:&dtml-cockatiel;This is equivalent to:The main reason to use the entity syntax is to avoid putting DTML tags inside HTML tags.For example,instead of writing:You can use the entity syntax to make things more readable for you and your text editor:The var tag entity syntax is very limited.You can't use Python expressions and some tag attributes with it.The If TagOne of DTML's important benefits is to let you customize your web pages.Often customization meanstesting conditions are responding appropriately.This if tag lets you evaluate a condition and carry outdifferent actions based on the result.What is a condition? A condition is either a true or false value.In general all objects are considered trueunless they are 0, None, an empty sequence or an empty string.Here's an example condition:objectValuesTrue if the variable objectValues exists and is true.That is to say, when found and renderedobjectValues is not 0, None, an empty sequence, or an empty string.As with the var tag, you can use both name syntax and expression syntax.Here are some conditionsexpressed as DTML expressions.expr="1"Always true.expr="rhino"True if the rhino variable is true.expr="x 5"True if x is less than 5.expr="objectValues('File')"True if calling the objectValues method with an argument of File returns a true value.This method isexplained in more detail in this chapter.The if tag is a block tag.The block inside the if tag is executed if the condition is true.Var Tag Entity Syntax 58IntroductionHere's how you might use a variable expression with the if tag to test a condition:How many monkeys are there?There are too many monkeys!In the above example, if the Python expression monkeys > monkey_limit is true then you will see thefirst and the second paragraphs of HTML.If the condition is false, you will only see the first.If tags be nested to any depth, for example, you could have:Are there too many blue monkeys?There are too many blue monkeys!Nested if tags work by evaluating the first condition, and if that condition is true, then evaluating the second.In general, DTML if tags work very much like Python if statements.Name and Expression Syntax DifferencesThe name syntax checks for the existence of a name, as well as its value.For example:There is a monkey house Mom!If the monkey_house variable does not exist, then this condition is false.If there is a monkey_house variablebut it is false, then this condition is also false.The condition is only true is there is a monkey_house variableand it is not 0, None, an empty sequence or an empty string.The Python expression syntax does not check for variable existence.This is because the expression must bevalid Python.For example:There is a monkey house, Mom!This will work as expected as long as monkey_house exists.If the monkey_house variable does not exist,Zope will raise a KeyError exception when DTML tries to find the variable.Else and Elif TagsThe if tag only lets you take an action if a condition is true.You may also want to take a different action if thecondition is false.This can be done with the DTML else tag.The if block can also contain an else singletontag [ Pobierz całość w formacie PDF ] |
Archiwum
|