Curl macros introduction (part 1) - Printable Version +- Curl Global Community (https://communities.curl.com) +-- Forum: Blogs (https://communities.curl.com/forumdisplay.php?fid=17) +--- Forum: Tech blog (https://communities.curl.com/forumdisplay.php?fid=18) +---- Forum: Robert blog (https://communities.curl.com/forumdisplay.php?fid=20) +---- Thread: Curl macros introduction (part 1) (/showthread.php?tid=195) |
Curl macros introduction (part 1) - RobertShiplett - 08-10-2011 This series is about Curl macros Macros allow you to create both very simple and very complex results in Curl web pages, in Curl applications, and in Curl scripts. Most often they hide complexity from the user of the macro and keep markup simple. You use a macro in Curl just as you would use any other Curl expression. Examples: are any macros? Suppose We Wrote:{paragraph This is a text procedure } Default Curl expressions (from the Curl library packages) which begin with {define- are macros. The Curl documentation clearly indicates which expression prefixes are macros both in the index and the respective macro pages. The live-code in the docs in built using the {example } macro. Expressions with arguments not separated by commas or separated by other characters would be macros (but I suggest using commas in your macros and in what follows I try to show why.) A macro is used to define new macros: it is {define-macro } and it gets its power from two other macros, those being {syntax-switch } and {expand-template }. A macro call is replaced by the source code corresponding to the value which the macro returns (although macros do not declare a return value, they use {return } expressions. In a package write: Code: {define-macro public {uc ?txt:text} In an applet write: Code: {import uc from MY.MACROS} In the browser we see Curl Wrote:THIS QUOTED TEXT WILL PRINT IN CAPS WITHOUT THE QUOTES Note: Curl is an expression-based language, but macros may identify some expressions as statements. Examples are let and set expressions which were not surrounded by curly-braces for your convenience or other expressions where curl-braces were not required. Macro pattern case logic can recognize MyClass.myClassProc as a BinaryOp. But we will set all these details to one side for now. RE: Curl macros introduction (part 1) - RobertShiplett - 08-10-2011 When passed quoted characters, all of these variants have the same effect: Code: {define-macro public {uc ?txt:text} When called as Code: {uc "this quoted text will print in caps without the quotes"} RE: Curl macros introduction (part 1) - RobertShiplett - 08-10-2011 When passed a String identifier, all of these variants have the same effect (the proc is the clue that these simple macros are examples only): Code: {define-proc {up s:String}:String when called as Code: {def str:String = "testing macros"} RE: Curl macros introduction (part 1) - RobertShiplett - 08-10-2011 But what happens if you attempt Code: {br} ??? RE: Curl macros introduction (part 1) - glennmcgrath - 04-25-2015 The difficulty for understanding {value} is especially obvious when using at the top level of a curl applet file. To understand this, a user must know a DefaultDocument or PlainDocument will add an object without being defined with a varirable to it's layout container (a TextFlowBox) by default , while a variable definition will be kept as a variable only and value of this variable won't be added to it's layout container implicitly. So when you want a variable to be added to a DefaultDocument or PlainDocument, eithier use {value var} at the top level of the *Document, or wrap this variable with another Graphical container like a Fill or Frame or anything else, which is then instantiated directly at the top level of *Document without being assigned to any variable. |