-
Notifications
You must be signed in to change notification settings - Fork 6
4 Modules
- A template can import/include many file types:
-
Mask — import components or nodes from other
*.maskfiles. -
Javascript —
CommonJSmodules are used, but via the configuration a javascript module loader can be defined to support any module system. - JSON — load data from files to use it within templates
-
HTML — load html and parse it to
Mask AST, so that it can be used as genericmasknodes. -
Style — load
cssstyles
-
Mask — import components or nodes from other
- A template can export components or nodes.
📦 Use Mask Optimizer to create single style, makrup and script files from many sources.
-
1Export -
2Import-
2.1Path -
2.2Mask module-
2.2.1Embed -
2.2.2Import nodes -
2.2.3Import components
-
-
2.3Javascript module -
2.4Data module -
2.5Style module -
2.6HTML module
-
-
3Loaders-
3.1Mask, Html, Json -
3.2Javascript -
3.3Style
-
There are no keywords and no specific syntax to export things, thats why any template can be rendered as a root template. But when a template is requested as a module, then exported are:
- Components, which are created with
definedirective - Nodes. All generic Mask Nodes.
Basic examples
-
Export generic nodes
// baz.mask h4 #baz > 'Baz'
-
Create and export components
// foo.mask define foo { /* definition */ }
-
Create and export components and nodes
// foo.mask define foo { /* definition */ } define baz { /* definition */ } h4 #qux > 'Lorem ~[name]' h4 #quux > 'Ipsum'
// mask examples
import from 'foo';
import foo from 'foo.mask';
import foo as baz from './foo';
import foo as xxx, baz as yyy from 'foo';
import * as Titles from 'foo.mask';
import * as Titles, foo, baz from 'foo.mask';
import h4#qux as Title from './baz';
// javascript example
import User from '/models/User.js';File extension defines the type of a module. When no extension is provided then mask extension will be automatically added.
-
Mask:
maskor empty file extension -
Javascript:
js,es6,coffee -
Style:
css,sass,less -
Html:
html -
Data:
json
In NodeJS this is process.cwd(). In Browser this is the baseURI of the document. You can also set custom root path:
-
via Javascript:
mask.Module.cfg('base', EXPRESSION); mask.Module.cfg('base', 'foo/'); // e.g
-
via Mask
import:base (EXPRESSION); import:base ('foo/'); // e.g
/path - is an absolute path and is concatenated with a root path to get the file path.
path, ./path - are relative paths to the parents module location or parents component location. But if the template is the root module, then the context is checked for filename/dirname properties, if they are not set, then the root path is used.
Setting the current templates location via context object:
mask.render(template, module, {
dirname: '/foo/baz/'
})For simple cases you can just embed everything from a module into the requested template. Embedding means the imported template is rendered in-place.
import from 'foo';-
Import all nodes as alias
import * as XXX from 'foo'; // now `XXX` can be used as normal tag name within the template XXX; //e.g: use `XXX` nodes to render each item in `foos` array in the model ul > each (foos) > li > XXX;
-
Use css selector to get specific node for an import
filtermethod is used to get the node, not thefind, so it should be the root node in the set.import h4#qux as lorem from 'foo'; lorem;
-
By defined name
import foo from 'foo'; ul > each (foos) > li > foo;
-
By defined name with alias
import foo as FOO from 'foo'; ul > each (foos) > li > FOO;
Imported object is set to the current controllers scope. So it can be accessed in interpolations and in define extendings.
-
Scope access examples
import * as App from '/configs/application.js'; h4 > '~[App.name]'
import name from '/configs/application.js'; h4 > '~[name]'
-
Component definition
import * as Utils from '/helpers/baseutils.js'; define foo extends Utils { /* definition */ }
Similar to Javascript
{
"application": "Foo App"
}import * as Config from 'config.json';
h4 > '~[Config.application]'Speaking about the UI it is always needed for a component to load stylesheets. Import can load them for you.
#foo { background: red; }import from 'foo.css';
section #foo > 'Lorem ipsum' // has then red background colorSimilar to *.mask HTML is loaded and parsed to Mask AST
Lorem <i>ipsum</i>import * as MyTemplate from 'lorem.html';
section #foo {
MyTemplate;
}fs module is used to load the mask files
XMLHttpRequest is used to load the files
mask.cfg('getFile', function (path) {
// should return Promise or Deferred
// and when ready resolve with the string, or reject with an `Error`.
})❕
JSON.parseis used forjsondata
require is used to load the javascript module
<script src> tag is used to load the module. MaskJS also provides global module object to simulate CommonJS exports.
mask.cfg('getScript', function (path) {
// should return Promise or Deferred
// and when ready resolve with `exported` object, or reject with an `Error`.
})Not loading.
<link href> tag is used.
mask.cfg('getStyle', function(path) {
// should return Promise or Deferred
// and when ready just resolve it
})🏁 If you have any questions, please open an issue or mail us.