Using Contextual Object Member Access

Contents
[ ]

You can make your templates less cumbersome using the contextual object member access feature. This feature enables you to access members of some objects without specifying the objects' identifiers in template expressions. An object to which the feature can be applied is determined depending on a context as follows:

  • Inside a data band body, the object is resolved to the innermost iteration variable.
  • Outside a data band body, the object is resolved to a passed data source.

Obviously, inside a data band body, you can not use the feature to access members of an outer iteration variable or a passed data source object. With the exception of this restriction, you can use both contextual and common object member access syntaxes interchangeably depending on your needs and preferences.

Consider the following example. Given that ds is a DataSet instance containing a DataTable object named “Persons” that has fields named “Name” and “Age”, you can use the following template to list the contents of the table.

No. Name Age
<<foreach [p in ds.Persons]>><<[ p.numberOf()]>> <<[p.Name]>> <<[p.Age]>><</foreach>>
Count: <<[ds.Persons.count()]>>

Alternatively, you can use the following template involving the contextual object member access syntax to get the same results.

No. Name Age
<<foreach [ in Persons]>><<[ numberOf()]>> <<[Name]>> <<[Age]>><</foreach>>
Count: <<[Persons.count()]>>

FAQ

  1. Q: How can I use contextual object member access inside an IF field?
    A: Place the IF field around the expression that uses contextual access, e.g., <<if [Age] > 30>>Adult<<endif>>. The member Age is resolved to the current iteration variable, so the condition works without specifying the variable name.

  2. Q: What happens if I try to access a member of an outer iteration variable using contextual access?
    A: Contextual access only resolves to the innermost iteration variable. Attempting to reference an outer variable will result in an error or an empty value. Use the full object identifier (e.g., outerVar.Property) for outer scopes.

  3. Q: Is contextual object member access available in other language APIs such as Java or Python?
    A: Yes, the same template syntax is supported across all Aspose.Words language bindings, including Java and Python. The feature is part of the template engine, not the programming language, so the same templates can be used regardless of the host language.