Coder Social home page Coder Social logo

angular-twig's Introduction

Angular2 ♥ Twig

This (WIP...) experiment is an attempt to add Twig templating support for Angular v2+

Install

yarn add @manekinekko/angular-twig

or

npm i -S @manekinekko/angular-twig

Setup with Angular-cli

Import the twig library into your .angular-cli.json file:

"scripts": [
		"../node_modules/twig/twig.min.js"
	],

How to use

import { Twig } from '@manekinekko/angular-twig';

The @Twig decorator supersedes the @Component() decorator. Don't use both!

Use a templateUrl

@Twig({
	templateUrl: 'templates/field--comment.html.twig',
  // -or- template: '',
	context: {
		title: 'Angular2 ❤ Twig',
		content_1: 'content 1',
		content_2: 'content 2'
	},
	selector: '#block-content',
})
export class AppComponent {}

Use an inlined template

@Twig({
  template: `
  <section>
    {% if title %}
      <h2>{{ title }}</h2>
    {% endif %}

    {{ content_1 }}

    {% if content_2 %}
      {{ content_2 }}
    {% endif %}
  </section>
  `,
	context: {
		title: 'Angular2 ❤ Twig',
		content_1: 'content 1',
		content_2: 'content 2'
	},
	selector: '#block-content',
})
export class AppComponent {}

Live Example

Checkout this plnkr for a quick demo.

Acknowledgments

The Twig templates are parsed using (a modified version of) the twig.js library from @justjohn. All credits go to him for the parser.

License

The MIT License

angular-twig's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

angular-twig's Issues

Twig as a service

Hi,

do you have anything to use twig as a service ? I want to parse string that is coming from a file that is downloaded any way to do that ?

I could probably easily use the library you are based on directly but just wondering since you already integrated it in angular2

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.