plugdo-view-engine
This module is a view engine for express. It render view components of plugdo EUI JS Framework from server side.
npm install plugdo-view-engine --save
The following configuration must be in place before you start using it.
- Include the module to your main index.js
- Register the new view engine to express or to the plugdo-mvc web server property
- Register the path of the view component files
// Register the module
const { pve } = require("plugdo-view-engine");
// Using plugdo-mvc
const mvc = require("@dellasera/plugdo-mvc").mvc();
// Helper for path handling
const path = require("path");
// Register the view engine
mvc.webserver.engine('pve', pve.render);
// Register the path of the view component that you will create
pve.setPath(path.resolve(__dirname) + "/content/component");
// Start the plugdo mvc server
mvc.start(3000, path.resolve(__dirname));
Now you can add your files with the extension PVE, and the Express will apply this view engine.
Let's see an example for Static Component
Create a view component file within the component folder, and name it plugdo-title.html. The name of the file must be the same of the view component, just add the html extension. Always add the .html extension to the components.
note: for more information about the view component, go to the documentation in Plugdo View Component
Component plugdo-title.html
<plugdo-title>
<template>
<h1>Hello World</h1>
</template>
</plugdo-title>
Web page to be rendered index.pve
<html>
<head>
<title>First Demo</title>
</head>
<body>
<plugdo-title id="title"></plugdo-title>
</body>
</html>
Let's understand what is going to happend:
- Express with detect the extension .pve and it will apply this view engine module for rendering
- This module will load the .pve file and It will collect each plugdo-* component
- Using the names collected, this module will check if the file exists within the component folder
- This module will process the view component render process, replacing the plugdo-* component with the final html result
- The html is returned to the express buffer
Output to the Web Browser
<html>
<head>
<title>First Demo</title>
</head>
<body>
<div id="title">
<h1>Hello World</h1>
</div>
</body>
</html>
Let's see an example for Dynamic Component
The view component also accept a model to be used for dynamic render. Let's see the title will be dynamic in the following template.
Component plugdo-title.html
<plugdo-title>
<template>
<h1>[json:title]</h1>
</template>
</plugdo-title>
Web page to be rendered index.pve
<html>
<head>
<title>First Demo</title>
</head>
<body>
<plugdo-title id="componentID"></plugdo-title>
</body>
</html>
Now you must pass a model to the view component following this instructions:
- The model structure is static
- The model must provide a property named component
- The name of the view component is the container of data within the component property
- Each view component will also include the id assigned to the view component
- At the end, a static hierarchy is created.
model = {
component: { // Immutable
"plugdo-title": { // view component
"componentID": { // id of the view component
title: "Hello World"
}
}
}
}
Let's pass the model using a controller of our plugdo-mvc module.
To get more information about our MVC framework go to Plugdo MVC
mvc.controller({
name: "home",
action: "index",
path: "",
view: "home/index.pve"
}, function (req, res) {
model = {
component: { // Immutable
"plugdo-title": { // view component
"componentID": { // id of the view component
title: "Hello World"
}
}
}
}
res(model);
});
Let's understand what is going to happen:
- If the model is undefined, nothing will be passed to the view engine
- If the view component exists and the id also exists, the model will be passed to the view component for rendering
We know, this module is not a dynamic solution, but It is matter of time to get use to it, and replicate always the same. The advantage for us is that we use the same view component in the front-end for ajax architecture, or in the server-side for post back architecture.
The big limitation for server-side is that the view component does not provide a state in memory for later use and it will never be reloaded as a view component in the front-end.