Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

twig-layout

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twig-layout

A layout system build on top of twig js

  • 1.0.0-alpha.1.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

twig-layout

! In Dev dont use this !

twig-layout is a layout system based on twig.

Instalation

$ npm install twig-layout

Exemple:

This exemple use express and express-twig-layout

install express and express-twig-layout

$ npm install --save express 
$ npm install --save express-twig-layout

index.js

Create a simple express app with two route: /home and /test

const express = require('express')
const layout = require('express-twig-layout')
const app = express()

//set the views directory
app.set('view', './views')
app.use(layout({
  extendFilter: {},
  extendFunction:{}
}))

//home route
app.get('/home', async (req, res) => {
  //load the layout from the file home.html
  await req.layout.loadTemplate('home.html')
    //send the layout html
  const html = await req.layout.render()
  res.send(html)
})

//test route
app.get('/test', function (req, res) {
  //load the layout from the file test.html
  await req.layout.loadTemplate('test.html')
  //Set the title of the block head
  req.layout.getBlock('head').data.title = 'Test page'
  //send the layout html
  const html = await req.layout.render()
  res.send(html)
})

app.get('*', function (req, res) {
  res.redirect('/home')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

/views/page/default.html

The template for the page For the template syntax read the twig js documentation

<template>
    <!doctype html>
    <html lang="en">
    <!-- block head -->
    {{ getBlockHtml('head') }}
    <body>

    <header>
        <nav>
            <ul>
                <li>
                    <a href="/home">Home</a>
                </li>
                <li>
                    <a href="/test">Test</a>
                </li>
            </ul>
        </nav>
    </header>
    <main role="main">
      <h1>{{ this.getSomething() }}</h1>
      <!-- block content -->
      {{ getBlockHtml('content') }}
      <!-- /block content -->
    </main>

    <footer>
        <p>footer</p>
    </footer>
    </body>
    </html>
</template>
<script>
  //Require the block dependency
  const Block = require('node-twig-layout/block')

  //Block for the page
  class Default extends Block {
    async init () {
      //set the name of the block
      //the name of the block can be define in this way or for other block it can be defined in the config
      this.name = 'page'

      //Head block
      this.addBlock({name: 'head', template: 'page/head.html'})

      //content block it just a block container
      //to use block with no html temple use type
      this.addBlock({name: 'content', script: 'twig-layout/scripts/container'})
    }

    /**
     * A method called in the template 
     */
    async getSomething() {
      return 'something';
    }

    /**
     * before render callback
     */
    async beforeRender () {
      //Add a css file
      this.layout.getBlock('head').addCss('https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css', -10)
    }
  }

  module.exports = Default
</script>

/views/page/head.html

template for the head block define in the file views/page/default.html

<template>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>{{title}}</title>
        {% for file in css %}
        <link rel="stylesheet" href="{{file}}">
        {% endfor %}

        {% for file in js %}
        <script src="{{file}}"></script>
        {% endfor %}
    </head>
</template>
<script>
  //requite the block object
  const Block = require('node-twig-layout/block')

  //A Test class for the test page
  class Head extends Block {
    /**
     * Init method
     */
    async init() {
      //unsorted array
      this._css = []
      this._js = []

      //data array for render
      this.data.css = []
      this.data.js = []
    }

    //add css files
    async addCss (cssFiles, weight = 0) {
      if (Array.isArray(cssFiles)) {
        for (let key in cssFiles) {
          this._css.push({weight: weight, file: cssFiles[key]})
        }
      } else if (typeof cssFiles === 'string') {
        this._css.push({weight: weight, file: cssFiles})
      } else {
        throw Error('Invalid addCss argument')
      }
    }

    //add js files to the data object
    async addJs (jsFiles) {
      if (Array.isArray(jsFiles)) {
        for (let key in jsFiles) {
          this._js.push({weight: weight, file: jsFiles[key]})
        }
      } else if (typeof jsFiles === 'string') {
        this._js.push({weight: weight, file: jsFiles})
      } else {
        throw Error('Invalid addJs argument')
      }
    }

    /**
     * Before render callback
     */
    async beforeRender() {
      const sort = function(a, b) {
        return a.weight - b.weight
      }
      this._css.sort(sort);
      for (const key in this._css)
        this.data.css.push(this._css[key].file)

      this._js.sort(sort);
      for (const key in this._js)
        this.data.js.push(this._js[key].file)
    }
  }

  module.exports = Head
</script>

/views/home.html

The template for the /home route

<template>
    <div>
        <h1>Home page</h1>
    </div>
</template>
<script>
  //requite the block object
  const Block = require('node-twig-layout/block')

  //A Block class for the home page
  class Home extends Block {
    async init () {
      this.page ='page/default.html'
      //name of the parent block of this block
      //here the block content, it is defined in the file page/default.html
      this.parent = 'content'
    }

    beforeRender() {
      this.layout.getBlock('head').data.title = 'Home page'
    }
  }

  module.exports = Home
</script>

/views/test.html

The template for the /test route

<template>
    <div class="test">
        <h1>Test</h1>
    </div>
</template>
<script>
  //requite the block object
  const Block = require('node-twig-layout/block')

  //A Test class for the test page
  class Test extends Block {
    init () {
      this.page ='page/default.html'
      //name of the parent block of this block
      //here the block content, it is defined in the file page/default.html
      this.parent= 'content'
    }
  }

  module.exports = Test
</script>

Run:

$ node index.js

Keywords

FAQs

Package last updated on 21 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc