No more need to carry html-layout for each page. By using templates, the content of the pages are automatically placed in the desired area. The number of content blocks is not limited.
Templates - is HTML files with special tags, which will be replaced to the content.
Content markers must be written in the format: <php:markerName/>
.
The template can be an unlimited number of content markers, but usually only two or three.
The following example shows the template in which defined two markers to output content: <php:Head/>
and <php:MainContent/>
.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="/Content/css/bootstrap.min.css" />
<script src="/Scripts/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="/Scripts/bootstrap.min.js" type="text/javascript"></script>
<php:Head/>
</head>
<body>
<div class="container">
<php:MainContent/>
</div>
</body>
</html>
Pages can contain blocks of content for the markers. To place content blocks, uses a special tag: php:Content
.
<php:Content ID="markerName">
Contents of the block here.
Acceptable use any tag,
server code and
user controls.
</php:Content>
If for the marker has not be content, then the marker is removed from the final code.
The following example shows HTML-code page with content for MainContent
: <h2>Hello world!</h2>
.
<php:Content ID="MainContent">
<h2>Hello world!</h2>
</php:Content>
The result of a web request specified below.
<!DOCTYPE html>
<html>
<head>
<title>Default</title>
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="/Content/css/bootstrap.min.css" />
<script src="/Scripts/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="/Scripts/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<h2>Hello world!</h2>
</div>
</body>
</html>
The templates can be used directive <?#Register ?>
to register a user controls.
Other directives are not supported and will not be processed.
The following example shows how to use directives in the template. The <?#Page ?>
directive will be ignored and will result in the original form. The <?#Register ?>
directives will be successfully processed and removed from the output data.
<?#Page Title="Test"?>
<?#Register Src="~/Controls/Header.php" TagPrefix="php" TagName="Header"?>
<?#Register Src="~/Controls/Footer.php" TagPrefix="php" TagName="Footer"?>
<?#Register Src="~/Controls/Message.php" TagPrefix="php" TagName="Message"?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<php:Header ID="Header1" />
<div class="container">
<php:Message Type="Info" Content="This is a simple example" />
<hr />
<php:MainContent/>
</div>
<php:Footer ID="Footer1" />
</body>
</html>
<php:Content ID="MainContent">
Hello world!
</php:Content>
<!--The #Page directive is not supported in the templates-->
<?#Page Title="Test"?>
<!DOCTYPE html>
<html>
<head>
<title>Default</title>
</head>
<body>
<!--Start Header-->
<h1>Test site</h1>
<!--End Header-->
<div class="container">
<!--Start Message-->
<div class="alert alert-info">This is a simple example</div>
<!--End Message-->
<hr />
<!--Start MainContent-->
Hello world!
<!--End MainContent-->
</div>
<!--Start Footer-->
<footer>
Copyright © Example, 2015. All rights reserved.
</footer>
<!--Endа Footer-->
</body>
</html>
To localize the templates can be used global resources (global.json).
When localization should take into account that individual keys can be overwritten in the local resources of pages.
Unfortunately, in the current version of the engine, in a templates does not work processing server-side code. It is easy to fix, but it does not make sense, because you can use user controls, in which there are no such restrictions.
If you want to implement support for server-side code in the templates, you can do it in the \Nemiro\UI\Page
. For example, in the method Render
.
# ...
# add head
$pattern = '|<head>(.*)</head>|is';
$result = preg_replace($pattern, '<head>'.$h.'</head>', $result);
# processing blocks of a server code here
$result = $this->Execute($result);
# controls
$result = $this->RanderControl('Template', $result);
# content blocks
if ($this->Content != NULL && count($this->Content) > 0)
# ...
Please note that the processing of server-side code will require additional resources and high load it can slow down processing of requests.
Marker names (identifiers) should consist of English letters and numbers.
php:
.Marker names are not checked for uniqueness. If a template has a multiple markers with the same names, the contents for the markers will be set for all copies.
The following example shows a template with two tags <php:MainContent/>
and the result of the query.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
<php:MainContent/>
<br />
<php:MainContent/>
</div>
</body>
</html>
<php:Content ID="MainContent">
Hello!
</php:Content>
<!DOCTYPE html>
<html>
<head>
<title>Default</title>
</head>
<body>
<div class="container">
Hello!
<br />
Hello!
</div>
</body>
</html>