Php simple template engine

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Simple PHP Template Engine based on Alpine.js

License

gregflln/ski-php

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

🏔️ Ski Template Engine (PHP+AlpineJS)

Ski Engine is a framework agnostic template engine that works with Alpine JS

Mock your next data driven application quickly with components.

x-app> x-article> h1>Posts :h1> template x-for pl-s">post in posts"> x-post> template> x-article> x-app>

Alpine JS is an quite simple and very intuitive Javascript framework inspired by Vue JS which can be used directly in HTML from a CDN.

The best way to start a Ski project is using composer

composer require ski/ski-php

Create the following project file structure.

Directories structure: ---------------------- / |___ components/ |___ templates/ |___ head.php 

In your php that instanciate ski, you need to specifies :

 require 'vendor/autoload.php'; use Core\Ski;

That’s all folks. Ski Engine is now ready to use !

This file can be your index.php or your router for example. This is how to render a view

require 'vendor/autoload.php'; Use Core\Ski; $ski = new Ski(__DIR__."/path/to/mySkiPath"); // Without "/" at end $datas = [..]; // retrieve datas as JSON, Array or object $ski->template('templateName'); //add template that you need $ski->data($datas); //add datas to Alpine to front $ski->render(); //And just ⚡️ !

📑 $ski->template() Choose template

Define which template will be rendered.

//example : $ski->template('articleTemplate'); // set template in /templates/articleTemplate.php

☁️ $ski->data() Add some datas OPTIONAL

Pass datas from your backend, DB, Models or whatever to the view.

//example : $datas = Http::get('/api/v1/users')->getJson(); $ski->data($datas); //$datas = //$datas = ['posts' => [. ]] //$datas = $posts[42]->content->. 

The $datas variable can be a JSON Object, PHP Object or an Array.

Datas are passed to AlpineJS in x-data tag in as Javascript Object datas

div x-data pl-s">datas"> div v-for pl-s">post in datas.posts"> template> h3 x-text pl-s">post.username">h3> p x-text pl-s">post.body">p> template> div> div>

⚡️ $ski->render() Build view and send it

The ->render() method will send the view to the browser, this methods must be called in last.

Previous methods can be called in any order.

🏔️ Create better UI simpler with components

With Ski Engine, you create templates by using components. Components are little piece of code that you can manipulate in differents way. Components system are processed by PHP and components behavior are defined with AlpineJS directives.

To create new component, create new file in components/ directory. The filename will be used as component tag name

Let’s create a simple comment component :

 div class pl-s">comment-card"> h3 x-text pl-s">post.userName">h3> p x-text pl-s">post.content">p> div>

Let’s create another component article :

 article class pl-s">article"> h2 x-text pl-s">datas.article.title">h2> p x-text pl-s">datas.article.content">p> div class pl-s">comments_Section"> h3>Comments:h3> x-slot>x-slot> div> article>

To build templates, you just have to assemble your components. For call components, use the following syntax represents our component.

 x-article> template x-for pl-s">post in datas.posts"> x-comment/> template> x-article>

The components names can only contains letters, and the word template is reserved and can’t be used. Tags are case insensitive : make reference to same «app.php» components file.

should be called in templates and not in components itself, is just a tip.

This file populate in every rendered HTML document by Ski. Here add your stylesheets or your favorite CSS frameworks CDN.

meta charset='utf-8'> script defer src pl-s">https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js">script>

Be sure Alpine JS CDN is linked

Integration with some frameworks

NAMM : Détailler comment implémenter Ski avec si possible avec Laravel, lumen, leaf, Flight,

Ski-Engine is an Open Source project, every contribution are welcomes. Some points needs attention :

  • Improve XSS protection
  • Add global variables system
  • Add Configuration File
  • Better management of head tag

At this early point of developpement, use Ski-Engine in production is clearly not recommanded.

THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This project is not affiliate with Alpine JS (Thanks to Caleb Porzio for this really cool works on AlpineJS)

About

Simple PHP Template Engine based on Alpine.js

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A simple PHP templating engine using native PHP syntax.

