Skip to content

Commit

Permalink
Merge pull request #10 from namics/feat/update-twig-2
Browse files Browse the repository at this point in the history
Update to Twig 2
  • Loading branch information
orlandothoeny authored Jul 30, 2020
2 parents f4631b5 + 1ce80f3 commit 7908336
Show file tree
Hide file tree
Showing 26 changed files with 1,698 additions and 712 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Drupal editor configuration normalization
# @see http://editorconfig.org/

# This is the top-most .editorconfig file; do not search in parent directories.
root = true

# All files.
[*]
end_of_line = LF
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[composer.{json,lock}]
indent_size = 4
34 changes: 34 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Twig Nitro Library

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php_version: ['7.3', '7.4']

steps:
- name: Setup PHP ${{ matrix.php_version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
tools: composer

- name: Checkout
uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --dev --no-progress --no-suggest

- name: Run tests
run: vendor/bin/phpunit
57 changes: 56 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
.idea
# OSX system files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Git
*.git

# Packages
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases
*.log
*.sql
*.sqlite

# Temporary
tmp
.grunt

# PHPStorm
.idea/
.idea/*

# NPM
node_modules/

# SASS #
########
.sass-cache/
*.css.map

# Composer
composer.phar
composer.lock
vendor/


# PHPUnit Tests
.phpunit.result.cache

# Drush
drush.phar

# Certificates
*.pem
*.crt
*.cert
*.key
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

81 changes: 46 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
# Terrific Twig
[![Build Status](https://travis-ci.org/namics/terrific-twig.svg?branch=master)](https://travis-ci.org/namics/terrific-twig)
[![Latest Stable Version](https://poser.pugx.org/namics/terrific-twig/v/stable.svg)](https://packagist.org/packages/namics/terrific-twig)
[![Total Downloads](https://poser.pugx.org/namics/terrific-twig/downloads.svg)](https://packagist.org/packages/namics/terrific-twig)
[![License](https://poser.pugx.org/namics/terrific-twig/license.svg)](https://packagist.org/packages/namics/terrific-twig)
![Build Status](https://github.com/namics/twig-nitro-library/workflows/workflow/badge.svg)
![Latest version](https://img.shields.io/github/v/release/namics/twig-nitro-library)
![License](https://img.shields.io/github/license/namics/twig-nitro-library)
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/namics/twig-nitro-library?color=%23787CB5)

Extension to embrace the [Terrific](https://github.com/brunschgi/terrificjs) frontend methodology in [Twig](http://twig.sensiolabs.org/).

Currently it adds a custom `component` tag to Twig which mimics [Nitro](https://github.com/namics/generator-nitro)'s handlebars helper.
Adds a custom `component` tag to Twig which mimics the [Nitro](https://github.com/namics/generator-nitro) handlebars helper.

## Installation
Using [composer](https://packagist.org/packages/namics/terrific-twig):

```bash
```shell script
$ composer require namics/terrific-twig
```

## Requirements

The following versions of PHP are currently supported.

+ ~~PHP 5.4~~ (**Deprecated**. Builds are failing since the tests are relying on [`::class`](http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.class).)
+ PHP 5.5
+ PHP 5.6
+ PHP 7
+ HHVM
* 7.3
* 7.4

## Setup
Step 1: Implement `TemplateInformationProvider`

```php
class TemplateInformationProvider implements TemplateInformationProviderInterface
{
public function getPaths()
{
return []; // List of path where Terrific Components can be found, e.g. (/var/www/example.com/frontend/components)
}

public function getFileExtension()
{
$fileExtension = 'html.twig';
return $fileExtension;
public function getPaths() {
/* List of path where Terrific Components can be found, e.g.
@code
[
'/var/www/example.com/frontend/src/atoms',
'/var/www/example.com/frontend/src/molecules',
'/var/www/example.com/frontend/src/organisms'
]
@endcode */
return [];
}
}
```
Expand Down Expand Up @@ -72,28 +69,29 @@ Step 5: Profit!

## Usage
```twig
{# Includes the component, component's default data is merged with the context #}
{% component 'Example' %}
{# Includes the component, the default data is injected as a child context #}
{% component 'Example' only %}
{# Includes the component, but a variantion of the component data is merged with the context #}
{% component 'Example' 'example-variant' %}
{# Includes the component, but a variantion of the component data is injected as a child context #}
{% component 'Example' 'example-variant' only %}
{# Includes the component, data object is merged with the context #}
{% component 'Example' { title: 'Inject an Object' } %}
{# Includes the component, data object is injected as a child context #}
{% component 'Example' { title: 'Inject an Object' } only %}
{# Includes the component, object variable data is merged with the context #}
{% set fooComponentData = { title: 'Inject an Object' } %}
{% component 'Example' fooComponentData %}
{# Includes the component with name contained by string variable, data object is merged with the context #}
{% set fooComponentName = 'Example' %}
{% component fooComponentName { title: 'Inject an Object' } %}
{# Includes the component with name contained by string variable, object variable data is merged with the context #}
{% set fooComponentName = 'Example' %}
{% set fooComponentData = { title: 'Inject an Object' } %}
{% component fooComponentName fooComponentData %}
```

## Documentation
### Extension
The extension provides terrific extensions to Twig. Currently the extension provides the `ComponentTokenParser`.
The extension provides terrific extensions to Twig. Currently, the extension provides the `ComponentTokenParser`.

### Token Parser
The token parser contains the parsing step for the component tag. It tokenizes the stream to different nodes (`component`, `data`) and an attribute (`only`).
Expand All @@ -104,7 +102,7 @@ The functionality is based on the fantastic `Twig_TokenParser_Include`.
The Node compiles the tokenized tag to PHP. To see some of the output, check the [`ComponentTest`](https://github.com/namics/terrific-twig/blob/master/test/Twig/Node/ComponentTest.php).

### Loader
The `TerrificLoader` extends the `Twig_Loader_Filesystem` as it actually loads templates from the filesystem. An implementation of `TemplateLocatorInterface` provides the paths where the loader should search for templates.
The `TerrificLoader` loads templates contained inside the given paths. An implementation of `TemplateLocatorInterface` provides the paths where the loader should search for templates. Recursively loads any directories contained inside the given directories.

### Template Information Provider
An implementation of `TemplateInformationProviderInterface` should return a list of paths where templates live. These should be in the form of `['frontend/components/atoms', 'frontend/components/molecules', 'frontend/components/organisms']`. The component directory will be provided by the `TerrificLoader` (`Example/example.[ext]`).
Expand All @@ -116,6 +114,19 @@ TODO: More on that.
### ConfigReader
Reads nitro's `config.json` and parses essential information such as the component paths and file extension.

### Tests
Tests can be run by using the following command:
```shell script
composer run-scrip tests
```

### CI
This project uses GitHub actions.
#### Run locally
* Install [nektos/act](https://github.com/nektos/act).
* Open terminal, go to project directory.
* Run `act -P ubuntu-latest=shivammathur/node:latest` as described [here](https://github.com/shivammathur/setup-php#local-testing-setup).

## Credits
+ [Twig Template Engine](http://twig.sensiolabs.org/)
+ [Terrific](http://terrifically.org/)
Expand Down
29 changes: 24 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,43 @@
"name": "namics/twig-nitro-library",
"description": "Extension to embrace the Terrific frontend methodology in Twig. Currently it adds a custom component tag to Twig which mimics Nitro's handlebars helper.",
"type": "library",
"keywords": ["terrific", "nitro", "twig", "integration", "namics"],
"keywords": [
"terrific",
"nitro",
"twig",
"integration",
"namics"
],
"homepage": "https://github.com/namics/twig-nitro-library",
"license": "MIT",
"authors": [
{
"name": "Robert Vogt",
"email": "[email protected]",
"homepage": "https://github.com/deniaz"
},
{
"name": "Namics",
"email": "[email protected]",
"homepage": "https://github.com/namics"
}
],
"autoload": {
"psr-4": {
"Deniaz\\Terrific\\": "src/",
"Deniaz\\Test\\Terrific\\": "test/"
"Namics\\Terrific\\": "src/",
"Namics\\Test\\Terrific\\": "test/"
}
},
"require": {
"php": ">=5.4",
"twig/twig": "~1.24"
"php": ">=7.3",
"twig/twig": "^2.13"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
"scripts": {
"tests": [
"vendor/bin/phpunit"
]
}
}
Loading

0 comments on commit 7908336

Please sign in to comment.