Giter Club home page Giter Club logo

slice-library's Introduction

Slice-Library

license

Slice-Library is a CodeIgniter library that simulates Laravel's Blade templating system! Slice-Library is also compatible with Modular Extensions - HMVC; saves the compiled template in cache and is easy to use and install.

Features

  • Requires nearly zero configuration!
  • Easy to install and use.
  • Helps you organize your views folder.
  • 30 directives to use!
  • 89 helper functions (eleven required, kkk)!
  • Does not restrict you from using plain PHP code in your views.
  • Easy to learn and to get used to.
  • Caches files until they are modified!
  • Handles complex language string lines.
  • Provides you clear, thorough documentation.

Requirements

  • PHP version 5.6 or newer is recommended.
  • CodeIgniter version 2.2.1 or newer.

Instalation

To use the Slice-Library you must first copy the file located in application/config/slice.php to your own application/config/ folder. Then, edit this file according to your configurations. Now, copy the file application/libraries/Slice.php to your own application/libraries/ folder. Finally, make sure the folder application/cache/ has a 0664 permission to save the compiled templates the library will produce.

That's all! Have fun!

Loading Slice-Library

Your can load the Slice-Library as you load any other library in CodeIgniter:

$this->load->library('slice');

Slice Helper functions

Since version 1.3.0 Slice-Library comes with a helper file with 89 functions! This functions will help you a lot. Some of the are:

  • app() - Returns any library instance
  • decrypt() - Decrypts a given string
  • e() - Escapes HTML entities in a string
  • email() - Sends an email
  • helper() - Loads any CodeIgniter helper
  • input() - Retrieves input item from the request
  • query() - Executes a query string
  • session() - Gets or sets session values
  • str_contains() - Determines if a given string contains a given substring
  • validator() - Validates post fields with CodeIgniter Form Validation Class
  • view() - Get the evaluated view contents for the given view

In the future a full list of helpers and it's definitions and how to use them will be available.

To use them, make sure the variable enable_helper is set TRUE in the config/slice.php file to load the helpers automatically or load the file as you would do with any CodeIgniter helper.

Other Libraries and Helpers

Within Slice you can optionally load any Library and/or Helper! This is very useful if you use a library and/or a helper to handle assets, for example, and you don't want to put them in the config/autoload.php file.

So, to autoload any library and/or helper set the variable enable_autoload in the config/slice.php file to TRUE. Then add the library you want to load in the config array libraries and the helpers you want to load in the config array helpers.

This way, your libraries and helpers will be loaded automatically!

Creating Views

Slice-Library has its own view method to display your HTML pages. So, to show a view you can do the following:

$this->slice->view('page', ['name' => 'GustMartins']);

It is important to remember that your view files MUST have .slice.php as extension. But you can change that in your application/config/slice.php file.

As you can see, the first argument passed to the view method corresponds to the name of the view file in your views directory. The second argument is an array of data that should be made available to the view. In this case, we are passing the name variable, which is displayed in the view using the syntax explained bellow.

Of course, views may also be nested within sub-directories of you views directory. "Dot" notation may be used to reference nested views. For example:

$this->slice->view('user.profile', $data);

Determining If A View Exists

If you need to determine if a view exists, you may use the exists method. The exists method will return TRUE if the view exists:

$this->slice->exists('user.email');

Passing Data To Views

As you saw in the previous examples, you may pass an array of data to views:

$this->slice->view('view', ['name' => 'GustMartins']);

When passing information in this manner, $data should be an array of key/value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?>. As an alternative to passing a complete array of data to the view method, you may use the with method to add individual pieces of data, or the set method to add an array of key/value pairs of data.

$this->slice->with('name', 'GustMartins')
            ->set(['foo' => 'bar', 'users' => ['Jack', 'Kate', 'Sawyer', 'Lock', 'Jacob']])
            ->view('users');

If you need to append some value to a data you can use the append method:

$this->slice->set(['foo' => 'bar', 'users' => ['Jack', 'Kate', 'Sawyer', 'Lock', 'Jacob']])
            ->append('users', 'Ben')
            ->view('users');

Localization

Slice's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.

If you want to support multiple languages in your application, you would provide folders inside your application/language/ directory for each of them, and you would specify the default language in your application/config/config.php:

  application/
              language/
                      english/
                              error_lang.php
                              message_lang.php
                      french/
                              error_lang.php
                              message_lang.php

