company.html file: <% INIT>
my $ user = GetUserFromCookies ();
$ m-> redirect ('/ login.html') unless $ user && $ user-> haveAccess ($ r-> uri);
</% INIT>
<& /Header.inc, title => "About site" &>
<h1> About Site </ h1>
<p> Our site is the most rulezny in RuNet! </ p>
<& /Footer.inc &>
<%FLAGS> section and a flag named inherit , or it can be calculated automatically. In the second case, in the folder where the component is located, a file with the name autohandler , and if it is not there, the search continues with the folder above, and so on - until the folder that mason considers to be the root. If an ancestor is found, the mason tries to find a parent for it.$m->call_next (the magic is exposed in the documentation). After the child has completed the execution of the parent component continues.<%ATTR> section of the component, you can <%ATTR> an arbitrary set of named parameters. If the parent components have attributes that are not directly declared in the current component, they are inherited.<%METHOD> section and is a “component in a component” that can be called in much the same way as connectors. Like conventional components, methods can be used as a template (for data output), and as a function (for obtaining the result of calculations)./autohandler . The only "but" - in the header displays the name of the page (in the title tag). It can be safely put in the attributes of the page, if we can get to them from the autohandler (and looking ahead, I will say yes, we can). Now we only have the duplicated <%INIT> section with the authorization check. What if authorization is not needed on all pages? An excellent and completely secure way to solve this problem is to entrust the page itself to control the verification. Let's get the authorize attribute, let the authorization be checked on all pages where it is set to the true value (for definiteness, we assume that we have to check all pages except /profile/login.html ). The results of our thinking: <% INIT>
my $ component_requested = $ m-> request_comp; # company.html component
if ($ component_requested-> attr ('authorize'))
{
my $ user = GetUserFromCookies ();
$ m-> redirect ('/ login.html') unless $ user && $ user-> haveAccess ($ r-> uri);
}
</% INIT>
<% ATTR>
authorize => 1
</% ATTR>
<& /Header.inc, title => $ component_requested-> attr ('title') &>
<h1> <% $ component_requested-> attr ('title')%> </ h1>
% $ m-> call_next;
<& /Footer.inc &>
<% ATTR> title => "About site" </% ATTR> <p> Our site is the most rulezny in RuNet! </ p>
<% ATTR> title => "Login to site" authorize => 0 </% ATTR> <p> There could be a login form </ p>
Header.inc accepts the title of the page as an argument, but in principle no one bothers to get to it from the header itself using exactly the same code.autohandler in them. This will allow more flexible management of attributes (for example, you can open access to the entire contents of the /free/ folder by moving the authorize attribute to the /free/autohandler ). You can also modify the content of the page (for example, add a top-level heading or a block with an EULA under the main text of the page)./autohandler write the default method to display the news (think up the implementation yourself), and in /profile/autohandler , the text that overrides it:<% METHOD rightmenu> <p> We will not give the data to anyone! </ p> </% METHOD>
SubMenu.inc component will deal with the SubMenu.inc , and the structure that describes the menu itself will be passed to it as a parameter. Question: where to put the structure to use the inheritance feature? Answer: in the component method. The following are fragments that are proposed to be added to the appropriate files. % my $ component_requested = $ m-> request_comp;
% if ($ component_requested-> method_exists ('submenu')) {
<& /SubMenu.inc, MenuItems => $ component_requested-> call_method ('submenu') &>
%}
<% METHOD submenu>
<% INIT>
return [
'/profile/password.html' => 'Change Password',
'/profile/update.html' => 'Refresh personal data',
'/profile/logout.html' => 'Logout'
];
</% INIT>
</% METHOD>
autohandler 's significantly reduces the amount of non-informative code in the templates. Attributes allow you to move from writing code to its configuration. Methods are ideal for describing visual blocks on a page, and allow flexible linking of code with components.Source: https://habr.com/ru/post/54290/
All Articles