Page View - PhpGt/WebEngine GitHub Wiki
A Page View is a static HTML file that makes up the final rendered page and acts to fully separate the application's view from its logic. A Page View that exists at "page/index.html" can be made dynamic by adding a Page Logic file at "page/index.php".
Page Views are loaded via the automatic router, according to the requested URI. Learn more about URIs.
API Webservices also have API View and API Logic files that share the same concepts as pages.
The structure of WebEngine application URIs is that the path does not include an extension. This is sometimes referred to as directory-style URIs. This allows for page views to be stored within single files, or nested in directories.
Like in most systems, requesting a directory will serve the index within that directory. Because of this, the developer must be careful to not create clashing files and directories: A request to /contact
will fail if there is a page/contact.html
and a page/contact/index.html
file present at the same time -- which one should the router choose?
In all software systems, "Don't Repeat Yourself" (DRY) is a principal that reduces code repetition by promoting the reuse of code using techniques such as object oriented programming.
PHP is a language that supports object oriented programming, and with modern tools, Client side assets such as JavaScript and CSS can be compiled with the benefits of object oriented programming too.
HTML however is often left behind when it comes to reusing code or creating templates. There are upcoming standards for handling W3C custom elements and W3C template elements which can be used, but WebEngine offers support for simple server-side templating and Page View reuse.
Page view HTML files exist within the page/
project directory. The view files map directly to the URI of an incoming request, but there is a special directory that can be used to store custom components on the server. The page/_component
directory can contain HTML files for each reusable HTML component in your application, which can then be added to other Page Views by using a custom HTML tag, <like-this>
, as described in the custom HTML components section.
The use of header and footer view files is a special templating technique and provide project-wide header and footer templates.
Any HTML content that is stored within page/_header.html
and page/_footer.html
is prefixed/suffixed to the page response, for every page that is requested at the same-or-lower directory level of the header/footer file.
Learn more in the Headers and footers section.
When building real-world applications, having a single Page View for every single requested URI would be infeasible. For example, in an ecommerce store, the URIs /shop/category/books
and /shop/category/music
would likely want to share the same page view.
Dynamic paths are made possible by prefixing the filename of the view file with an @
symbol, for example: /page/shop/category/@category-name.html
. This makes requests to any URI under the /shop/category/
directory render using the same page view, and it is possible to get the name of the requested category in Page Logic using $this->dynamicPath->get("category-name")
.
Dynamic paths are explained in more depth in the Dynamic URIs and Pages section.