Configuring The Locale

The default language for your application is stored in the application/config/config.php file. Of course, you may modify this value to suit the needs of your application. You may also change the active language using the locale method:

$this->slice->locale('french');

Retrieving Translation Strings

You may retrieve lines from language files using the @lang directive. The @lang accepts the file and key of the translation string as its first argument. For example, let's retrieve the welcome translation string from the application/language/english/message_lang.php language file:

@lang('message.welcome')

If the specified translation string does not exist, the @lang directive will simply return the translation string key. So, using the example above, the @lang directive would return 'messages.welcome' if the translation string does not exist.

Replacing Parameters In Translation Strings

If you wish, you may define place-holders in your translation strings. All place-holders are prefixed with a :. For example, you may define a welcome message with a place-holder name:

$lang['welcome'] = 'Welcome, :Name';

To replace the place-holders when retrieving a translation string, pass an array of replacements as the second argument to the @lang directive:

@lang('messages.welcome', ['name' => 'GustMartins'])

If your place-holder contains all capital letters, or only has its first letter capitalized, the translated value will be capitalized accordingly:

$lang['welcome'] = 'Welcome, :NAME';  //  Welcome, GUSTMARTINS
$lang['goodbye'] = 'Goodbye, :Name';  //  Goodbye, Gustmartins

Pluralization

Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. By using a "pipe" character, you may distinguish singular and plural forms of a string:

$lang['apples'] = 'There is one apple|There are many apples';

You may even create more complex pluralization rules which specify translation strings for multiple number ranges:

$lang['apples'] = '{0} There are none|[1,19] There are some|[20,*] There are many';

After defining a translation string that has pluralization options, you may use the @choice directive to retrieve the line for a given "count". In this example, since the count is greater than one, the plural form of the translation string is returned:

@choice('messages.apples', 10)

Slice Syntax

Just like Laravel's Blade, Slice-Library is simple, and powerful! Slice-Library does not restrict you from using plain PHP code in your views. All Slice views are compiled into plain PHP code and cached until they are modified, meaning Slice adds essentially zero overhead to your application. Slice view files use the .slice.php file extension, but you can change it in the application/config/slice.php file.

Defining a Layout

To get started, let's take a look at a simple example. First, we will examine a "master" page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Slice view:

<!-- Stored in application/views/layouts/app.slice.php -->
<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

The @section directive, as the name implies, defines a section of content, while the @yield directive is used to display the contents of a given section.

Extending A Layout

When defining a child view, use the Slice @extends directive to specify which layout the child view should "inherit".

<!-- Stored in application/views/child.slice.php -->

@extends('layouts.app')

@section('title', 'Child Page Title')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

In this example, the sidebar section is utilizing the @parent directive to append (rather than overwriting) content to the layout's sidebar. The @parent directive will be replaced by the content of the layout when the view is rendered.

Now, you just need to load the view with the Slice view method:

$this->slice->view('child');

Displaying Data

You may display data passed to your Slice views by wrapping the variable in curly braces. For example, you may display the contents of a name variable like so:

Hello, {{ $name }}.

Of course, you are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function as well as CodeIgniter loaded library's functions:

Find out more in this {{ anchor('anchor/this', 'link') }}

Echoing Data If It Exists

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. But, instead of writing a ternary statement, Slice provides you with the following convenient shortcut, which will be compiled to the ternary statement:

{{ $name or 'Default' }}

In this example, if the $name variable exists, its value will be displayed. However, if it does not exist, the word Default will be displayed.

Slice & JavaScript Frameworks

Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the @ symbol to inform the Slice-Library rendering engine an expression should remain untouched. For example:

<h1>Slice Library</h1>

Hello, @{{ username }}

In this example, the @ symbol will be removed by Slice; however, {{ username }} expression will remain untouched by the Slice-Library engine, allowing it to instead be rendered by your JavaScript framework.

If Statements

You may construct if statements using the @if, @elseif, @else, and @endif directives. These directives function identically to their PHP counterparts:

@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
@else
    I do not have any records!
@endif

Slice-Library also provides an @unless directive:

@unless (count($users) != 0)
    There are no users to show.
@endunless

In addition to the conditional directives already discussed, you may use the @isset and @empty directives as convenient shortcuts for their respective PHP functions:

@isset($rows)
  //  $rows is defined and is not null...
@endisset

@empty($rows)
  //  $rows is "empty"...
@endempty

Loops

