Qiq templates for PHP 8.

Related tags

Templating qiq
Overview

Qiq Templates for PHP 8

This package provides a PHP 8.0 implementation of the TemplateView and TwoStepView patterns using PHP itself as the templating language, along with an optional {{ ... }} syntax for concise escaping and helper use.

Background

I don't like compiled templates or specialized template languages. Smarty, Twig, etc. are all just too heavy-handed. I don't need a new language, and I don't need to "secure" my templates against designers on my team. I am generally happy with plain PHP as a template language.

However, I do find escaping tedious -- necessary, and easy enough, but tedious. A template helper to output escaped HTML, like this ...

<?= $this->h($var) ?>

... is not that bad -- but even so, it could be a little easier. Imagine this little bit of syntax sugar:

{{h $var }}

All that happens is that {{h ... }} is replaced with <?= $this->h(...) ?>.

Once that is in place, it becomes easy to support helpers, control structures, and other code, all while keeping native PHP as the fallback syntax, because the {{ ... }} tags are essentially stand-ins for PHP tags.

Qiq really is PHP -- just with some syntax sugar when you want it.

History

Qiq's relatives include ...

This package is more closely related to Aura.View than to the pre-Composer versions of Savant, but does reintroduce the Savant compiler hook ideas.

Comments
  • Is there a way you can turn off the compiler

    Is there a way you can turn off the compiler

    Basically the {{ is used by some other front-end systems like vuejs.

    I know we can escape the {{, but in case I don't need the sytax itself.

    Do you think it is a good idea ? I have looked into the code, but seems the current compiler is hard coded inside the TemplateLocator .

    opened by harikt 4
  • Undefined variable $name but $this->name work

    Undefined variable $name but $this->name work

    Hi ! Nice and interesting lib :) I try to use Qiq but I think there is a little problem : {{= $this->name}} work but not {{= $name}} in template (Undefined variable $name).

    You can easily reproduce this behavior modifying the tests/templates/index.php with {{= hello($name)}}. It work when I use the render() method because I pass string $__NAME__, array $__VARS__ = [] parameters by hand ( $actual = ($this->template)->render('index', ['name' => 'Index']);) but not with the __invoke because $__VARS__ parameter is not pass in render().

    I propose something like this to resolve this problem :

    // in src/TemplateCore.php
      public function __invoke() : string
      {
          $view = $this->getView();
          $data = (array) $this->getData();
          $this->content = ($view === null) ? '' : $this->render($view, $data);
          $layout = $this->getLayout();
    
          if ($layout === null) {
              return $this->content;
          }
    
          return $this->render($layout, $data);
      }
    

    Alternative can be to merge $__VARS__ with $this->getData() in Template#render() method.

    I can make a PR if you want !

    opened by yoh 3
  • Static analysis proof-of-concept

    Static analysis proof-of-concept

    @koriym @harikt and anyone else interested:

    ~~This branch https://github.com/qiqphp/qiq/tree/1.x-stan-poc is a proof-of-concept for enabling static analysis on Qiq template files.~~ Here is a more limited/restricted POC branch, with only the Container and assigned-variable differences. https://github.com/qiqphp/qiq/tree/1.x-stan-poc-limited

    ~~The diff is at https://github.com/qiqphp/qiq/compare/1.x-stan-poc?expand=1~~ Limited/restricted diff here https://github.com/qiqphp/qiq/compare/1.x-stan-poc-limited?expand=1

    It solves a couple of problems raised by @koriym and others, namely that of helper-object creation through containers, and of template analysis. It is a substantial BC break and will warrant a 2.x release if we go with it.

    tl;dr:

    • HelperLocator is now a ContainerInterface implementation
    • You don't register helpers, you create traits for them, and use those traits inside a Template class of your own
    • Assigned variables are $foo and not $this->foo
    • Use docblocks in the template file to enable static analysis

    Look to the README-STAN.md file for detailed information.

    Please let me know what you think, if you have alternative implementation ideas, if it's dumb and everything is fine as it is now, etc.

    Thanks, and Happy New Year!

    opened by pmjones 4
  • Question : Any reason render is abstract inside the TemplateCore

    Question : Any reason render is abstract inside the TemplateCore

    Can we move the render method from Template class to TemplateCore ?

    Any specific reason for not keeping it inside TemplateCore .

    This probably is also related to discussion https://github.com/qiqphp/qiq/issues/8 different compiler, passing additional factories to helperlocator if https://github.com/qiqphp/qiq/pull/9 is merged etc.

    opened by harikt 2
  • Enable autoloading of Helper class

    Enable autoloading of Helper class

    This PR will allow helper factories that are currently pre-registered all helper with the service locator to be registered with autoload on demand.

    Users can use the helpers by simply creating their own helper files without touching any code for initialization.

    This would be useful, for example, when using the same set of custom helpers in several applications.

    <?php
    namespace Qiq\Helper;
    
    class MyRot13 extends Helper
    {
        public function __invoke(string $input) : string
        {
            // ...
           return $output;
        }
    }
    

    ( We need to set the autoload in composer.json so that this class will be autoloaded properly in the user application.)

    opened by koriym 8
Releases(1.0.2)
Owner
null
PHP template engine for native PHP templates

FOIL PHP template engine, for PHP templates. Foil brings all the flexibility and power of modern template engines to native PHP templates. Write simpl

Foil PHP 167 Dec 3, 2022
Simple PHP templating system for user editable templates.

Simple template Simple PHP templating system for user editable templates. Idea Most applications need to render templates that insert safely treated v

Baraja packages 1 Jan 23, 2022
Foil brings all the flexibility and power of modern template engines to native PHP templates

Foil brings all the flexibility and power of modern template engines to native PHP templates. Write simple, clean and concise templates with nothing more than PHP.

Foil PHP 167 Dec 3, 2022
Contao extension to provide content templates for pages.

Contao Content Templates In Contao, the regular content of a page can be made up of different articles, each assigned to different sections of a page

inspiredminds 7 Oct 11, 2022
Renders Mithril components to HTML for use in blade templates

Flarum Mithril2Html Uses Chrome Puppeteer via Spatie Browsershot to render Mithril components as static HTML. Follow Browsershot instructions to setup

Clark Winkelmann 3 Nov 13, 2022
Document templates Laravel package is intended for creating/managing user editable document template

Document Templates Introduction Document templates Laravel package is intended for creating/managing user editable document templates, with ability to

42coders 139 Dec 15, 2022
Laravel Live Templates for PhpStorm

Laravel Live Templates for PhpStorm How to: Go to Preferences | Tools | Settings Repository Add Read-only Source https://github.com/koomai/phpstorm-la

Sid 1.2k Dec 22, 2022
Yii2 Gii Extended templates and generators

model template with TimestampBehavior and BlameableBehavior according to columns

潘文斌 1 Feb 12, 2020
A PHP project template with PHP 8.1, Laminas Framework and Doctrine

A PHP project template with PHP 8.1, Laminas Framework and Doctrine

Henrik Thesing 3 Mar 8, 2022
Twig, the flexible, fast, and secure template language for PHP

Twig, the flexible, fast, and secure template language for PHP Twig is a template language for PHP, released under the new BSD license (code and docum

Twig 7.7k Jan 1, 2023
A Mustache implementation in PHP.

Mustache.php A Mustache implementation in PHP. Usage A quick example: <?php $m = new Mustache_Engine(array('entity_flags' => ENT_QUOTES)); echo $m->re

Justin Hileman 3.2k Dec 24, 2022
Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.

Smarty 3 template engine smarty.net Documentation For documentation see www.smarty.net/docs/en/ Requirements Smarty can be run with PHP 5.2 to PHP 7.4

Smarty PHP Template Engine 2.1k Jan 1, 2023
Native PHP template system

Plates Plates is a native PHP template system that's fast, easy to use and easy to extend. It's inspired by the excellent Twig template engine and str

The League of Extraordinary Packages 1.3k Jan 7, 2023
☕ Latte: the intuitive and fast template engine for those who want the most secure PHP sites.

Latte: amazing template engine for PHP Introduction Latte is a template engine for PHP which eases your work and ensures the output is protected again

Nette Foundation 898 Dec 25, 2022
Multi target HAML (HAML for PHP, Twig, )

Multi target HAML MtHaml is a PHP implementation of the HAML language which can target multiple languages. Currently supported targets are PHP and Twi

Arnaud Le Blanc 363 Nov 21, 2022
PHP Template Attribute Language — template engine for XSS-proof well-formed XHTML and HTML5 pages

PHPTAL - Template Attribute Language for PHP Requirements If you want to use the builtin internationalisation system (I18N), the php-gettext extension

PHPTAL 175 Dec 13, 2022
View template engine of PHP extracted from Laravel

Blade 【简体中文】 This is a view templating engine which is extracted from Laravel. It's independent without relying on Laravel's Container or any others.

刘小乐 143 Dec 13, 2022
PHP 5.3 Mustache implementation

Phly\Mustache MOVED! This package has moved to phly/phly-mustache, and the package name has changed to phly/phly-mustache. I have updated packagist to

phly 123 Jan 11, 2022
Provides TemplateView and TwoStepView using PHP as the templating language, with support for partials, sections, and helpers.

Aura View This package provides an implementation of the TemplateView and TwoStepView patterns using PHP itself as the templating language. It support

Aura for PHP 83 Jan 3, 2023