Skip to content

Getting started with Twig templates thanks to a simple PHP script

License

Notifications You must be signed in to change notification settings

kaliop/twig-express

Repository files navigation

TwigExpress

TwigExpress packages the Twig templating engine, and a few other tools, in a single file.

Our goal is to make it easy to get started with Twig if you’re a designer or a front-end developer, without having to install a heavy PHP framework or CMS.

Main features:

  • Browse and serve files (⚠ only for development!)
  • Render Twig templates
  • Error pages with code excerpt, source view
  • And a few extra tools for prototyping (dummy text, Markdown to HTML…):

Table of contents

  1. Running TwigExpress
  2. Adding content
  3. Writing Twig templates
    Getting Started with Twig
    TwigExpress-specific features
  4. Configuration
    TwigExpress configuration reference
    Using TwigExpress with Apache
  5. Library and license info

Running TwigExpress

Requirements

PHP 5.4+ available on the command line.

  • On macOS, you should have PHP installed already.
  • On Windows, one easy way to install PHP (and other tools) is XAMPP.

Download TwigExpress

Usage

Open your project folder in a Terminal or Command Prompt and run this command:

php --server localhost:8000 twigexpress.phar

Now load http://localhost:8000/ in a web browser to browse your files.

Any file ending in .twig will be interpreted as a Twig template. You can check out the example pages in the demo directory.

Adding content

After installing TwigExpress, you should have a folder whose content looks like this:

myproject/
    demo/
    LICENSE
    README.md
    twigexpress.json
    twigexpress.phar

If you don’t care about the demo and read-me, you can keep these only:

myproject/
    twigexpress.json
    twigexpress.phar

You can add your own content anywhere. This content can be Twig templates (which must have the .twig extension), CSS, scripts, images, etc.

Example file path Corresponding URL
myproject/index.twig http://localhost:8000/
myproject/some/page.twig http://localhost:8000/some/page
myproject/css/styles.css http://localhost:8000/css/styles.css

Writing Twig templates

There are two sets of features available in templates:

Configuration

Configuration goes in a twigexpress.json file at the root of your project. This file should follow the JSON format (here’s a good introduction).

More information:

Library and license info

TwigExpress is licensed under the MIT License.

We are using the following libraries: