Git Product home page Git Product logo

laravel.smarty's Introduction

Laravel.Smarty

Smarty Template Engine for Laravel
(Support for Laravel5.x - Laravel8.x and Lumen)

Build Status Coverage Status Scrutinizer Code Quality

License Latest Version Total Downloads

Installation For Laravel

Require this package with Composer

$ composer require ytake/laravel-smarty

or composer.json

"require": {
  "ytake/laravel-smarty": "^6.0"
},

Supported Auto-Discovery(^Laravel5.5)

add Laravel.Smarty Service Providers

your config/app.php

'providers' => [
    // add smarty extension
    Ytake\LaravelSmarty\SmartyServiceProvider::class, 
    // add artisan commands  
    Ytake\LaravelSmarty\SmartyConsoleServiceProvider::class, 
]

Installation For Lumen

Require this package with Composer

$ composer require ytake/laravel-smarty

or composer.json

"require": {
  "ytake/laravel-smarty": "~2.0"
},

register Laravel.Smarty Service Providers

your bootstrap/app.php

$app->configure('ytake-laravel-smarty');
$app->register(Ytake\LaravelSmarty\SmartyServiceProvider::class);
$app->register(Ytake\LaravelSmarty\SmartyConsoleServiceProvider::class);

Configuration

publish configuration file (for Laravel5)

$ php artisan vendor:publish

publish to config directory

Of Course, Blade Template can also be used to Render Engine.

configuration file (for Lumen)

Copy the vendor/ytake/laravel-smarty/src/config/ytake-laravel-smarty.php file to your local config directory

config for Production

edit config/ytake-laravel-smarty.php

    // enabled smarty template cache
    'caching' => true, // default false
    
    // disabled smarty template compile
    'force_compile' => false, // default true(for develop)

Or

add .env file

SMARTY_CACHE=true
SMARTY_COMPILE=false

edit config/ytake-laravel-smarty.php

    'caching' => env('SMARTY_CACHING', false),
   
    'force_compile' => env('SMARTY_FORCE_COMPILE', true),

and more..!

Basic

easily use all the methods of Smarty

// laravel5 view render
view("template.name");

// Laravel blade template render(use Facades)
\View::make('template', ['hello']);
// use Smarty method

\View::assign('word', 'hello');  
\View::clearAllAssign(); // smarty method

View Composer, and View Share

$this->app['view']->composer('index', function (View $view) {
    $view->with('message', 'enable smarty');
});
$this->app['view']->share('title', 'laravel-smarty');
Hello Laravel.Smarty

{$title}

{$message}

Artisan

smarty's cache clear, remove compile class from Artisan(cli)

Template cache clear

$ php artisan ytake:smarty-clear-cache
Options description
--file (-f) specify file
--time (-t) clear all of the files that are specified duration time
--cache_id (-cache) specified cache_id groups

Remove compile file

$ php artisan ytake:smarty-clear-compiled
Options description
--file (-f) specify file
--compile_id (-compile) specified compile_id

Template Compiler

$ php artisan ytake:smarty-optimize
Options description
--extension (-e) specified smarty file extension(default: .tpl)
--force compiles template files found in views directory

Template Caching

choose file, memcached, Redis
(default file cache driver)

// smarty cache driver "file", "memcached", "redis"
'cache_driver' => 'file',

// memcached servers
'memcached' => [
    [
        'host' => '127.0.0.1',
        'port' => 11211,
        'weight' => 100
    ],
],

// redis configure
'redis' => [
    [
        'host' => '127.0.0.1',
        'port' => 6379,
        'database' => 0,
    ],
],

example

registerFilter in ServiceProvider
registerFilter in Controller
layout.sample
layout.extends.sample

laravel.smarty's People

Contributors

cabloo avatar coderua avatar dariusiii avatar eschalks avatar jaybizzle avatar kevincobain2000 avatar laravel-shift avatar likemusic avatar linushstge avatar m-bymike avatar unstoppablecarl avatar ytake 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel.smarty's Issues

