GithubHelp home page GithubHelp logo

proai / laravel-handlebars Goto Github PK

View Code? Open in Web Editor NEW
39.0 7.0 18.0 76 KB

:bicyclist: Laravel wrapper of LightnCandy for using Handlebars (and Mustache) templates

License: MIT License

PHP 86.35% HTML 5.48% Blade 0.77% Handlebars 7.40%
laravel php handlebars mustache lightncandy

laravel-handlebars's People

Contributors

ftrotter avatar greut avatar kohenkatz avatar laravel-shift avatar lorti avatar markusjwetzel avatar matthewgoslett avatar thijsdaniels avatar torann 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-handlebars's Issues

Support for LightnCandy v0.94

Hi Markus,

do you plan on updating this package to use the latest LightnCandy?
I have naively updated the version number in a fork and got some error messages. It seems that there have been some API changes since v0.89?

If you don't have the time I will look further into it and maybe open up a pull request.

Grüße aus Linz,
Manuel

How to add a custom helper?

Hi,

I'm quite lost on how to use the package with Laravel 5. I want to use the templates both in server and in client.

  • I've created a helper called hotness in /resources/views/helpers/hotness.js.
  • I've added 'helpers' => ['hotness'], to /config/handlebars.php.
  • Then I use {{hotness job.company.hotness }} on my .hbs template

I have the following error:

ErrorException in lightncandy.php line 792:
Can not find custom helper function defination hotness() ! (View: /home/.../resources/views/job/list.blade.php)

What's wrong? Also, could you share a working example?

Thanks a lot!!

Support for view composers on partials

Ok so I realise this is a niche feature most people won't use, but I find especially useful when using a template engine like this.

View composers work fine for the main view or layout of a page but when you register for partial included using the {{> _partialname }} syntax it never fires.

I currently use the view composer for my layout controller which trims that down nicely, and lets me set some variables as arrays that I cannot access directly in handlebars normally. I wanted to implement a view composer for a partial called sidebar since I have a lot of custom stats being pulled in for the sidebar, but currently limited to one view composer.

Bad indentation in case of nested partials

I have 3 handlebars template files inside views/templates directory:
test.hbs:

<div>
  <p>Hello</p>
  {{> testPartial}}
</div>

testPartial.hbs:

<div>
  <p>This is inside a partial</p>
    {{> testPartial2}}
</div>

testPartial2.hbs:

<div>
  <p>This is inside partial 2</p>
</div>

The bug

When I execute return view('templates.test');, I get:

<div>
  <p>Hello</p>
  <div>
    <p>This is inside a partial</p>
      <div>
      <p>This is inside partial 2</p>
    </div>
</div>
</div>

But I should get:

<div>
  <p>Hello</p>
  <div>
    <p>This is inside a partial</p>
      <div>
        <p>This is inside partial 2</p>
      </div>
  </div>
</div>

In case of handlebars with javascript, the indentation works as expected.

The handlebars.php config file:

<?php

use ProAI\Handlebars\Support\LightnCandy;

return [

    /*
    |--------------------------------------------------------------------------
    | Flags
    |--------------------------------------------------------------------------
    |
    | Set Lightncandy flags here. See https://github.com/zordius/lightncandy
    | for more information.
    |
    */

    'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_HANDLEBARSJS_FULL,

    /*
    |--------------------------------------------------------------------------
    | File Extensions
    |--------------------------------------------------------------------------
    |
    | All file extensions that should be compiled with the Handlebars template
    | engine. Unless you specify your own partial resolver the package will
    | look for files in Laravel's view storage paths.
    |
    */

    'fileext' => [
        '.handlebars',
        '.hbs',
    ],

    /*
    |--------------------------------------------------------------------------
    | Cache Busting
    |--------------------------------------------------------------------------
    |
    | Using nested Handlebars partials makes is difficult to determine if the
    | view at a given path is expired. Therefore you can specify environments
    | where the cached views will be recompiled on each request.
    |
    */

    'development_environment' => [
        'local',
    ],

    /*
    |--------------------------------------------------------------------------
    | Partials
    |--------------------------------------------------------------------------
    |
    | https://github.com/zordius/lightncandy#partial-support
    |
    */

    'partials' => [],
    'partialresolver' => false,

    /*
    |--------------------------------------------------------------------------
    | Helpers
    |--------------------------------------------------------------------------
    |
    | https://github.com/zordius/lightncandy#custom-helper
    |
    */

    'helpers' => [],
    'helperresolver' => false,

    /*
    |--------------------------------------------------------------------------
    | Language Helpers
    |--------------------------------------------------------------------------
    |
    | Use this option, if you want to use the language helpers in a template.
    | You can use a {{lang ...}} and {{choice ...}} helper. Both have the same
    | behaviour like the @lang and @choice Blade directives.
    |
    */

    'language_helpers' => true,

    /*
    |--------------------------------------------------------------------------
    | Optional Raw Output
    |--------------------------------------------------------------------------
    |
    | If this option is set to true, you can pass a $raw variable to the data
    | array. If $raw is true, then the template will be returned without
    | rendering in raw format. This is helpful if you want to use a Handlebars
    | template clientside with javascript.
    |
    */

    'optional_raw_output' => true,

    /*
    |--------------------------------------------------------------------------
    | Translate Raw Output
    |--------------------------------------------------------------------------
    |
    | If language_helpers and optional_raw_output are set to true, this option
    | can also set to true. If so, the translation helpers will also be
    | rendered for the raw output.
    |
    */

    'translate_raw_output' => true,

];