Slice-Library provides simple directives for working with PHP's loop structures. Again, each of these directives functions identically to their PHP counterparts:

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor

@foreach ($users as $key => $user)
    <p>This is user {{ $user }}</p>
@endforeach

@forelse ($users as $key => $user)
    <li>{{ $user }}</li>
@empty
    <p>No users</p>
@endforelse

@while (true)
    <p>I am looping forever.</p>
@endwhile

When using loops you may also end the loop or skip the current iteration:

@foreach ($users as $key => $user)
    @if ($user == 'Kate')
        @continue
    @endif

    <li>{{ $user }}</li>

    @if ($key == 4)
        @break
    @endif
@endforeach

You may also include the condition with the directive declaration in one line:

@foreach ($users as $key => $user)
    @continue($key == 1)

    <li>{{ $user }}</li>

    @break($key == 5)
@endforeach

Comments

Slice-Libray also allows you to define comments in your views. However, unlike HTML comments, Slice comments are not included in the HTML returned by your application:

{{-- This comment will not be present in the rendered HTML --}}

PHP

In some situations, it's useful to embed PHP code into your views. You can use the Slice @php directive to execute a block of plain PHP within your template:

@php
    //
@endphp

Including Sub-Views

Slice's @include directive allows you to include a Slice view from within another view. All variables that are available to the parent view will be made available to the included view:

<div>
    @include('public.errors')

    <form>
        <!-- Form Contents -->
    </form>
</div>

Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:

@include('view.name', ['some' => 'data'])

Of course, if you attempt to @include a view which does not exist, Slice-Library will throw an error. If you would like to include a view that may or may not be present, you should use the @includeIf directive:

@includeIf('view.name', ['some' => 'data'])

Rendering Views For Collections

It is very useful to combine loops and includes into one line and it is possible with Slice's @each directive:

@each('view.name', $cars, 'car')

The first argument is the view partial to render for each element in the array. The second argument is the array you wish to iterate over, while the third argument is the variable name that will be assigned to the current iteration within the view.

In the example, if you are iterating over an array of cars, typically you will want to access each car as a car variable within your view partial.

The key for the current iteration will be available as the key variable within your view partial.

You may also pass a fourth argument to the @each directive. This argument determines the view that will be rendered if the given array is empty.

@each('view.name', $cars, 'car', 'view.empty')

Extending Slice-Library Directives

Slice-Library allows you to define your own custom directives using the directive method. When the Slice compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains.

The following example creates a @slice('Text') directive which echo a given string:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller
{
   public function __construct()
   {
      parent::__construct();
      $this->load->library('slice');
   }

   public function index()
   {
      $this->slice->with('username', 'John Doe')
                  ->directive('Test::custom_slice_directive')
                  ->view('users.profile');
   }

   static function custom_slice_directive($content)
   {
      // Finds for @slice directive
      $pattern = '/(\s*)@slice\s*\((\'.*\')\)/';
      return preg_replace($pattern, '$1<?php echo "$2"; ?>', $content);
   }
}

Contributions

This package was created by Gustavo Martins, but your help is welcome! Things you are welcome to do:

  • Report any bug you may encounter
  • Suggest a feature for the project

For more information about contributing to the project please, read the Contributing Requirements.

Change Log

Currently, the Slice-Library is in the version 1.3.0. See the full Changelog for more details.

slice-library's People

Contributors

gustmartins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

slice-library's Issues

Cant find 'application/config/assets.php'

I downloaded zip 'slice library' files and extracted, Where is 'application/config/assets.php this file located ? I cant find it inside config folder, only file i see is 'slice.php'.

Suggestion: Use this Blade Engine(EFTEC/BladeOne)

Can you make use of this Blade Engine?

It's a one file, Blade Engine, that's being actively maintained. All blade's directive are available on it, and all the dependency of Laravel has been stripped away.

For example, with this engine, we can use @stack & @Push, and @auth is really cool too

Am considering this alternative because some important directives are currently missing, and also because of the way commenting currently is on slice, most especially with multi-line blade comment

Data not cleared in foreach loop

I use slice to send email to my members.
When I add a view as data in foreach loop to send an email the content of first email is added to all others, seems the view data is not cleared.

It's weird because when I load the view normaly (not using slice) and send email it's working well.

Cannot pass object in view

Hi. I'm currently using Illuminate Database ORM for my database. I cant pass the object to the view but when i convert it to array, it works fine. How can I pass object to the view?