Run my views

Hello,

First I need to say this packge are awesome!

I have a doubt. All times I change a view I need to do
php artisan ytake:smarty-clear-cache
php artisan ytake:smarty-clear-compiled
php artisan ytake:smarty-optimize

Is this right? I really need to do it all times I change my views or have other way to refresh?

Laravel 10 support

I see that there is a PR for this, could it be reviewed and merged so we can use this pckage with new Laravel version? Thank you in advance @ytake !

Installing issue on Laravel 8.12

`Invalid argument supplied for foreach()

at vendor/ytake/laravel-smarty/src/SmartyFactory.php:214
210▕
211▕ $smarty->error_reporting = Arr::get($config, 'error_reporting', E_ALL & ~E_NOTICE);
212▕ // SmartyTemplate class for laravel
213▕ $smarty->template_class = SmartyTemplate::class;
➜ 214▕ foreach ($config as $key => $value) {
215▕ if (in_array($key, $this->configKeys)) {
216▕ $smarty->{$key} = $value;
217▕ }
218▕ }

  +21 vendor frames 

22 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))`

Call to undefined method Ytake\LaravelSmarty\Smarty::setTemplateDir()

Getting error:
Fatal error: Call to undefined method Ytake\LaravelSmarty\Smarty::setTemplateDir() in ProjectDir\vendor\ytake\laravel-smarty\src\SmartyFactory.php on line 203.

"laravel/framework": "5.0.*",
"ytake/laravel-smarty": "^2.1"

Want to render: resources/views/login.tpl
With cmd: \View::make('login')

Own plugin

Hello, is it possible and how to write my own plugin, which won't be in the default smarty plugin directory inside vendor folder, but samewhere inside app folder? Thanks

Nocache doesn't seem to work in included templates

(Laravel 5)

The nocache filter and block tag don't seem to work when including templates. Putting {$smarty.now nocache} or {nocache}{$smarty.now}{/nocache} in an included template always outputs the time that the template was first run after being modified. NOTE: the same does not apply when adding the nocache attribute to the {include}.

I have caching on and compile_check enabled.

Update from Laravel 5.8 to 6.0 fails, illuminate/view required

Hi there,

I'm trying to update an old project but stucked from 5.8 to 6.0. It tells me that illuminate/view is required but that is replaced by Laravel 6.0.x

- ytake/laravel-smarty 2.5.0 requires illuminate/view 5.5.*|5.6.*|5.7.*|5.8.* -> satisfiable by illuminate/view[v5.5.0, ..., 5.8.x-dev].
- ytake/laravel-smarty 2.4.0 requires illuminate/view 5.5.*|5.6.*|5.7.* -> satisfiable by illuminate/view[v5.5.0, ..., 5.7.x-dev].
- Only one of these can be installed: illuminate/view[v5.5.0, ..., 5.8.x-dev], laravel/framework[v6.0.0, ..., v6.0.4]. laravel/framework replaces illuminate/view and thus cannot coexist with it.

How can I resolve this problem? I thought it is compatible with Laravel 8x as well?

Thanks for any help.

ytake-laravel-smarty.php config file has no effect

Hi!
Using Laravel 7.14.1 with Laravel.Smarty 4.0.0.
Changing the 'template_path' variable in ytake-laravel-smarty.php has no effect on loading view files from that path.
I had to change the path from config/view.php for it work. Is this expected behavior?

Same goes for the compile/cache path.

Artisan queue:work command does not clear assignments in mailables

In Version 6 and 7 there is an issue with assigned template variables in relation with the queue:work command.
Instead of queue:listen queue:work does not always boot Laravel so the existing Smarty Instance is kept.

Steps to reproduce

  1. Create two Mailables
  2. Create a simple smarty template where the variable is dumped
  3. Assign a variable only to the first mailable view
  4. Queue both Mails

Result in queue:listen: As expected, the variable gets only applied to the first mailable
Result in queue:work: The variable is dumped in both templates

Example files

App/Mail/TestMail1.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;

