Code locality in CXP pages Foundation
The cxc-builder transpiles the CXP page into PRG code. For this, the cxc-builder creates Xbase++ code for the HTML markup and merges the generated code with the code in the <%...%> code sections defined in the CXP page. By default, the resulting code is injected into the :render() method of the page class.
However, sometimes more complex code needs to be added to a CXP page. For this, code locality directives can be used to tell the cxc-builder where to place the Xbase++ code in your code sections. The following code is injected into the header of the resulting PRG file, allowing the developer to add functions/procedures/classes or statics to a CXP page.
<!DOCTYPE html>
<html>
<%#code locality="page-global"%>
<%
// This section is injected into the module header
STATIC aData := { 3, 5, 7, 11 }
FUNCTION MyHelper(cInput)
LOCAL cDate
cDate := SubStr( CMonth(Date()), 1, 3) + ". "
cDate += Str( Day(Date()) ,2) + ", "
cDate += Str( Year(Date()) ,4)
RETURN(cDate)
%>
<body>
<p>Today is @(MyHelper())!</p>
</body>
</html>
To sum it up, using <%#code locality> directives the location of your Xbase++ code in the generated code can be controlled. This way, your CXP pages and related code can be organized in such a way that there is no chaotic mixup between code and markup. To learn more about the separation between view and logic, see the CXP helperschapter.
CXP comes with the following localities for your code: page-init, page-load, page-unload, page-global and finally, the default locality which is page-render. For a detailed explanation of the different code localities, see the #code directivein the Language Reference chapter.
The following are some best-practice rules related to code locality.
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.