ddycai/simple-template-engine

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A simple and lightweight PHP templating engine using pure PHP syntax. Simple Template Engine adds on to PHP’s templating capabilities by introducing blocks and template inheritance.

It’s easy to learn and is useful for small websites or in conjunction with microframeworks.

To use the template engine, include loader.php , create an Environment object, and render away! The Environment’s render() function takes the path to a template, renders it and returns its contents as a string.

//include the loader require_once 'path/to/loader.php'; $env = new SimpleTemplateEngine\Environment('path/to/templates/directory'); echo $env->render('template.php');

You can also pass in an extension that will be appended to all template paths in Environment.

$env = new SimpleTemplateEngine\Environment('path/to/templates', '.php'); //will render index.php echo $env->render('index');

You can pass variables to your template via an array:

//index.php echo $env->render('template.php', ['name'=>$value, 'fruit'=>'banana']);

You can then access the variable $fruit in your template, and its value will be apple.

//template.php My favourite fruit is  echo $fruit ?>.

The Environment can hold variables shared by all your templates such as helpers. Set variables like this:

$env->helper = new Helper(); $env->colour pl-s">green";

Now, in your template, you can use your Helper object and your colour variable.

//inside template.php My favourite colour is  echo $this->colour ?>.  echo $this->helper->doSomething() ?>

Blocks are sections of layout that you can define and then use later. You can define blocks by enclosing text in $this->block(‘name here’) and $this->endblock() :

 $this->block('title') ?> Welcome to my site!  $this->endblock() ?>  $this->block('title', 'Welcome to my site!'); //shortcut for small blocks

This will create a Block object that you can access later through $this by using their name. For example, to output the block defined above:

You can also use if structures to set a default block to use if a block is not defined:

  if(!$this['title']): ?> Default Title  else: echo $this['title']; endif; ?> title>

You can escape blocks of output easily:

echo $this['title']->escape(); // OR this shorthand notation echo $this['title']->e();

The function endblock returns a Block object that you can output. This is useful for escaping a Block immediately after ending it by calling escape() :

 $this->block() ?> script>alert('I am dangerous code!');script>  echo $this->endblock()->escape() ?>

As you can see above, we didn’t assign a name to our block because we output it right away, and have no need to save it. If you don’t assign a name to your block, it cannot be accessed later.

You can pass a closure to endblock that you want to apply to the content.

 $this->block() ?> Hello, this is a block of text.  echo $this->endblock(function($content) < return strtoupper($content); >); ?>

The above code converts a block’s content to uppercase.

Blocks are useful because we can define blocks in one template, then extend another one and use it there! This allows us to reuse a template such as a layout multiple times with different blocks. Extending a template is done using the extend function:

 $this->extend('layout.php'); ?>  $this->block('title', 'My Awesome Page') ?>  $this->block('scripts') ?> script src pl-s">jquery.js">script>  $this->endblock() ?> This is my content.

When you extend a parent template, any non-block code in the child will become a special block named content in the parent. In the above code, we defined a title block and some content. Now we can use it in our extended layout. In layout.php, we can output our content and title with $this[‘content’] and $this[‘title’] .

   $this['title'] ?>title> $this['scripts'] ?> head> body> $this['content'] ?> body> html>

Please be careful not to name a block content unless you are intentionally defining a content block. If you define a content block, it will be prepended to the non-block content in the layout.

A template that extends a template that extends a template: If the parent template is extending another template, then any non-block content in the parent will be appended to the content block of their child. This new content block will become the content block of the grandparent.

Templates can also be created without having to render from file and their blocks can be defined within PHP. You can create a new template object through the template() function of Environment.

$template = $env->template(); $template->extend('layout.php'); $template['content'] pl-s">hello"; echo $template->render();

Remember that a Template object created this way must have a layout otherwise nothing will be rendered.

Now you have everything you need to create a great website!

About

A simple PHP templating engine using native PHP syntax.

Источник

Читайте также:  Методы интерфейса set java
Оцените статью