This is my warning message

A PHP Error was encountered
Severity: Warning

Message: Illegal offset type

Filename: libraries/Slice.php

Line Number: 314

Backtrace:

File: /home/xijeixhan/Documents/codeigniter-eloquent/application/libraries/Slice.php
Line: 314
Function: _error_handler

File: /home/xijeixhan/Documents/codeigniter-eloquent/application/libraries/Slice.php
Line: 360
Function: set

File: /home/xijeixhan/Documents/codeigniter-eloquent/application/controllers/Welcome.php
Line: 27
Function: view

File: /home/xijeixhan/Documents/codeigniter-eloquent/index.php
Line: 315
Function: require_once

Support for @stack and @push directive

Is your feature request related to a problem? Please describe.
Add support for @stack and @Push directive so we can insert script and style on demand

Describe the solution you'd like
Add support for @stack and @Push directive

Describe alternatives you've considered
How to insert script and style tag on demand

$key not available in @each directive

Describe the bug
When using the @each directive the $key/index isn't available.
The documentation states:
'The key for the current iteration will be available as the key variable within your view partial.' But when you try to use $key in your template it's undefined.

To Reproduce
In your main template:
@each('partial.car', $cars, 'car')
and inside `partial/car.slice.php':
{{$key}}
Expected behavior
print the key/index

Versions (please complete the following information):

  • PHP: 7.2
  • CodeIgniter: 3.1.9
  • Slice: 1.3.0

I want use a variable in @yield..

Describe the bug
I want use a variable in @yield..

To Reproduce
Steps to reproduce the behavior:

  1. There are just three files. (base.slice.php, content.slice.php, index.php)
    in base.slice.php
    @yield('title')
    in content.slice.php
    @section('title', '{{ $sub_title }}')
    in index.php
    $data['sub_title'] = 'intro';
  2. but it show empty data.. so I try @section('title', '') .. same result..

Expected behavior
It must show 'intro' !!!

Screenshots
I think it's unnecessary.

Versions (please complete the following information):

  • PHP: PHP 5.6.40-1+ubuntu14.04.1+deb.sury.org+1 (cli)
  • CodeIgniter: 3.0.6
  • Slice: 1.3.0

Additional context
Could you give me your hand?

Two args @section directive not working as intended

Hi,

First of all: Thanks for this awesome and clean library, it has helped me a lot but today I've been struggling with my "main.slice.php" where I have <title>@yield('title', 'Placeholder')</title>, it works fine, but when I extend it is where the problem starts.

When I do
@extends('main')
@section('title', 'Hello')
It throws an error on eval'd code "Message: syntax error, unexpected '?'"

But it works fine if I add @endsection
@extends('main')
@section('title', 'Hello')
@endsection

Is that intended?
I'm working on HMVC btw

Thanks

um...@include not working..

hi~!
You've created a really cool library!


but, @include is not working... (my case)

[ just my code (for phpstorm syntax) ]

application/libraries/Slice.php -- 24public $slice_ext       = '.blade.php';

application/config/slice.php -- 19$config['slice_ext'] = '.blade.php';

[ view folder ]

ㄴ main.blade.php
ㄴ main_modal.blade.php

[ main.blade.php ]

@include('main_modal')

[error msg]

error

error when using JavaScript framework angularjs

I used this code in my view file:
@{{myForm.myInput.$valid}}

But I got the following error:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: valid

Filename: libraries/Slice.php(519) : eval()'d code

Line Number: 3

Backtrace:

File: D:\xampp\htdocs\test\application\libraries\Slice.php(519) : eval()'d code
Line: 3
Function: _error_handler

File: D:\xampp\htdocs\test\application\libraries\Slice.php
Line: 519
Function: eval

File: D:\xampp\htdocs\test\application\libraries\Slice.php
Line: 364
Function: _run

File: D:\xampp\htdocs\test\application\controllers\Method.php
Line: 20
Function: view

File: D:\xampp\htdocs\test\index.php
Line: 315
Function: require_once

Can i use CI flash messges with slice ?

Previously i used xiaoler/blade blade library with codeignitor and found that i cant use flash messages with it, is it possible to use codeignitor flash messages with slice ? Secondly, does slice provide all the features that blade provides so i can use it in my next CI project ?

Are Slice Library is support for Codeigniter v4 ? i hope your team can developing slice library for future ci4 thanks

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

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.