1.0.5 • Published 2 years ago

ejs-element v1.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

EJS Element

usage

From your terminal:

npm install ejs-element --save

In your script:

var ejsElem = require('ejs-element') 
ejsElem.init('my-crate', {
  payload : ['apples', 'oranges', 'raisins'], 
  ready : true
})
ejsElem.render('my-crate', state)

In your HTML page:

<my-crate>
  <h1 class="<?= state.ready ? 'blue' : 'red' ?>">Crate contents:</h4>
  <ul>
    <? state.payload.forEach( function(item) { ?>
      <li><?= item ?></li>
    <? }) ?>
  </ul>
  <? if(state.ready) { ?>
    <h2>READY</h2>
  <? } ?>
</my-crate>

alternatively, render without DOM :

var ejsElem = require('ejs-element')
var message = 'hello world'
var html = `
<p><?= state.message ?></p>
`
var renderedElem = ejsElem.render(html, { message : message })

api

init(elementNameOrHTMLstring, state, render)

ejsElem.init('element-name', { victory : true })

Finds your element (ie- 'element-name') in the DOM, parses it for EJS, and stores it in ejsElem.elems array. Optionally provide an initial state for the element, the properties of which are stored on the element in that array and used as default state for future renders (ie- so that providing an explicit state with each render is not required). Pass true as third param to immediately render.

render(elementNameOrHTMLstring, state*)

ejsElem.render('element-name', { color : 'orange' })

Render the element's EJS. Provide optional state; the properties of which will be assigned as children of a 'state' object you will need to reference to access them ex: <h1 class="<?= state.color ?>GO</?>

if an HTML string (containing EJS) is provided instead of an element name, the function simply returns the rendered output.

renderAll(state*)

ejsElem.renderAll({ everything : 'awesome' , over : 9000 })

Render all initialized ejs elements with the optional state provided (shortcut for calling .render on them all)

MIT

1.0.5

2 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.3

6 years ago