class TestMail1 extends Mailable
{
    use Queueable;

    public function build()
    {
        $this->view('mail.test', [
            'example' => 'assigned to first mailable'
        ]);

        return $this;
    }
}

App/Mail/TestMail2.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;

class TestMail2 extends Mailable
{
    use Queueable;

    public function build()
    {
        // "example" is not applied here
        $this->view('mail.test');

        return $this;
    }
}

templates/mail/test.tpl

<pre>{$example}</pre>

TestController.php

Mail::queue(new TestMail1());
Mail::queue(new TestMail2());

Required Changes

Smarty assignments have to be cleared on each Mailable.

In the current implementation, there is a risk that variables that are not overwritten by a second Mailable are erroneously transmitted in an email.

Notes

  • Tested with Laravel 9 and 10.
  • Smarty Caching false
  • Smarty Force Compile true
  • The test $variable is applied to all further mailables (even if the template file name differs)
  • This issue only occurs with Smarty templates, Blade templates are not affected

PHP 8 support

ytake/laravel-smarty 5.0.0 requires php ^7.3 -> your php version (8.0.5) does not satisfy that requirement.

somethings wrong in laravel6.0

notice me don't have function configure in bootstrap/app.php

image

and the Error: Fatal error: Uncaught Error: Call to undefined method Illuminate\Foundation\Application::configure() in /Users/dk/workspace/SGame/bootstrap/app.php:46 Stack trace: #0 /Users/dk/workspace/SGame/public/admin/index.php(39): require_once() #1 {main} thrown in /Users/dk/workspace/SGame/bootstrap/app.php on line 46

if i remove it.
image

i will get this error: Fatal error: Uncaught ReflectionException: Class config does not exist in /Users/dk/workspace/SGame/vendor/laravel/framework/src/Illuminate/Container/Container.php:803 Stack trace: #0

accessing the Laravel-Session in $smarty.session

Dear all,
i like to share my .tpl files between projects with and without Laravel.
My last problem is to access the Laravel-Session-Variables in the template as $smarty.session...
I am aware that i can access it with session() inside the template, but i had to change a lot of templates.
so - my question is: would it be possible to add/change the behaviour of $smarty.session... ?
i found a possible candidate in smarty_internal_compile_private_special_variable.php, but i am not sure if it is the right place :)
kr Tom

View composers not firing for included files

I have this simple test file...

<!-- base.tpl -->

{include file='main-nav.tpl'}

Then i have this view composer...

<?php

namespace App\Providers;

use App\Repositories\Eloquent\UsersRepository;
use Illuminate\Support\ServiceProvider;

class ViewComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot(UsersRepository $users)
    {
        $this->app['view']->composer('main-nav', function ($view) use ($users) {
            $view->with('userCount', $users->count());
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

...but it seems that view composer aren't fired for any files that are included like this.

Looking at the output of in Debugbar, the included main-nav.tpl doesn't get logged as rendered.

screen shot 2016-06-08 at 21 21 04

Laravel 9 Support

Since Laravel 9 is now out, could the constraints be updated? 👍🏻

Sessions not working in Laravel 5.2.29

I am not sure why but sessions appear to not work when using Laravel Smarty views.
I got them working again by changing code here https://github.com/ytake/Laravel.Smarty/blob/master/src/Smarty/Engines/SmartyEngine.php#L58
to not use the ob functions.

    protected function evaluatePath($path, array $data = [])
    {
        try {
            if (!$this->smarty->isCached($path)) {
                foreach ($data as $var => $val) {
                    $this->smarty->assign($var, $val);
                }
            }
            // render
            $cacheId = isset($data['smarty.cache_id']) ? $data['smarty.cache_id'] : null;
            $compileId = isset($data['smarty.compile_id']) ? $data['smarty.compile_id'] : null;
            return $this->smarty->fetch($path, $cacheId, $compileId);

        } catch (\Exception $e) {
            $this->handleViewException($e);
        }
    }

I am not sure what is causing this or why this fixes it.

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.