RegularWebPageTemplate
The main Pug template for generating of the valid HTML5 pages recommended for most cases.
- It is assumed that this template will be inherited with extends keyword.
- Includes the Functionality.pug file with additional JavaScript functionality for Pug (including integration with @yamato-daiwa/es-extensions), that is why including of this file repeatedly will cause the error.
If only inherit the Pug file from this template but without adding any other code, then the output code already will represent the valid HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Untitled</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body></body>
</html>
Because the lang
attribute is required for
html
tag and title
tag is also necessary, they has been generated according the default settings.
Most likely you want to change them, particularly if you page is non-English.
These and many other things could be done by
RegularWebPageTemplate__YDF,
the JavaScript class for the configuring which could be accessed from
JavaScript block of Pug preprocessor.
RegularWebPageTemplate__YDF
Configuration Class
Allows to configure the generating of the typical HTML code. This class is available starting from the end of the Functionality Pug block, but it is recommended to access to this class in Metadata block, because majority of settings either related with meta tags, or is the metadata at its core:
block append Metadata
-
RegularWebPageTemplate__YDF.configure({
metadata: {
locale: "en",
title: "Produts List — \"Galore\" Online Store"
}
});
The settings could be specified by calling the configure
static
method, the only parameter of which is a
multi-level configuration object:
- metadata.locale
- Used to fill the attribute
lang
of the taghtml
. Since this attribute is required for thehtml
tag, there is the default value "en", which must be replaced with another value for non-English pages. It is noteworthy, but the value of the attributelang
is officially called "language tag", what is confusing, that is why this property has been named the"locale"
. - metadata.title
- Used to fill the tag
title
. Since this tag is required, there is a default value — "Untitled". - metadata.description
- If defined, then the tag
meta
will be added with the attributesname="description"
andcontent
, the last of which will be filled with the specified value. - metadata.keywords
- If defined, then the tag
meta
will be added with the attributesname="keywords"
andcontent
, the last of which will be filled with the specified value. - metadata.author
- If defined, then the tag
meta
will be added with the attributesname="author"
andcontent
, the last of which will be filled with the specified value. - faviconURI
- If defined, then 2 tags
link
will be added, herewith one of them will have therel="icon"
attribute, and another one will haverel="shortcut icon"
, attribute, and also both will have thehref
attribute filled with the specified value. This value must be a valid URI referencing the icon file to be displayed on the browser tab. - AMP_VersionURI
- If defined, then the tag
link
will be added with the attributesrel="amphtml"
andhref
, the last of which will be filled with the specified value. This value must be a valid URI referencing the AMP version of the same page. - stylesSheetsURIs
- If defined with the array, then for each its element the
tag
link
withrel="stylesheet"
andhref
attributes will be added, herewith thehref
attribute will be filled by the array element. Each of these elements must be the string and represent the valid URI referencing the CSS file. - scripts.atEndOfHead
If defined with the array, then for each its element the tag
script
withsrc
attribute will be added to end of thehead
tag, herewith thesrc
attribute will be filled by the array element. Each of these elements must be the string and represent the valid URI referencing the JavaScript file.- Generally it is not recommended to add the scripts to the end of head tag because without specific measures the rendering of the HTML page will be suspended until the script will be loaded. The exceptions are the cases such as the third-party scripts for the analytics.
- The
script
tags added by this way will not have any attributes exceptsrc
. If you need any other attributes, add thescript
tag manually using theHeadScripts
Pug block.
- scripts.atEndOfBody
If defined with the array, then for each its element the tag
script
withsrc
attribute will be added to end of thebody
tag, herewith thesrc
attribute will be filled by the array element. Each of these elements must be the string and represent the valid URI referencing the JavaScript file.- This method of adding of the scripts is the recommended one because this way the loading of the scripts will not detain the page rendering.
- The
script
tags added by this way will not have any attributes exceptsrc
. If you need any other attributes, add thescript
tag manually using theEndBodyScripts
Pug block.
The configure
method could be called
repeatedly, but because the repeated specifying of
same properties will overwrite the previous values, the calling of this
method repeatedly is meaningful only if specify the
non-repetitive properties.
For example, if your site or application has single natural language (assume that English), and there is the
single author for all pages, it is meaningful to specify the respective settings in the separate file
(e. g. CommonMetadata.pug):
-
RegularWebPageTemplate__YDF.configure({
metadata: {
locale: "en",
author: "Acme Corporation"
}
});
Then, this file could be included to the files of pages an specify only specific for certain page metadata:
block append Metadata
include CommonMetadata.pug
-
RegularWebPageTemplate__YDF.configure({
metadata: {
title: "Services"
// "locale" и "author" already has been set in "CommonMetadata.pug"
}
});
Pug Blocks
The modification of Pug templates is being executed via Pug blocks. In RegularWebPageTemplate template, the following Pug blocks has been declared.
Functionality
Use this block if you want to write some functionality on built-in JavaScript.
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append Functionality
-
function sum(...summands) {
return summands.reduce((interimSum, summand) => interimSum + summand, 0)
}
Because the functionality of
"Yamato Daiwa ECMAScript Extensions"
library is being included to this block, starting from this block you can
use it
if specified the append
keyword during referring to this
block:
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append Functionality
-
function formatPrice(amount) {
return `${ separateEach3DigitsGroupWithComma(amount) }$`;
}
Data
Use this block if you want to generate some data to use it for the generating of the markup.
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append Data
-
const items = Array.from(new Array(16).keys()).map(
index => ({
ID: index,
title: `Item ${ index + 1 }`
})
);
Requirements
Use this block for including of the Pug files containing the Pug mixins or inner JavaScript code, for which the previous blocks are not suited for some reason.
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append Requirements
mixin ProductCard(product)
.ProductCard
h1= product.title
p= product.description
Metadata
Use this block if you want to define the metadata of HTML page
via JavaScript variables.
In particular, this block is exactly where it is
recommended to call the
RegularWebPageTemplate__YDF.configure({})
.
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append Metadata
-
RegularWebPageTemplate__YDF.configure({
metadata: {
locale: "en",
author: "ACME Co., Ltd."
}
});
StatesSimulations
If it is planned to create the dynamic page, then on the stage of creating of the markup and styles using the Pug conditional rendering it is possible to check, does page displaying correctly with or without certain elements. Specifically, various states could be displayed. For example, if the dynamic data collections retrieved by AJAX must be displayed, the page will have the following states:
- Data retrieving is process
- Data retrieving error
- No data
- There is the data but no search results
- The data has been successfully retrieved and displaying
All of this states could be defined via boolean variables (in below example it has been
implemented by the class with the object-type
statesSimulations
static field which
properties are referring to specific states) and organize the conditional displaying:
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append Metadata
-
class ProductsListPage {
static statesSimulations: {
loading: false,
loadingError: false,
noData: false,
noSearchingResults: false
};
}
block append PageContent
main
h1 Товары
if ProductsListPage.statesSimulations.loading
//-
Commonly for the loading state the component called "Loading Placeholder"
(AKA "Skeleton Loading" or "Content Placeholder") could be fine
else if ProductsListPage.statesSimulations.loadingError
//- Erorr message
else if ProductsListPage.statesSimulations.noData
//- The message about there is no goods or everything has been sold
else if ProductsListPage.statesSimulations.noSearchingResults
//- The message about no search request results
else
//- Goods list
To switch between the states, it is required to re-run the Pug.
However if you want to represent all of these states as separate pages to customer
before the implementation of JavaScript dynamics and server part but also
minimize the routines (particularly, avoid the manual
creating of the separate page for each state),
consider the
static preview concept
and the project building tool
Yamato Daiwa Automation (abbreviation: YDA), which has the
functionality designed exactly for such cases.
HeadBegin
html
tag.
All previous blocks are intended to be used
only for the declaring of
internal JavaScript code
and Pug mixin what does not entail the output HTML code.Use this block if you want to add something to the start of the
head
tag (before
other children elements).
The sequence of children in relation to head
elements is
rarely matters (mainly it ths the sequence of the styles and scripts), so
usually there is no need to add something precisely to the start thus
this block will come in handy only in the rare occasions.
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
// ...
block append HeadBeging
link(rel="dns-prefetch" href="//example.com")
link
has been added to the start of
head
tag, but there was no need to add the
link
to exactly start of head
.
head
tag.
Styles
Use this block if for some reason it is not enough to specify the styles via
(RegularWebPageTemplate__YDF.configure({})
) or if you want to declare the
styles directly in the head
tag.
Particularly, because the link
tags which will be generated
according the configuration have no any attributes except
href
and rel="stylesheet"
, so if you need other
attributes, add the link
tags inside
Styles
block manually:
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append Styles
link(
rel="stylesheet"
href="/path/to/style.css"
type="text/css"
media="screen"
crossorigin="anonymous"
integrity="sha384-oqVuAfXRKap7fdgcCY5hULvJ0GhBGDq4IcF3J08z9QY9l03VvH3p0EOTr4N3kR0o"
)
This way of styles specifying does not conflict with specifying of the styles via configuration, however for the correct cascading of styles it is required to respect the sequence in which CSS rules declared on the page. Because in the CSS, the priority depends on multiple factors it is impossible to say unequivocally that the CSS rules declaration sequence will change the displaying, however the sequence is the one of factors on which final displaying depends.
- Use
prepend
keyword when modifying theStyles
block to add the styles before the ones specified via configuration. - Use
append
keyword when modifying theStyles
block to add the styles after the ones specified via configuration.
head
tag.
HeadScripts
Use this block if for some reason it is not enough to specify the scripts via
(RegularWebPageTemplate__YDF.configure({})
) or if you want to declare the
scripts directly in the head
tag.
Particularly, because the script
tags which will be generated
according the configuration have no any attributes except
src
, so if you need other attributes, add the
script
tags inside HeadScripts
block manually:
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append HeadScripts
script(
src="/path/to/index.js"
type="module"
async
defer
crossorigin="anonymous"
integrity="sha384-oqVuAfXRKap7fdgcCY5hULvJ0GhBGDq4IcF3J08z9QY9l03VvH3p0EOTr4N3kR0o"
referrerpolicy="no-referrer"
nonce="randomString"
)
Basically it is recommended to add the scripts to the end of
body
tag, because the scripts defined previously
could slow down the page rendering.
However, in some cases the scripts must be added exactly to
head
tag, especially if the are the third-party scripts for the
analytics such as the Google Analytics:
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append HeadScripts
script(
async
src="https://www.googletagmanager.com/gtag/js?id=G-F3D8Is3NGR4S56G4EBS"
)
script.
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-S3L0PQFKMV");
This way of scripts specifying does not conflict with specifying of the scripts via configuration, and if the scripts independent of each other (in modern times it is usually such as but not in legacy sites and web applications), these methods could be combined.
- Use
prepend
keyword when modifying theHeadScripts
block to add the scripts before the ones specified via configuration. - Use
append
keyword when modifying theHeadScripts
block to add the scripts after the ones specified via configuration.
head
tag.
PageContent
body
тэга.Add the visible content of this page to this block.
Herewith, the body
will be the direct parent of the generated
HTML code.
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append PageContent
header.TopPage-Header
//- ...
main
//- ...
footer
//- ...
EndBodyContent
According to some methodologies, the elements with the fixed positioning such as the
modal dialogs should be placed after all
other elements, but of course, still inside the
body
tag.
If you are following this methodology, use EndBodyContent
block.
The content of this block will be placed direct after
the content of PageContent
block.
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append PageContent
//- ...
block append EndBodyContent
+ContactRequestPage-ModalDialog
EndBodyScripts
Use this block if for some reason it is not enough to specify the scripts via
(RegularWebPageTemplate__YDF.configure({})
) or if you want to declare the
scripts directly in the body
tag.
Particularly, because the script
tags which will be generated
according the configuration have no any attributes except
src
, so if you need other attributes, add the
script
tag inside EndBodyScripts
block manually:
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append PageContent
// ...
block append EndBodyScripts
script(
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"
integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
crossorigin="anonymous"
)
This way of scripts specifying does not conflict with specifying of the scripts via configuration, and if the scripts independent of each other (in modern times it is usually such as but not in legacy sites and web applications), these methods could be combined.
- Use
prepend
keyword when modifying theEndBodyScripts
block to add the scripts before the ones specified via configuration. - Use
append
keyword when modifying theEndBodyScripts
block to add the scripts after the ones specified via configuration.
Multi-level Inheritance
If your project has multiple pages, herewith most of them has the common part (for example, shared header,
footer and sidebar), then it is meaningful to extend Pug files of the pages from the
interim page templates containing the shared markup,
not directly from the RegularWebPageTemplate
.
Usually there are the main template, admin panel template, authentication template etc.
The files which intended to be extended for creating of the complete HTML pages are frequently being called "layouts". However, the "layout" term is too wide what could cause the confusion.
The "layout" term is frequently being used relation to any structurally grouped combination of the elements (not necessary the HTML elements, it could be the elements from any technologies for the GUI development even if the ones not related directly with the web development such as Windows Presentation Foundation or JavaFX). To reduce the ambiguity, it is recommended to use the "page layout" term in relation to the files from which it is intended to extend the complete HTML pages instead of ambiguous "layout".
Below, the page template including single-column page layout has been represented.
To embed the page-specific content to this page layout, new
SpecificContent
pug block has been defined:
extends ../../node_modules/@yamato-daiwa/frontend/PagesTemplates/RegularWebPage.pug
block append PageContent
.MainLayout
.MainLayout-UpperFixedContentSlot
+Header
+NavigationBar
main.MainLayout-SpecificContent
block SpecificContent
+Footer
Now, it is possible to extend other pages from this page template:
extends ../Layouts/OneColumnLayout.pug
block append SpecificContent
h1 Products