It's not possible to create a helper function for dynamic partial names.

I have tried to create a helper function to render dynamic partials in my code.
I've added these in the config/handlebars.php file:

'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_RUNTIMEPARTIAL,

'partials' => [],
    'partialresolver' => function ($cx, $name) {
        if (file_exists(resource_path("views/partials/$name.hbs"))) {
            return file_get_contents(resource_path("views/partials/$name.hbs"));
        }
        return '';
    },


'helpers' => [
        'concat' => function ($path) {
            return $path;
        },
    ],
    'helperresolver' => function ($cx, $name) {
        if ($name === 'concat') {
            return (function () {
                return '';
            });
        }
        return '';
    },

I've added these in the resources/views/home.hbs file:

<main id="main">
        {{#each data }}
            {{> (concat this.name) this}}
        {{/each}}
    </main>

I've made this helper function work, but the partial resolver is not working after the helper function is called.

Pre-compile constants on serverside

For now we can pre-compile language variables on serverside (when option $translate_raw_output is true) and then output the raw template with compiled language variables. This is helpful, because language variables are unlikely to change and otherwise we would have to pass all language variables to clientside and compile them on clientside each time we use the template.

The same is true for some other variables. For example we have a post comment form, which shows the current logged in user. The current logged in user will be the same over the whole page. So it would be nice to compile all user information on clientside and deliver the raw template with compiled user information to the client.

Solution:

  • Add a new helper function {{const var}} or {{fix var}}, where var can be any variable that should be compiled on serverside even for raw output (e. g. {{const username}}).
  • Rename option $translate_raw_output to $prepare_raw_output, so this option refers to language AND constant variable compilation.

Prevent caching if modified date is out

Hi there,
I seem to be getting a lot of caching problems with my content. It is easy to fix but gets quite mundane to delete multiple files everytime you make a change to a template.

May I suggest implementing something where the modified dates of both the template and the cached file are compared and if the modified date of the template is newer than the cached file then the cache gets regenerated.

I have done this in laravel 5 before and would be willing to contribute code if needed.

How to use this client side?

I'm using laravel 5.2 and raw output doesn't work for me

File: resources/views/test/sample.hbs

<div class="entry">
    <h1>{{title}}</h1>
    <div class="body">{{body}}</div>
</div>

File: resources/views/template/default.blade.php

<head>
...
    <script id="sample-template" type="text/x-handlebars-template">
        @raw('test.sample')
    </script>
...
</head>

Output:

    <script id="sample-template" type="text/x-handlebars-template">
        <div class="entry">
    <h1></h1>
    <div class="body"></div>
</div>
    </script>

Basically {{title}} and {{body}} is gone, so I can't use the template in javascript.
I'm using the default config file and laravel-handlebars version 1.1.0

View not found (with .hbs extension)

Hi again, sorry just pointing out another small thing.

When I was trying to set this up and publish the config I originally got a view not found error.

Looking at the config I realize you have this line
'fileext' => [
'.handlebars',
'.hb',
],

Adding .hbs to this works but is the "hb" a typo or is that another file extension people use for handlebars? I know hbs and handlebars are used quite commonly as the extension.

publish config

You need to publish the config using

php artisan vendor:publish --tag=config

I do not see that in the readme, but its pretty important as you discuss configuration...

Esp since you do not get support for .mustache file without modifying that file... it would also be nice to support those out-of-the-gate.

Fix PHP 8.2 deprecations

'Use of "self" in callables is deprecated'

This line uses the following syntax for passing a function to array_map:

$data = array_map('self::convertObjectToArray', $data);

PHP 8.2 has deprecated this callable format, because it does not always work properly.

The following syntax should provide equivalent behavior, and is not deprecated:

$data = array_map([$this, 'convertObjectToArray'], $data);

(There are several other equivalent syntaxes, but this is the one I personally like best.)

'Creation of dynamic property ProAI\Handlebars\Compilers\HandlebarsCompiler::$options is deprecated'

This line creates an $options property on the HandlebarsCompiler object, which was not previously defined.

The class should probably have $options defined as an explicit property.

Last update broke using partials from template dir

hi there,

When updated lightncandy to v0.89, it only looks for partials in resources/views

I'm using pingpong modules

downgrading solved the issue, but latest laravel handlebars, requires lightncandy v0.89

Laravel 8 support

Thanks for this package and I realise it's quite old but it's an important piece of my app.

Is Laravel 8 compatibility just a matter of updating the dependancies? I'm more of a front end guy and have explored trying to test this locally but have not had much luck.

Thanks!

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.